Developer tools

How to format SQL for readability

Learn the formatting rules that make SQL reviewable: clause placement, indentation, casing, and using a formatter consistently.

9 min read Reviewed August 4, 2026 Professional reference

Document summary

A guide to readable SQL: consistent casing, clause placement, indentation, and when to use the formatter.

Key takeaways

  • One clause per line makes queries skimmable
  • Consistent casing turns keywords into landmarks
  • Format before review, review before running
01

The core formatting rules

  • Start each main clause (SELECT, FROM, JOIN, WHERE, GROUP BY, ORDER BY) on its own line
  • Indent subqueries and JOIN conditions by one level
  • Keep keywords uppercase and identifiers lowercase for consistency
  • Align commas at the start of continuation lines
02

Before and after

The same query reads very differently before and after formatting. The formatted version shows the clause structure at a glance.

Invalid
SELECT u.name,o.total FROM users u JOIN orders o ON u.id=o.user_id WHERE o.status='paid' ORDER BY o.total DESC LIMIT 10;
Valid
SELECT u.name,
       o.total
FROM users u
JOIN orders o
  ON u.id = o.user_id
WHERE o.status = 'paid'
ORDER BY o.total DESC
LIMIT 10;
03

Use the formatter for consistency

  1. 1

    Paste the query into the SQL formatter.

  2. 2

    Set the indentation and casing options to your team standard.

  3. 3

    Format and copy the result into the review.

  4. 4

    Re-run the formatter after any edit so the style never drifts.

04

When formatting is not enough

Formatting reveals structure but does not fix design. Long chains of subqueries or OR conditions are often better rewritten, and indexes matter more than whitespace for performance.

05

Frequently asked questions

Does formatting change what my query returns?

No. Whitespace and casing do not affect query semantics.

Which casing should I use?

Uppercase keywords with lowercase identifiers is the most common convention, but consistency matters more than the choice.

Can the formatter handle complex nested queries?

Yes. Subqueries and CTEs are indented by the formatter, which makes nesting visible.

Is my SQL uploaded?

No. Formatting runs locally in your browser.

Jump to tool

Format your SQL

Open tool