Formatting CTEs and JOINs for reviewability
Common table expressions are the biggest readability win in modern SQL: name each step of a transformation (WITH active_users AS (...), recent_orders AS (...)) instead of nesting subqueries three levels deep. Format each CTE’s body with full indentation, and keep the final SELECT short — reviewers then read your query as a pipeline. For JOINs, put each JOIN on its own line with its ON condition indented beneath it, and join on explicit column equality before adding filters; mixing join conditions into the WHERE clause works for inner joins but silently changes semantics for outer joins.
The keyword casing debate, settled practically
UPPERCASE keywords (SELECT, FROM, WHERE) with lowercase identifiers is the convention this formatter applies and the one most style guides recommend: keywords become visual anchors that structure the query at a glance, exactly like syntax highlighting that survives being pasted into an email, a ticket, or a log. The counter-argument — that highlighting makes casing redundant — fails everywhere SQL travels as plain text, which in practice is most places SQL travels.
One query, one shape: team standardization
Formatting pays off most when it’s uniform across a codebase: diffs shrink to real changes, review comments stop relitigating style, and copy-pasted query fragments compose cleanly. Use this tool for ad-hoc work and one-off cleanups; for repositories of SQL (dbt projects, migration folders), add an automated formatter/linter such as sqlfluff or pgFormatter to CI so the style is enforced, not requested. Adopt the formatter’s defaults wherever possible — custom configurations are maintenance debt.
Formatting as a debugging instrument
A query that returns wrong results often confesses under formatting: the OR that isn’t parenthesized, the join condition hiding in WHERE, the correlated subquery referencing the wrong alias. Reformatting forces every clause onto its own line where these mistakes become visible. It’s the SQL equivalent of rubber-duck debugging — and it’s why formatting belongs before asking a colleague to look at the query.