Format SQL queries to make them more readable.
All calculations performed locally in your browser. No data sent to server.
Results are for informational purposes. Verify results with other sources.
This formatter matches keywords, so it will break a string containing the word FROM
It splits your query on whitespace and punctuation and inserts a line break before any of twenty-three keywords. It has no idea what a string literal is. Format SELECT name FROM t WHERE label = 'order from stock' and the literal comes back as 'order\nfrom stock' — a newline inside the quotes, and a query that now matches something different.
How it works
- Collapses all runs of whitespace, then splits the query into tokens on spaces, brackets, commas and semicolons.
- Starts a new line before each of twenty-three clause keywords, matched case-insensitively.
- Tracks nesting with opening and closing parentheses and indents two spaces per level.
for each token:
if token is ')' → indent = max(0, indent − 1)
if token is a clause keyword → emit newline + two spaces × indent
emit the token unchanged
if token is '(' → indent = indent + 1Worked example
A query whose text contains a keyword inside quotes.
- input: SELECT name FROM t WHERE label = 'order from stock'
- SELECT, FROM and WHERE each start a new line, as intended
- the word from inside the quotes is also matched, because quoting is invisible to the matcher
- output puts a line break between 'order and from stock'
- the literal is no longer the same string it was
Three intended line breaks and one that changes the meaning of the query. Everything outside string literals formats correctly; everything inside them is at risk.
Reading the result
- Check any query containing quoted text before you run the result. Comments are the same hazard — a keyword inside a comment gets a line break too, and in a single-line comment that break can push the rest of the comment onto a live line of SQL.
- Indentation follows parentheses only, so it reflects nesting depth rather than clause structure. A long flat query with no brackets comes out entirely unindented no matter how many clauses it has, which is correct behaviour for this rule and often not what you wanted.
- The keyword list is fixed and general. Dialect-specific syntax — window functions, common table expressions, vendor extensions, MERGE — is not in the list and will be left inline rather than given its own line.
- Only whitespace is ever added. Tokens are emitted exactly as you typed them, so your capitalisation is preserved and no keyword is rewritten; the tool changes layout, not text.
Common questions
- Is it safe to run on production queries?
- Format first, then read the output before using it. The tool never rewrites your tokens, but it can insert a newline inside a quoted string or a comment, and in both cases the resulting SQL means something different from what you pasted in.
- Why is my nested query not indented?
- Indentation is driven purely by parentheses. A subquery wrapped in brackets indents; a series of joins and clauses with no brackets does not, because the tool has no model of clause structure to indent against.