What makes SQL readable: clause layout, indents and leading commas explained

Why SQL is laid out one clause per line, what leading commas are for, how minifying stays safe, and how a formatter tells keywords from strings and names.

Open the SQL formatter Free, in your browser, nothing uploaded.

A database reads a query the same way whether it sits on one line or fifty, but people do not. Clear layout makes a wrong join or a missing condition jump out, keeps code reviews short and makes changes in version control easy to follow. This guide explains the common layout conventions and the small amount of parsing a formatter needs to apply them safely. It is for analysts, students and developers who read other people’s SQL. To run a query on sample data, use SQL playground; to get a spreadsheet into a table first, CSV to SQL; and for the JSON that many databases now return, JSON formatter.

What it is

SQL has a fixed order of clauses: SELECT, FROM, JOIN … ON, WHERE, GROUP BY, HAVING, ORDER BY, LIMIT. The most common layout puts each clause at the left edge and everything that belongs to it indented underneath, one item per line:

  • One column per line in SELECT, so adding or removing a column changes exactly one line.
  • One condition per line in WHERE, with AND or OR at the start, so you can read the logic down the left edge.
  • ON under its JOIN, because the join condition belongs to that table.
  • Subqueries indented a level, with the closing bracket back at the outer level, so nesting shows at a glance.

Comma style is a matter of taste. Trailing commas (a, at the end of a line) read like English. Leading commas (, b at the start) line up on the left, and you can comment out any column but the first without touching another line.

Keyword case is also taste. Upper-case keywords make the structure stand out against lower-case table and column names, which a formatter must never change, since some databases treat "Order" and "order" as different names.

To do any of this safely, a formatter first tokenizes: it splits the text into words, numbers, operators, strings, quoted names and comments. Text inside quotes or a comment is passed through untouched, so the word select inside 'select * from x' is not treated as a keyword. Minifying removes comments and spaces, but keeps a space wherever two tokens would otherwise merge: a - -1 must not become a--1, which would turn the rest of the line into a comment.

How to use it

  1. Open the SQL formatter and paste your query, or press Example to see one.
  2. Choose Keywords (UPPER CASE, lower case or leave as typed), Commas (end or start of a line) and Indent (2 spaces, 4 spaces or a tab).
  3. Press Format to lay it out, or Minify to squeeze it onto one line.
  4. Copy the result or download it as a .sql file; Clear empties the box.

A worked example

An analyst pastes this one-line query (284 bytes) that totals first-quarter orders by region:

select c.region, count(*) as orders, sum(o.total) as revenue from orders o join customers c on c.id = o.customer_id where o.placed_at between '2026-01-01' and '2026-03-31' and o.status <> 'cancelled' -- skip refunds followed on the next line by group by c.region having sum(o.total) > 1000 order by revenue desc;

Formatted with upper-case keywords and a 2-space indent, it becomes 16 lines: SELECT with three columns on their own lines, FROM orders o, JOIN customers c with ON c.id = o.customer_id indented under it, WHERE with two condition lines, then GROUP BY, HAVING and ORDER BY, each with its item indented. Three details show the tokenizer at work:

  • BETWEEN '2026-01-01' AND '2026-03-31' stays on one line, because that AND belongs to BETWEEN.
  • The dates and 'cancelled' keep their exact text, and count and sum become COUNT and SUM.
  • The comment -- skip refunds stays at the end of its line.

With leading commas and a 4-space indent, the columns read c.region, , COUNT(*) AS orders, , SUM(o.total) AS revenue. Minified, the query is 257 bytes, about 10% smaller; the comment is gone and spaces remain only where two words meet, as in as orders.

Checked with the tools

We ran the query through the tool’s own formatSql() and minifySql() (src/lib/dev/sql.ts) in Node and copied the line count, byte counts and comma layout from the output. We also checked that a string containing select * from x was left untouched and that a - -1 minified to a- -1, not to a comment.

Where it is used

  • Code review. One item per line means a change shows as one changed line.
  • Reports and dashboards. Business intelligence tools often save long one-line queries; formatting makes them auditable.
  • Teaching SQL. Students see the clause order clearly and spot a WHERE that should be a HAVING.
  • Logs. Queries copied from application logs arrive minified; formatting makes them readable again.

Common mistakes

  • Expecting a syntax check. A formatter lays out text; a misspelt column stays misspelt. Run the query to test it.
  • Changing the case of names. Quoted names can be case-sensitive. Keyword case is safe; name case is not.
  • Mixing styles in one project. Pick one comma and indent style for a team and stick to it.
  • Minifying code you will edit. Keep the formatted version in your files; minify only for transport.

Questions people ask

Is upper case faster for the database? No. Case of keywords makes no difference to speed or results.

Why do some teams use leading commas? Each line starting with a comma can be commented out without editing the line above.

Does formatting change what the query does? No. Only spaces, line breaks and keyword case change, and the database ignores all three.

Sources

  • fibub's own SQL tokenizer and layout rules, written for this page. It reads strings, quoted names (double quotes, backticks, [brackets]), comments, numbers, variables like @x, :x and $1, and the operators :: || -> and ->>. It lays out text only; it does not check that the SQL is valid for your database.

If you are an author or publisher and want a credit or a passage changed or removed, tell us and we will act quickly.

Tools in this article

Read next