How to Format SQL Queries

How to Format SQL Queries

SQL written in a hurry looks like a run-on sentence: SELECT id,name,SUM(amount) FROM orders o JOIN customers c ON o.cid=c.id WHERE o.status='paid' GROUP BY id,name HAVING SUM(amount)>100 ORDER BY 3 DESC. It works. It’s also unreadable.

Why SQL formatting matters

  • Bugs hide in long lines — a missing join condition or an OR that should be an AND is nearly invisible in a wall of text and obvious in formatted code.
  • Reviewers read it — pull requests with formatted SQL get reviewed; unformatted SQL gets rubber-stamped.
  • Consistent style — when every query follows the same layout, your team spends time on logic, not decoding indentation.

SQL has no official style standard, but the de-facto convention puts each clause (SELECT, FROM, WHERE) on its own line, one column per line in the select list, and indents subqueries and joins consistently.

Formatting in your browser

  1. Paste the query into the SQL formatter.
  2. It re-indents the query into the standard layout: clauses on separate lines, commas leading or trailing per style, subqueries nested.
  3. Copy the formatted result back to your editor.

No upload, no account — the transformation happens locally.

The habits that prevent the mess

  • Format as you go: write SQL with line breaks from the first draft, and formatting stays a non-task.
  • Uppercase keywords: SELECT/FROM/WHERE in caps and table/column names in lower case makes structure pop at a glance.
  • Name joins explicitly: INNER JOIN ... ON ... reads better than the old FROM a, b WHERE a.x = b.x syntax — and formats better too.

A query you can read in one pass is a query you can debug in one pass. The formatter is a two-second fix for everything you wrote before adopting the habit.