SQL Formatting Best Practices for Clean Code
Learn SQL formatting best practices to write clean, readable database queries. Covers keyword casing, indentation, aliasing, and common style conventions.
Table of Contents
- Why Format SQL?
- Keyword Casing
- Indentation and Line Breaks
- Aliasing Conventions
- Common Formatting Patterns
Why Format SQL?
Unformatted SQL is notoriously hard to read. A single complex query can span dozens of joins, subqueries, and conditions. Without proper formatting, debugging becomes a nightmare and code reviews slow to a crawl.
Well-formatted SQL is self-documenting. When each clause is on its own line and indentation shows structure, you can understand a query's logic in seconds rather than minutes.
Keyword Casing
| Style | Example | Popularity |
|---|---|---|
| UPPERCASE keywords | SELECT name FROM users | Most common |
| lowercase keywords | select name from users | Growing trend |
| Title Case keywords | Select name From users | Rare |
The uppercase convention is the most widely adopted because it clearly separates SQL keywords from table and column names. Whatever you choose, be consistent across your project.
Indentation and Line Breaks
The golden rule: each major SQL clause goes on its own line. Here is a well-formatted example:
SELECT
u.name,
u.email,
COUNT(o.id) AS order_count
FROM users u
INNER JOIN orders o ON u.id = o.user_id
WHERE u.created_at >= '2026-01-01'
GROUP BY u.name, u.email
HAVING COUNT(o.id) > 5
ORDER BY order_count DESC
LIMIT 10;
Aliasing Conventions
- Use meaningful aliases:
users uis fine for simple queries, butcustomers custis better thancin complex ones. - Always use AS:
COUNT(*) AS totalis clearer thanCOUNT(*) total. - Align aliases: When selecting multiple columns, align the AS keywords vertically for readability.
Common Formatting Patterns
- Comma placement: Put commas at the beginning of each line (leading commas) or end of each line β but never mix styles.
- Subquery indentation: Indent subqueries one level deeper than the parent query.
- CTE formatting: Each Common Table Expression (CTE) should be separated with a blank line.
- Trailing semicolons: Always end queries with a semicolon for clarity and portability.
Try it now: Open the SQL Formatter →
Paste your SQL to instantly format it with consistent indentation and keyword casing.
Frequently Asked Questions
Should SQL keywords be uppercase or lowercase?
The most common convention is to write SQL keywords (SELECT, FROM, WHERE, JOIN) in UPPERCASE and identifiers (table names, column names) in lowercase. This makes it easy to distinguish keywords from data references at a glance.
How should I format long SQL queries?
Break long SQL queries into multiple lines with each major clause (SELECT, FROM, WHERE, GROUP BY, ORDER BY) on its own line. Indent subqueries and conditions. Put each column in a SELECT on its own line when there are many.
What is the best SQL style guide?
There is no single official SQL style guide, but popular ones include the dbt SQL style guide, the Mozilla SQL style guide, and the SQLStyle.guide community project. The key is consistency β pick a style and stick with it across your codebase.