SQL Formatting Guide

Written by Ensar Karayel. Last updated May 11, 2026.

Readable SQL helps more than style discussions. It makes joins, filters and grouping decisions visible during code review. A query written on one line may run correctly, but it hides the exact clause that changes result count or performance.

Separate Clauses First

Start by placing major clauses on separate lines: SELECT, FROM, JOIN, WHERE, GROUP BY, HAVING, ORDER BY and LIMIT. This gives reviewers a reliable scan path from the selected fields to the source tables and filters.

SELECT c.customer_id,
       c.name,
       SUM(o.total_amount) AS revenue
FROM customers c
LEFT JOIN orders o ON o.customer_id = c.customer_id
WHERE o.status = 'paid'
GROUP BY c.customer_id, c.name
ORDER BY revenue DESC;

Make Joins Reviewable

Every join should make the relationship obvious. Put each JOIN on its own line and keep the ON condition next to it. If a query produces duplicate rows, the join section is usually the first place to inspect.

Filters Deserve Care

WHERE clauses often encode business rules. Split AND and OR conditions so that date ranges, statuses and null checks are visible. Be careful with OR conditions mixed with AND conditions; parentheses may be required to express the intended logic.

Formatting does not prove a query is safe. Always review UPDATE, DELETE, DROP, TRUNCATE and ALTER statements before executing them.

Use Formalint

Use the Formalint SQL Formatter to format, compact and run basic checks on query snippets. For data conversion next to SQL work, try CSV to JSON.