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?

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

StyleExamplePopularity
UPPERCASE keywordsSELECT name FROM usersMost common
lowercase keywordsselect name from usersGrowing trend
Title Case keywordsSelect name From usersRare

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 u is fine for simple queries, but customers cust is better than c in complex ones.
  • Always use AS: COUNT(*) AS total is clearer than COUNT(*) total.
  • Align aliases: When selecting multiple columns, align the AS keywords vertically for readability.

Common Formatting Patterns

  1. Comma placement: Put commas at the beginning of each line (leading commas) or end of each line β€” but never mix styles.
  2. Subquery indentation: Indent subqueries one level deeper than the parent query.
  3. CTE formatting: Each Common Table Expression (CTE) should be separated with a blank line.
  4. 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.

Related Guides

View all guides →