This page puts SQL clauses on separate lines and indents joins, conditions and nested queries. Paste a one-line query, choose upper or lower case for keywords, and the formatted query appears beside the form. It works on the text rather than on a database connection, so no server, credentials or schema are involved.
It is a cosmetic formatter built around a list of keywords and clause boundaries. That is enough to make a generated query readable, and it also means the tool can be fooled by input that is not SQL. The output is valid as long as the input was valid; the formatter does not add missing clauses or check table names.
What it formats
The major clauses start on a new line: SELECT, FROM, WHERE, GROUP BY, HAVING, ORDER BY, LIMIT, OFFSET, JOIN, UNION and their relatives. A join's ON condition is indented under the join, and a second condition joined with AND is indented to the same level.
Commas inside a select list stay on the same line, so a query with ten columns produces one long line rather than one column per line. SET assignments in an UPDATE also stay together. That keeps the output compact, though it is less aggressive than a formatter that breaks every list item.
String literals, quoted identifiers and comments are tokenised so their contents are not treated as keywords. "select", `from` and [where] stay lower case and unchanged inside their quotes, and a string containing 'select from where' is left alone. Line comments and block comments are kept in place.
Keyword case and the indent option
Keyword case has two settings. UPPERCASE is the common convention for SQL keywords, and lowercase is preferred by some teams and by tools that generate queries. The choice applies to recognised keywords only; identifiers and string contents keep their original case.
Indent AND and OR conditions now works under WHERE, HAVING and ON. With it on, each top-level condition goes on its own line one level in. The sample's WHERE o.status = 'paid' AND o.created_at >= '2026-01-01' becomes two lines, and the query grows from 11 lines to 12 without changing any value. With it off, the conditions stay on the clause line.
A condition that is already inside its own parentheses stays together, so WHERE (a = 1 AND b = 2) OR c = 3 keeps the grouped pair on one line and breaks only at the outer OR. That preserves the meaning of the grouping instead of exploding every boolean operator.
A worked example
A 95-token join query with six clauses was formatted with keywords in upper case and the indent option on. The result was 12 lines and 332 characters; with the condition option off it was 11 lines and 330 characters.
| Query | Result |
|---|---|
| 95-token join with six clauses, conditions on | 12 lines, 332 characters |
| Same query, conditions off | 11 lines, 330 characters |
| CTE with a nested aggregate, 59 tokens | 12 lines, 266 characters; inner query indented |
Subquery in WHERE, 28 tokens | 9 lines, 135 characters |
The sample output put each join on its own line, indented the two ON conditions, kept the select list, the GROUP BY list and the ORDER BY list on single lines, and placed the second WHERE condition on its own indented line.
Lowercasing the keywords changed only the keywords: select, from, join, on, where, group by, having, order by and limit. The table and column names were untouched, so the two outputs are the same query.
SELECT can still be treated as a query.Subqueries, CTEs and comments
A parenthesised query is now indented as a block. WITH recent AS ( opens one level in, the inner SELECT, FROM and WHERE sit at that level, and the closing parenthesis returns to the outer level on its own line. The test CTE formatted to 12 lines and 266 characters with the inner aggregate on its own clause lines.
The same applies to a subquery after IN or EXISTS. In the test, WHERE id IN ( was followed by the inner SELECT at one level in, its own WHERE and indented AND, then a closing parenthesis at the outer level. A condition that follows the subquery, such as AND active = 1, returns to the outer condition indent rather than staying on the closing line.
A second CTE after a comma starts on a new line, so a chain of WITH blocks reads as a list rather than one long line. A nested subquery goes one level deeper again. The formatter tracks parentheses for this purpose but still does not parse the query, so unbalanced parentheses in the input will produce unbalanced indentation.
Comments are kept and placed where they appeared. A line comment stays on its own line, and a block comment inside a statement stays on the line before the clause it preceded. They are not removed or reordered, which matters when a comment explains why a condition exists.
Limits and dialect notes
The formatter does not parse a dialect. PostgreSQL dollar-quoted strings, MySQL DELIMITER blocks, SQL Server GO batches and procedural bodies are not handled specially. A dollar-quoted body containing semicolons can be split at the wrong place, and a stored procedure may not survive the round trip intact.
It also does not validate. A misspelled keyword is treated as an identifier, a missing FROM is not reported, and a query that would fail on the server formats without complaint. Use an EXPLAIN or a database client when correctness matters.
For a dialect-aware formatter with more layout options, use a dedicated library or a database tool. This page is for making a generated or copied query readable quickly. The code diff checker is useful for comparing the formatted query with the original, and the regex tester helps when you need to extract part of a query from a log.