What the formatter does to a query
select u.id, u.email, count(o.id) as orders from users u left join orders o on o.user_id = u.id
and o.status = 'paid' where u.active = true and u.country in ('GB','US') group by u.id, u.email
having count(o.id) > 2 order by orders desc limit 20
SELECT
u.id,
u.email,
COUNT(o.id) AS orders
FROM
users u
LEFT JOIN orders o ON o.user_id = u.id
AND o.status = 'paid'
WHERE
u.active = TRUE
AND u.country IN ('GB', 'US')
GROUP BY
u.id,
u.email
HAVING
COUNT(o.id) > 2
ORDER BY
orders DESC
LIMIT 20
The layout follows a small set of rules, applied the same way every time:
- Each clause starts a line —
SELECT,FROM,WHERE,GROUP BY,HAVING,ORDER BY— and its contents sit on the lines beneath, one level in. - One item per line in a select list, a
GROUP BY, anORDER BYor aSET, with the comma at the end. - Each join on its own line, with its
ONcondition beside it and any further conditions indented underneath. ANDandORstart a line, so the conditions of aWHEREread as a list. TheANDinsideBETWEEN … AND …is left where it is.- Subqueries and common table expressions are indented as blocks, with the closing parenthesis back at the outer level.
CASEis opened up, with eachWHENand theELSEon a line of its own.- Function calls and
INlists stay on one line. - Statements are separated by a blank line when the input holds several.
Comments are kept, both -- and /* */, and stay attached to the line they were
on. Minify does the reverse of all this and produces one line, turning
-- comments into /* */ so that they cannot swallow the rest of the statement.
Why layout matters more in SQL than most languages
A query's bugs hide in its structure. Whether a condition belongs to the JOIN or to the
WHERE changes which rows a LEFT JOIN returns, and on a single line the two
look the same. Laid out, the difference is one of indentation: a condition under the join filters the
joined table before matching; the same condition under WHERE filters the result
afterwards and quietly turns the outer join into an inner one.
The same goes for mixing AND and OR. AND binds tighter, so
a OR b AND c means a OR (b AND c). With each operator starting its own line,
a missing pair of parentheses is far easier to notice. And a formatted query produces useful diffs: add
one column to a select list and one line changes, where the unformatted version shows the whole query
as modified.
Where the SQL comes from
The usual source is something that generates SQL for you. ORMs and query builders emit a single line with machine-chosen aliases; slow-query logs and database monitoring views store statements without their original line breaks; an API that returns the query it ran in an error body does the same. Paste it here first, then read it. If the query came out of an API response, the JSON viewer will find the field, and the API tester will fetch the response.
Dialects, and what the selector changes
The formatter does not need to understand a whole dialect, only to know where a string or a quoted name begins and ends, since nothing inside one may be altered. That is the part that differs.
| Standard, PostgreSQL, SQLite | MySQL, MariaDB | SQL Server | |
|---|---|---|---|
| String | 'it''s' | 'it''s' or 'it\'s' | 'it''s' |
| Quoted name | "order" | `order` | [order] |
| Line comment | -- | -- or # | -- |
| Also recognised | Dollar-quoted strings, :: casts, ->> | Backslash escapes in strings | @variables |
Choosing the wrong one matters only for input that uses those features: a MySQL string ending in an escaped quote, read as standard SQL, appears to end early and the rest of the query is misread. If the result looks wrong, or the status bar reports an unclosed string, check the dialect first.
Keyword case
SQL keywords are case-insensitive, so select and SELECT mean the same thing.
Upper case is the long-standing convention because it separates the language from your table and
column names at a glance; lower case is common in teams that find capitals noisy. The formatter changes
only reserved words and the names of built-in functions and types where they are called. Your
identifiers are never re-cased, and a column that happens to be named key,
first or date is left as written.
What it does not do
- It does not validate. The formatter lays out whatever it is given and will format a query that the database would reject. It reports unbalanced parentheses and unclosed strings, because those make any layout unreliable, and nothing else. Only the database can tell you a statement is correct.
- It does not run anything. There is no connection to a database, here or anywhere.
- It does not rewrite. No aliases are added, no joins reordered, no
SELECT *expanded. The self-check after each run compares the output with the input, token by token, ignoring only whitespace and keyword case, and warns if they differ. - Procedural code is formatted loosely. Stored procedures, triggers and vendor scripting such as PL/pgSQL or T-SQL control flow are kept intact and readable, without the block-level indentation a dedicated tool would give them.
One caution if the SQL you are formatting was built by joining strings with user input: formatting it
will not make it safe. Values belong in parameters — the $1, ? or
:name placeholders the formatter leaves untouched — and an API that builds queries any
other way is open to SQL injection, which typically surfaces as a
500 when a stray quote reaches the database.
Frequently asked questions
How do I format a SQL query online?
Paste the query into the box above and press Format. It is laid out with one clause per line, the select list and conditions indented, subqueries stepped in, and keywords in the case you choose.
Does formatting change what a SQL query does?
No. Whitespace and the case of keywords have no meaning in SQL, and those are the only things changed. After each run the tool compares its output with your input and warns if anything else differs.
Should SQL keywords be upper case?
It is a convention, not a rule: SQL keywords are case-insensitive. Upper case makes the structure of a query stand out from table and column names, which is why it is the most common style. Pick one and be consistent.
Does this formatter support MySQL, PostgreSQL and SQL Server?
Yes. The layout rules are the same for all of them. The dialect selector tells the formatter how strings, quoted identifiers and comments are written, such as backticks and backslash escapes in MySQL or square brackets in SQL Server.
Can it check my SQL for errors?
Only for unbalanced parentheses and unclosed strings or comments. It does not know your tables and does not parse the full grammar, so a query can format cleanly and still be rejected by the database.
How do I minify SQL?
Press Minify. The query is reduced to a single line with single spaces. Line comments are converted to block comments so that they do not comment out the rest of the statement.
Is my query sent to a server?
No. Formatting runs in JavaScript on this page. The query is not uploaded and is never executed.
Related tools and reference
JSON Formatter
The same job for JSON: indent, validate or minify.
XML Formatter
Pretty-print and validate XML.
Diff Checker
Compare two versions of a query line by line once both are formatted.
Case Converter
Turn snake_case column names into camelCase properties.
500 Internal Server Error
What an API returns when a query fails behind it.
API Tester
Call the endpoint whose query you are debugging.