A single-line SQL query with no spacing or indentation might run fine, but it's painful to read, debug, or review. When a query spans dozens of lines with multiple JOINs and a subquery or two, formatting makes the difference between "I see it" and "I'll figure it out tomorrow."
This guide covers what SQL formatting means, practical conventions to follow, and how to auto-format in seconds.
What is SQL Formatting?
SQL formatting (or SQL beautifying) is the practice of adding consistent indentation, line breaks, and keyword capitalization to queries to make them easier to read. The query's behavior doesn't change — it's purely for human comprehension.
Here's the difference in practice:
-- Unformatted SELECT u.id,u.name,o.total FROM users u JOIN orders o ON u.id=o.user_id WHERE o.total>1000 ORDER BY o.total DESC LIMIT 10;
-- Formatted SELECT u.id, u.name, o.total FROM users u JOIN orders o ON u.id = o.user_id WHERE o.total > 1000 ORDER BY o.total DESC LIMIT 10;
Same result. The second version makes the logic immediately clear.
SQL Formatting Conventions
Capitalize keywords
Write SQL keywords like SELECT, FROM, WHERE, and JOIN in uppercase. Table names and column names stay lowercase. This visual contrast makes the structure scannable at a glance.
-- Hard to scan select id, name from users where active = true; -- Easy to scan SELECT id, name FROM users WHERE active = TRUE;
One clause per line
Start each major clause on its own line: SELECT, FROM, WHERE, JOIN, GROUP BY, ORDER BY, LIMIT.
SELECT p.id, p.title, c.name AS category_name FROM products p JOIN categories c ON p.category_id = c.id WHERE p.price < 5000 AND p.stock > 0 ORDER BY p.created_at DESC;
Indent subqueries
Nested SELECTs should be indented one level deeper to visually separate them from the outer query:
SELECT u.name, recent_orders.total FROM users u JOIN ( SELECT user_id, SUM(amount) AS total FROM orders WHERE created_at >= '2026-01-01' GROUP BY user_id ) AS recent_orders ON u.id = recent_orders.user_id;
Leading vs trailing commas
Some teams put commas at the end of each line, others at the start of the next. Leading commas make it easier to spot a missing comma at the end of a column list — but both styles work fine. Just pick one and stick to it across the team.
-- Trailing comma (common default) SELECT u.id, u.name, u.email -- Leading comma (easier to add/remove lines) SELECT u.id , u.name , u.email
Frequently Asked Questions
- Does formatting change how the query runs?
- No. SQL ignores whitespace (spaces, tabs, newlines) when parsing. Formatting is purely for human readability. The database engine sees the same query regardless of how it's laid out.
- Can I format SQL generated by an ORM or a query logger?
- Yes. ORM-generated queries are often machine-produced and run together on a single line. Pasting them into a SQL formatter instantly makes them readable — which is useful during debugging when you want to understand what query is actually being sent to the database.
- Should I format SQL in code comments too?
- If the SQL is short and simple, a single line in a comment is fine. For anything with a JOIN or subquery, formatting it helps future readers understand what the query does without having to mentally parse it from scratch.
Summary
- SQL formatting adds indentation, line breaks, and uppercase keywords — the query result doesn't change
- Start each major clause on its own line: SELECT, FROM, JOIN, WHERE, ORDER BY
- Capitalize SQL keywords; keep table and column names lowercase
- Indent subqueries one level deeper to separate them from the outer query
Format your SQL instantly with these tools:
- SQL Formatter — paste any query and get it formatted in one click
- Text Diff Checker — compare before and after versions of a query
- JSON Formatter — clean up JSON from API responses alongside your queries