SQL Formatter & Beautifier
Format messy SQL with a tokenizer that preserves string literals and comments exactly — clause line breaks, indented columns, and a keyword-case toggle. Runs in your browser.
Input SQL
Formatted SQL
The one job of a formatter: change the layout, never the meaning
A SQL formatter has one job that sounds trivial and is not: rearrange whitespace and keyword casing without ever changing what the query does. SQL was first standardized by ANSI in 1986 and by ISO in 1987, and it has been revised many times since — SQL-89, the large SQL-92 revision, SQL:1999 (which added recursive queries and the WITH clause for common table expressions), SQL:2003 (window functions), and SQL:2016 (JSON operators). Across all nine of those revisions the standard says nothing about layout, so every query arrives formatted however its author left it. The naive way to reformat it — run a handful of global search-and-replace patterns over the whole string — breaks on the first query that contains a string literal or a comment, because a blind regex cannot tell the difference between the keyword FROM in the query and the word from inside 'shipped from warehouse 3'. This formatter takes the correct approach instead: it tokenizes the input first, then formats only the parts that are actually code.
What breaks without a tokenizer
A pattern that inserts a line break after every comma will split the value 'New York, USA' into two lines, silently corrupting your data. A pass that uppercases keywords will turn the stored text 'select the best option' into 'SELECT the best option', rewriting a string the database returns to a user. And collapsing all whitespace merges a trailing line comment — everything after the two dashes runs to the end of its line — into the following clause, so a -- get active users comment can end up commenting out the WHERE. All three failures share one root cause: treating a flat character stream as if every comma and keyword were structural, when some live inside quotes or comments.
How tokenizing fixes it
The formatter scans the query one character at a time and groups it into six typed tokens: quoted strings (single, double, and backtick, with doubled-quote and backslash escapes handled), line comments starting with two dashes, block comments wrapped in slash-star, punctuation like commas and parentheses, whitespace, and everything else as words. Strings and comments become opaque tokens emitted byte-for-byte, so nothing inside them is ever re-cased or line-broken. Only word and punctuation tokens flow into the layout pass, which is why 'New York, USA' survives intact while a structural comma in a column list still triggers a new line. The same rule protects a value like 'O''Brien', where the two single quotes are an escaped apostrophe inside one string, not the end of one string and the start of another.
The layout, and why it reads well in review
The layout follows the vertical style most database teams settle on. Major clause keywords — SELECT, FROM, WHERE, the six JOIN variants (INNER, LEFT, RIGHT, FULL, CROSS, and plain JOIN), GROUP BY, ORDER BY, HAVING, LIMIT, and data-modification starters like INSERT INTO and UPDATE — each begin a new line, so the shape of the statement is visible at a glance. Multi-word starters are matched longest-first, so LEFT OUTER JOIN is recognized as one clause rather than three separate words. Columns after SELECT are indented two spaces, one per line, so a diff that adds a column touches exactly one line. The boolean connectors AND and OR drop to their own indented line under the WHERE, which makes a dangling connector or an unintended condition easy to spot in review. Keyword casing is a toggle: uppercasing the roughly 60 reserved words this tool knows while keeping identifiers lowercase is the convention followed in the PostgreSQL and MySQL style guides, because it lets the eye separate fixed grammar from your own table and column names — but you can turn it off with one click.
Two honest limits
First, this is a formatter, not a parser or a validator — it does not build a syntax tree, so it will not catch a missing comma, an unbalanced parenthesis, or a misspelled column, and it does not encode the full grammar of any one of the five major dialects: MySQL, PostgreSQL, SQLite, Microsoft SQL Server, and Oracle Database. It normalizes layout; it does not judge correctness. Second, deeply nested subqueries and common table expressions are laid out with the same clause rules rather than fully recursive indentation, so a query four levels deep may still want a manual pass. Within those two limits the transformation is safe and reversible: because strings and comments are preserved exactly and only whitespace and keyword case change, the formatted query executes identically to the one you pasted, and every byte is processed in your browser with nothing uploaded.
How to Use
Paste your SQL query into the input box.
Toggle uppercase keywords on or off.
Click Format SQL to beautify it.
Copy the formatted output.
Features
Common Questions
About SQL Formatter
Paste messy SQL and get vertical, review-friendly formatting: each major clause on its own line, columns indented one per line, AND/OR broken out, and an uppercase-keyword toggle. It tokenizes first, so commas and keywords inside string literals or comments are never altered — the formatted query runs identically to the one you pasted. Handles keywords across MySQL, PostgreSQL, SQLite, SQL Server, and Oracle.
Also known as: format sql, sql beautifier, prettify sql query, sql pretty print, beautify sql, sql indent, clean up sql, sql query formatter, format select query, sql code formatter, mysql formatter, postgres formatter, tidy sql, sql formatter online.
Processing Note
SQL Formatter runs in your browser, so the input you enter is processed locally on this page and is not uploaded to a ToolMintX account.
Tool Limits
IT tools provide quick diagnostics and transformations. They cannot see every private network, deployment setting, proxy, firewall, or production edge case.
Explore More
AI VRAM Calculator
Estimate GPU VRAM for LLM inference and training using model, quantization, users, and context length.
Client-sideAI API Cost Calculator
Compare and estimate AI API costs across OpenAI, Claude, Gemini, DeepSeek and more for text, image, video, and embeddings.
Client-sideAPI Key and .env Secret Generator
Generate secure .env secrets plus selectable Hugging Face, OpenAI, JWT, database, and webhook variables.
Client-sideSubnet Calculator
IPv4 subnet calculator: network and broadcast address, subnet and wildcard mask, usable host range, and host count from any IP and CIDR prefix.
Client-side