How to Format SQL So Your Team Can Read It
6 min read · Published 1 November 2026 · Updated 2 November 2026
SQL that works is not always SQL that can be maintained. A long query squeezed into one line is difficult to review, debug and change safely. Consistent formatting does not make a query faster, but it makes mistakes visible, and that is worth a lot. These are the conventions most teams settle on, and why.
In this guide
- Before and after
- Formatted
- Uppercase keywords, lowercase names
- One clause per line, one column per line
- Indent conditions and subqueries
- Be explicit with joins and aliases
- A subtle bug that formatting can reveal
- Formatting CASE expressions
- Agree on a style and automate it
- Leading or trailing commas?
- Use common table expressions for multi-step logic
- Naming and small habits that help
Before and after
The two queries below are identical for the database. Only the layout is different.
Unformatted
select u.id,u.name,count(o.id) as orders from users u left join orders o on o.user_id=u.id where u.active=1 and o.created_at>='2026-01-01' group by u.id,u.name having count(o.id)>2 order by orders descFormatted
You can now see at a glance which tables are used, how they are joined, which filters apply and how the result is grouped and sorted.
Formatted
SELECT
u.id,
u.name,
COUNT(o.id) AS orders
FROM users u
LEFT JOIN orders o ON o.user_id = u.id
WHERE u.active = 1
AND o.created_at >= '2026-01-01'
GROUP BY u.id, u.name
HAVING COUNT(o.id) > 2
ORDER BY orders DESCUppercase keywords, lowercase names
Writing keywords such as SELECT, FROM, JOIN and WHERE in capitals and table and column names in lowercase lets the eye separate the structure of the query from the data it touches. Pick one rule and apply it everywhere; a formatter can do this automatically.
One clause per line, one column per line
Start each major clause (SELECT, FROM, WHERE, GROUP BY, HAVING, ORDER BY) on its own line, and list selected columns one per line. Version control then shows a clean one-line difference when someone adds a column, instead of marking an entire long line as changed.
Indent conditions and subqueries
Indent continuation lines such as additional AND and OR conditions, and indent subqueries one level deeper than the line that contains them. Put parentheses so the opening and closing ones line up, which makes a missing bracket easy to find. For queries that nest more than one level, consider common table expressions (WITH name AS (...)). Naming each step usually reads better than a deeply nested subquery.
Be explicit with joins and aliases
- Use explicit JOIN ... ON syntax rather than listing tables separated by commas and putting join conditions in WHERE.
- Give tables short, meaningful aliases and use them for every column, so it is clear which table each column comes from.
- Avoid SELECT * in production queries. Listing the columns you need makes the query self-documenting and protects it from schema changes.
A subtle bug that formatting can reveal
In the example above, the query uses a LEFT JOIN but then filters the right-hand table in the WHERE clause (o.created_at >= ...). Rows with no matching order have a NULL date, so they are removed and the LEFT JOIN behaves like an INNER JOIN. If you want to keep users without orders, move that condition into the ON clause. Clear layout makes this kind of mistake much easier to notice during review.
Formatting CASE expressions
Putting each WHEN on its own line, with END aligned to CASE, makes the logic read like a table.
CASE layout
CASE
WHEN score >= 90 THEN 'A'
WHEN score >= 80 THEN 'B'
ELSE 'C'
END AS gradeAgree on a style and automate it
The exact choices matter less than applying them consistently. Decide on keyword case, indentation width and comma placement, write it down, and run a formatter before committing. Formatting only changes whitespace and keyword case, so it does not alter what a query returns or how fast it runs.
Leading or trailing commas?
Teams argue about where commas belong in a column list. Trailing commas (at the end of each line) look natural and match everyday writing. Leading commas (at the start of each line) make it easy to comment out or add the last column without touching the line above it, and they make a missing comma obvious. Either is fine. What matters is choosing one, writing it down and letting a formatter apply it.
Use common table expressions for multi-step logic
When a query needs several steps, a common table expression (CTE) lets you name each one and read the query from top to bottom, instead of from the innermost subquery outwards:
CTE example
WITH recent_orders AS (
SELECT user_id, COUNT(*) AS orders
FROM orders
WHERE created_at >= '2026-01-01'
GROUP BY user_id
)
SELECT u.name, r.orders
FROM users u
JOIN recent_orders r ON r.user_id = u.id
WHERE r.orders > 2
ORDER BY r.orders DESCNaming and small habits that help
- Use clear, consistent names: singular or plural for tables, but not both.
- Alias aggregated columns (COUNT(*) AS orders) so that results are self-explanatory.
- Put each condition of a long WHERE clause on its own line and group OR conditions in parentheses to show your intent.
- Add a short comment above a non-obvious condition explaining why it is there. Comments are for the reason, not the syntax.
Frequently asked questions
- Does formatting change performance?
- No. Whitespace and keyword case are ignored by the database when it plans and runs the query. Performance depends on the query logic, indexes and data, not on layout.
- Do SQL keywords have to be uppercase?
- No, keywords are case-insensitive. Uppercase is a readability convention. Be careful with identifiers: depending on the database and on whether they are quoted, table and column names can be case-sensitive.
- Tabs or spaces for indentation?
- Either works. Many teams use four spaces, because tabs display at different widths in different tools. Consistency matters more than the choice.
- Should I format queries by hand or automatically?
- Automatically. A formatter or a pre-commit hook keeps style consistent without arguments and makes diffs smaller, because only real changes show up.
Sources and further reading
Try it yourself: SQL Formatter runs in your browser, free and with no sign-up.