SQL Formatter

Reflow a one-line SQL query: break at keywords, uppercase main clauses, align indentation. Handy for reading code and debugging slow queries.

Paste SQL
Hint: only whitespace and keyword case are adjusted — semantics unchanged.
Formatted result

Why formatting SQL matters

SQL is forgiving about whitespace, which means badly formatted queries still run and still pass review. The cost shows up later: a query whose joins and conditions run together takes far longer to audit than one where each clause starts on its own line. Formatting is a reviewability tool first and a style preference second.

The conventional shape

ClauseConvention
KeywordsUppercase — SELECT, FROM, WHERE
IdentifiersLowercase, ideally snake_case
Leading commasComma-first column lists make it easy to comment out a line
JoinsEach JOIN on its own line, with ON indented beneath
ConditionsOne predicate per line, with AND / OR leading
SubqueriesIndented one level, with the closing bracket aligned to its opening

Logical order is not written order

A query is read in the order it appears, but the database evaluates it differently:

FROM → JOIN → WHERE → GROUP BY → HAVING → SELECT → ORDER BY → LIMIT

This explains two things that otherwise look strange: an alias defined in SELECT usually cannot be used in WHERE, because WHERE runs first; and filtering in WHERE is far cheaper than filtering in HAVING, because HAVING runs after aggregation. Organising your formatting to mirror this order makes the logic easier to follow.

Dialects differ at the edges

A formatter that assumes the wrong dialect can rewrite a query into something that no longer parses. Match the dialect to the target database.

Do not reformat and commit in one step

Reformatting an existing query produces a diff in which every line changed, hiding the one edit that mattered. When a file is already in a different style, format it in a separate commit, or not at all. The same applies to stored procedures and migration files, where an opaque diff is genuinely risky.

What a formatter must never change

Whitespace is usually insignificant in SQL, but not everywhere. A formatter is only safe if it recognises the places where it stops being cosmetic:

A cheap verification: format the file, then diff the result against the original with whitespace ignored. If anything other than whitespace moved, the formatter touched something it should not have.

Related tools

JSON formatter XML formatter HTML / CSS formatter

FAQ

Which SQL dialects are supported?
The formatter splits on keywords independent of dialect. Main clauses of common MySQL / PostgreSQL / SQL Server / Oracle statements — SELECT, INSERT, UPDATE, DELETE, JOIN, WHERE, GROUP BY, ORDER BY and more — are put on their own lines and uppercased.
Will it change my SQL semantics?
No. Formatting only adjusts whitespace, line breaks and keyword casing; fields, table names, conditions and string contents are untouched — safe for production troubleshooting.
Is my SQL uploaded?
No. Formatting happens entirely in your browser; the SQL is never sent to any server.