A Readable SQL Style, Before and After
A one-line query with joins, GROUP BY and HAVING, formatted, plus conventions to agree on.
By the TurantTools Dev team · Updated October 27, 2026 · 2 min read
SQL works the same however it's laid out, but a consistent style makes review and debugging much faster. This is the layout our formatter uses: keywords in capitals, one clause per line, one column per line.
Before
select o.id, c.name, sum(i.qty*i.price) as total from orders o join customers c on c.id=o.customer_id left join items i on i.order_id=o.id where o.created_at>='2026-01-01' and o.status='paid' group by o.id, c.name having sum(i.qty*i.price)>100 order by total desc limit 20
After
SELECT o.id, c.name, SUM(i.qty * i.price) AS total FROM orders o JOIN customers c ON c.id = o.customer_id LEFT JOIN items i ON i.order_id = o.id WHERE o.created_at >= '2026-01-01' AND o.status = 'paid' GROUP BY o.id, c.name HAVING SUM(i.qty * i.price) > 100 ORDER BY total DESC LIMIT 20
Conventions worth agreeing on
- Short, meaningful table aliases (
ofor orders) and always prefix columns once there's a join. - Explicit
JOIN ... ON, never commas inFROM. - Leading or trailing commas: pick one for the team and let the formatter enforce it.