SQL that works is not always SQL that anyone can read. A 600-character query on one line is hard to review, hard to debug and easy to break. Consistent formatting makes queries understandable at a glance – for your colleagues and for you six months later.
Page Contents
Before and after
Before: select p.name, c.title as category, p.price from products p inner join categories c on c.id = p.category_id where p.price > 20 and p.active = 1 order by p.price desc limit 10;
After (one clause per line):
SELECT p.name, c.title AS category, p.priceFROM products pINNER JOIN categories c ON c.id = p.category_idWHERE p.price > 20AND p.active = 1ORDER BY p.price DESCLIMIT 10;
The rules most teams follow
1. Uppercase keywords
SELECT, FROM, WHERE, JOIN in uppercase; table and column names in lowercase. The structure stands out from the data. (Some teams prefer all lowercase – consistency matters more than the choice.)
2. One major clause per line
Start each of SELECT, FROM, JOIN, WHERE, GROUP BY, HAVING, ORDER BY and LIMIT on a new line.
3. Indent conditions
Put each AND / OR condition on its own indented line. Use parentheses when you mix AND and OR – never rely on precedence.
4. Meaningful aliases
p for products and c for categories is fine in short queries; in long ones, use readable aliases like orders o and customers cust. Always use AS for column aliases.
5. List columns explicitly
Avoid SELECT * in production code – it fetches unneeded data and breaks when columns change.
6. Commas: leading or trailing
Trailing commas read naturally; leading commas make it easy to comment out a line. Pick one style.
7. Use CTEs for complex logic
WITH recent_orders AS (…) splits a complex query into named steps – far easier to read than nested subqueries.
8. Comment the why
-- Exclude test accounts created by QA explains intent that the code cannot.
Naming conventions
- Tables and columns in snake_case:
order_items,created_at– see text case types. - Be consistent with singular or plural table names.
- Avoid reserved words as names (
order,user,group).
Format automatically
Do not format by hand. Paste queries into the SQL Formatter, and in your projects use your editor’s formatter or a linter such as SQLFluff so every query follows the same style. To see what changed between two versions of a query, format both and compare them in the Diff Checker.
FAQ
Should SQL keywords be uppercase?
It is the most common convention because it separates keywords from names. Consistency matters most.
Does formatting affect performance?
No. Whitespace and capitalisation do not change how the database runs the query.
What is a CTE?
A common table expression – a named subquery defined with WITH that makes complex queries easier to read.
Why avoid SELECT *?
It fetches unnecessary columns and can break code when the table changes.
Is my SQL sent anywhere?
No. The formatter runs in your browser.
Need a website or app for your business? Talk to Techpass Master – we design and build websites and apps for clients worldwide.
