Developer Tools

How to Format SQL Queries for Readability

Practical SQL formatting rules covering indentation, keyword casing, aligned joins, CTEs and subqueries, with a clear before-and-after example.

3 min read·Updated 2026-07-25

A query written as one long line is nearly impossible to review, debug or reuse. Consistent formatting turns SQL into something a teammate can understand in seconds and a reviewer can diff properly. This guide sets out conventions that work for day-to-day analytics and application SQL, with a worked example showing the transformation.

Indentation conventions

Indent each major clause one level, and each column or condition another level inside it. The rule that makes the biggest difference: one column per line.

SELECT
u.id,
u.full_name,
SUM(o.total) AS lifetime_value

Two spaces per level is the most portable choice — it survives copy-paste into chat tools and terminals that render tabs inconsistently. Never mix tabs and spaces in the same query.

Keyword casing

Pick one style and keep it: uppercase for language keywords, lowercase (or original case) for identifiers.

ElementStyleExample
KeywordsUPPERSELECT, FROM, WHERE, JOIN
FunctionsUPPERCOALESCE(x, 0), DATE_TRUNC('day', ts)
Tables and columnslower_snakeorders.created_at
Aliasesshort, lowercaseo, u, s
String literalsas written'2026-01-01'

Uppercase keywords is the dominant convention; the important thing is that select and SELECT do not appear in the same file.

Aligning joins and their conditions

Put each join on its own line and indent the ON clause beneath it so the matched pair is visually obvious:

FROM orders o
LEFT JOIN customers c
ON c.id = o.customer_id
LEFT JOIN products p
ON p.id = o.product_id

When several conditions belong to one join, align them under the first and repeat AND at line starts — never bury a second condition at the end of the previous line.

CTEs and subqueries

Prefer CTEs (WITH clauses) over nested subqueries; they read top-down and each step names its intent:

  1. Start with WITH and one CTE per transformation.
  2. Give every CTE a name describing what it contains, such as monthly_revenue.
  3. Indent each CTE body one level and separate them with commas on their own lines.
  4. Keep the final SELECT at the outer indentation level — it should be the first thing a reader can run.
  5. Reserve subqueries for single-value lookups inside WHERE, where they are genuinely clearer.

Before and after

select u.id,u.name,o.total from users u inner join orders o on o.user_id=u.id where o.created_at>'2026-01-01' order by o.total desc;

becomes:

SELECT
u.id,
u.name,
o.total
FROM users u
INNER JOIN orders o
ON o.user_id = u.id
WHERE o.created_at > '2026-01-01'
ORDER BY o.total DESC;

Same rows, same plan — a query anyone can scan. Paste yours into the SQL Formatter & Beautifier to get this automatically, and use the Code Beautifier (HTML, CSS, JS) when the surrounding application code needs the same treatment.

Frequently asked questions

Does formatting change query performance? No. Whitespace and casing are stripped by the parser before planning; only the logical structure affects the plan.

Should I format queries inside application code? Yes — multi-line template strings in your ORM or data layer read far better formatted. Keep parameters on separate lines.

Is there an official standard? No SQL standard for style exists. Follow your team's documented convention, or let a formatter enforce one automatically in code review.

Tools used in this guide

Related guides