How the SQL formatter lays out a statement
The formatter reads SQL as tokens rather than characters. A keyword, a name, a number, a string, an operator and a bracket are each one token, and the whitespace between them is a value that can be recomputed. Formatting therefore keeps the token order and only re-lays-out spaces and line breaks, which is why it cannot change what the statement does.
One clause per line
Clauses that shape a statement start on a new line: SELECT, FROM, WHERE, GROUP BY, HAVING, ORDER BY and LIMIT. JOIN and LEFT JOIN sit one level under FROM, and AND or OR in a condition go one level further than WHERE.
SELECT u.id, u.email FROM users u JOIN orders o ON o.user_id = u.id WHERE u.active = true AND o.total_cents > 0 ORDER BY u.id;
When a clause holds more than one item — that is, when it contains a comma — each item starts its own line. A clause with a single item stays on one line so a short query does not grow tall for no reason. The column list of CREATE TABLE and every subquery are always expanded.
The options
| Option | Values | Effect |
|---|---|---|
| Keywords | upper, lower, keep | Applies only to keywords; table and column names are untouched. |
| Indent | 2 spaces, 4 spaces, tab | The width of one level, used by subqueries and column lists alike. |
| Comma | trailing, leading | Whether a broken list ends a line with a, or starts it with , a. |
Leading commas make a diff touch one line when a column is added or removed. Trailing commas look familiar but invite a mistake on the last item. Without a team convention, pick whichever makes a review diff smaller.
What is preserved
- A string literal is one token. Spaces, line breaks and case inside it are never changed.
--line comments and/* */block comments are kept and moved to the nearest line, never dropped.- The number of semicolons and the statement boundaries stay as they were.
- Brackets are not invented or removed. An unbalanced pair is reported as a diagnostic instead.
Before you paste formatted SQL into production, glance at strings and comments once. The formatter only touches whitespace, but copying and pasting is where trailing spaces and stray tabs sneak in.
When to minify
Minifying keeps the same tokens, collapses whitespace and drops comments. Use it for the one-line statement in a log, a query in a configuration value, or a string sent over the wire. Comments disappear, so a statement that relies on them for review should stay formatted.
Why a formatting rule matters
When the same query looks different to every reader, structure stops being visible in review. With clauses starting in the same place and conditions indented alike, a missing WHERE or a wrong join condition stands out. Formatting is not taste; it is a way to lower the cost of review.
Questions
Can formatting change the execution plan?
No. Whitespace has no meaning in SQL and this tool keeps the token order. The plan is decided by indexes, statistics and the shape of conditions.
Does it tidy a single very long line?
Yes. Line breaks in the input are ignored and the tokens are read again, so a one-line statement still comes out with clause breaks.
What about stored procedure bodies?
BEGIN and END blocks are recognised as statements. Dialect-specific procedural syntax may be read differently from plain SQL, so check the result.
Format one query with each option to settle a team convention quickly.
Open the toolThis article follows the published documentation of PostgreSQL 17, MySQL 8.4 and SQLite 3.46. Every table, column and value shown is synthetic.