How to Format SQL So Anyone Can Read It
Developer · Published October 17, 2026 · Updated October 24, 2026 · 3 min read
Written by Akinrinmade Michael
SQL queries often start small and grow into long, tangled statements. Formatting doesn't change what a query does, but it makes mistakes far easier to spot and reviews far faster. A query that reads like a short document gets fixed in minutes, and the same query on one line can take an afternoon.
Before and after
Here's a query as it often arrives, pasted from a log:
select o.id,c.name,sum(i.price*i.quantity) as total from orders o join customers c on c.id=o.customer_id join items i on i.order_id=o.id where o.status='paid' group by o.id,c.name order by total desc;
And here it is laid out so anyone can follow it:
SELECT o.id, c.name, SUM(i.price * i.quantity) AS total FROM orders AS o JOIN customers AS c ON c.id = o.customer_id JOIN items AS i ON i.order_id = o.id WHERE o.status = 'paid' GROUP BY o.id, c.name ORDER BY total DESC;
A readable layout
- Put each major clause (SELECT, FROM, WHERE, GROUP BY, ORDER BY) on its own line.
- Write keywords in capitals so they stand out from table and column names.
- Indent the list of columns and the join conditions.
- Give tables short but meaningful aliases and use them for every column.
- Pick one comma style, either at the end of lines or at the start, and keep to it.
- Put spaces around operators:
price * quantity, notprice*quantity.
Habits that prevent bugs
- Name the columns you need instead of using SELECT *, which breaks when a table changes and fetches data you don't use.
- Break a long query into named steps with common table expressions (WITH).
- Put a condition on every join, so you don't multiply rows by accident.
- Use brackets in conditions that mix AND with OR.
a AND b OR cisn't what many people expect. - Write NULL checks as
IS NULLorIS NOT NULL, since= NULLnever matches.
Taming a long query with CTEs
A common table expression gives a sub-result a name, so the final query reads like a sentence. Compare a nested sub-query with the same logic split into steps:
WITH paid_orders AS ( SELECT id, customer_id FROM orders WHERE status = 'paid' ), order_totals AS ( SELECT order_id, SUM(price * quantity) AS total FROM items GROUP BY order_id ) SELECT c.name, t.total FROM paid_orders AS p JOIN customers AS c ON c.id = p.customer_id JOIN order_totals AS t ON t.order_id = p.id ORDER BY t.total DESC;
Each step can be run and checked on its own, which is how you find where a wrong number first appears.
Comments that help
Use comments for the why, not the what. A line like -- exclude test accounts, they have the internal flag saves the next reader from deleting a condition they think is pointless. Two dashes comment out the rest of a line, so be careful with them when you shrink a query onto one line.
Quotes inside text
A single quote ends a text value in SQL. To include one inside the text, write it twice, so O'Brien becomes 'O''Brien'. If you're building SQL from other text, the String Escape tool handles quoting for SQL and many other languages. Even so, programs should pass values to the database as parameters instead of building queries from text, which prevents SQL injection.
A review checklist
- Format the query first, so you're reading its structure and not its spacing.
- Check every join has the right condition and that no table is joined twice by accident.
- Look at the WHERE clause for AND and OR precedence.
- Check GROUP BY includes every non-aggregated column.
- Run it on a small data set and compare the row count with what you expect.
Use the tool
Paste a query into the SQL Formatter, choose your database dialect, and it lays the query out consistently. It can also shrink a query to one line, leaving quoted text untouched, for places where you need it compact.