DEVESSENTIALS

SQL Formatting Best Practices: Write SQL That's Easy to Read

By · Updated

SQL that works and SQL that's readable are two different things. Consistent formatting makes queries easier to review in PRs, debug at 2am, and hand off to colleagues. Here are the conventions professionals use.

The short version: uppercase keywords, one clause per line, one column or condition per line, explicit JOIN ... ON instead of comma joins, short table aliases on every column once more than one table is involved, CTEs instead of deep nesting, and a formatter in CI so nobody argues about it in review. The worked example below applies all of these to a real query, and the last sections show bugs that good formatting makes easy to spot.

To apply these conventions automatically, run your query through the SQL formatter. It supports MySQL, PostgreSQL, SQLite and T-SQL.

Keyword Casing

Use uppercase for SQL keywords. This visually distinguishes the SQL language constructs from your table and column names (see naming conventions for how to name those):

-- Good
SELECT
    user_id,
    email,
    created_at
FROM users
WHERE status = 'active'
  AND created_at > '2026-01-01'
ORDER BY created_at DESC;

-- Bad (hard to scan)
select user_id, email, created_at from users where status = 'active'

One Clause Per Line

Each major SQL clause (SELECT, FROM, WHERE, GROUP BY, ORDER BY, LIMIT) starts on its own line. Column lists are indented under SELECT:

SELECT
    o.order_id,
    o.total_amount,
    u.email,
    u.name
FROM orders o
INNER JOIN users u ON u.user_id = o.user_id
WHERE o.status = 'completed'
  AND o.created_at >= CURRENT_DATE - INTERVAL '30 days'
ORDER BY o.total_amount DESC
LIMIT 100;

This layout means you can add, remove, or comment out any column or condition without touching other lines — diffs are clean.

Commas: Leading vs Trailing

Two schools of thought. Leading commas keep errors visible:

-- Leading commas (errors are obvious — a line without comma is wrong)
SELECT
    user_id
  , email
  , created_at

-- Trailing commas (more natural to read)
SELECT
    user_id,
    email,
    created_at

Most style guides prefer trailing commas for readability. Pick one and be consistent; a formatter keeps it that way without review comments. The DevEssentials SQL Formatter puts commas at the end of the line.

JOINs

Always use explicit JOINs with ON conditions. Never use implicit joins (comma-separated tables):

-- Good: explicit JOIN, relationship is clear
SELECT
    o.order_id,
    p.product_name
FROM orders o
INNER JOIN order_items oi ON oi.order_id = o.order_id
INNER JOIN products p    ON p.product_id = oi.product_id;

-- Bad: implicit join, easy to accidentally create a cartesian product
SELECT o.order_id, p.product_name
FROM orders o, order_items oi, products p
WHERE oi.order_id = o.order_id
  AND p.product_id = oi.product_id;

Align the ON keyword with the JOIN keyword — some teams go further and align all ON clauses to the same column.

Aliases

Use short, consistent aliases for tables. Initials or abbreviated names work well:

FROM orders o
INNER JOIN users u        ON u.user_id = o.user_id
INNER JOIN order_items oi ON oi.order_id = o.order_id

Always qualify column names with the table alias when joining multiple tables. Never leave ambiguous column references that break when a new table is joined.

WHERE Conditions

Align AND and OR operators at the start of each condition. Use parentheses to make complex logic explicit:

WHERE o.status = 'active'
  AND (
        o.amount > 1000
     OR o.priority = 'high'
      )
  AND o.created_at > '2026-01-01'

Subqueries and CTEs

Prefer CTEs (Common Table Expressions) over nested subqueries — they're named, readable, and easier to debug:

-- Subquery (hard to read at a glance)
SELECT user_id, total
FROM (
    SELECT user_id, SUM(amount) AS total
    FROM orders
    WHERE status = 'completed'
    GROUP BY user_id
) AS order_totals
WHERE total > 500;

-- CTE (clear intent, easy to test each step)
WITH order_totals AS (
    SELECT
        user_id,
        SUM(amount) AS total
    FROM orders
    WHERE status = 'completed'
    GROUP BY user_id
)
SELECT user_id, total
FROM order_totals
WHERE total > 500;

Chain multiple CTEs for complex queries — each CTE builds on the previous one, making the data transformation steps explicit.

CASE Expressions and Window Functions

Give each WHEN its own line, indented under CASE, and close with an aligned END plus an alias. For window functions, break the OVER clause when it has both a partition and an order:

SELECT
    o.order_id,
    CASE
        WHEN o.total_amount >= 1000 THEN 'large'
        WHEN o.total_amount >= 100  THEN 'medium'
        ELSE 'small'
    END AS order_size,
    ROW_NUMBER() OVER (
        PARTITION BY o.user_id
        ORDER BY o.created_at DESC
    ) AS recency_rank
FROM orders o;

Worked Example: Before and After

A query as it often arrives in a bug report or an ORM log:

select u.id,u.email,count(o.id) as orders,sum(o.total) as revenue from users u left join orders o on o.user_id=u.id where o.status='paid' and u.created_at>='2026-01-01' or u.vip=true group by u.id,u.email having count(o.id)>3 order by revenue desc

The same query formatted with the conventions above:

SELECT
    u.id,
    u.email,
    COUNT(o.id)  AS orders,
    SUM(o.total) AS revenue
FROM users u
LEFT JOIN orders o ON o.user_id = u.id
WHERE o.status = 'paid'
  AND u.created_at >= '2026-01-01'
   OR u.vip = TRUE
GROUP BY
    u.id,
    u.email
HAVING COUNT(o.id) > 3
ORDER BY revenue DESC;

Laid out this way, two bugs that were invisible on one line stand out. The OR sits at a different level from the AND it was meant to group with, and the LEFT JOIN is followed by a WHERE filter on orders. Both are explained in the next section.

Bugs That Formatting Makes Visible

AND binds tighter than OR

a AND b OR c means (a AND b) OR c. In the example, every VIP user matches regardless of order status or signup date. If the intent was "paid orders from users who are recent or VIP", say so with parentheses:

WHERE o.status = 'paid'
  AND (
        u.created_at >= '2026-01-01'
     OR u.vip = TRUE
      )

A WHERE filter on the right side of a LEFT JOIN

Users without orders get NULL in every o.* column, and o.status = 'paid' is never true for NULL, so those users disappear. The LEFT JOIN silently behaves like an INNER JOIN. To keep users with no paid orders, move the condition into the join:

FROM users u
LEFT JOIN orders o
       ON o.user_id = u.id
      AND o.status = 'paid'

NOT IN with a NULL in the list

-- Returns no rows at all if any banned_users.user_id is NULL
SELECT id FROM users
WHERE id NOT IN (SELECT user_id FROM banned_users);

-- Safe
SELECT u.id FROM users u
WHERE NOT EXISTS (
    SELECT 1 FROM banned_users b WHERE b.user_id = u.id
);

x NOT IN (1, NULL) expands to x <> 1 AND x <> NULL, and the second comparison is never true. NOT EXISTS has no such trap.

BETWEEN on timestamps

created_at BETWEEN '2026-10-01' AND '2026-10-31' stops at midnight at the start of October 31, dropping almost the whole last day. Use a half-open range instead: created_at >= '2026-10-01' AND created_at < '2026-11-01'.

NULL Handling

Use IS NULL and IS NOT NULL, never = NULL. Any comparison with NULL evaluates to NULL (unknown), not true, so the row is never selected:

-- Correct
WHERE deleted_at IS NULL
WHERE email IS NOT NULL

-- Always wrong: NULL = NULL is unknown, so this matches nothing
WHERE deleted_at = NULL

Comments

Use inline comments to explain non-obvious business logic, not what the SQL syntax does:

-- Good: explains business rule
WHERE created_at > CURRENT_DATE - INTERVAL '90 days'  -- rolling 90-day window per billing policy

-- Useless: explains the syntax
WHERE status = 'active'  -- filter by status

Automate It

Don't rely on manual formatting. Use a formatter in your workflow:

  • Online: DevEssentials SQL Formatter supports MySQL, PostgreSQL, SQLite, T-SQL, BigQuery, MariaDB
  • CLI: sql-formatter (npm), sqlfluff (Python) for CI/CD integration
  • IDE: DataGrip, DBeaver, VS Code SQL extensions all have built-in formatters
  • Pre-commit hooks: run sqlfluff lint before every commit

A minimal SQLFluff configuration that enforces uppercase keywords for PostgreSQL:

# .sqlfluff
[sqlfluff]
dialect = postgres

[sqlfluff:rules:capitalisation.keywords]
capitalisation_policy = upper

sqlfluff lint queries/ reports violations and sqlfluff fix queries/ rewrites the files, so the same command works locally and in CI.


Format your SQL now with the SQL Formatter — supports MySQL, PostgreSQL, SQLite, T-SQL, BigQuery and MariaDB with configurable indentation.

Frequently Asked Questions

Does SQL formatting affect performance?▾

No. SQL formatting is purely cosmetic — whitespace, newlines, and keyword casing have zero effect on query execution. The database parser strips all of that before parsing. Formatting affects the humans reading the query, not the database executing it.

Should SQL keywords be uppercase or lowercase?▾

Uppercase keywords (SELECT, FROM, WHERE) are the traditional convention and are used in most style guides (SQLStyle.guide, GitLab, Kickstarter). The main argument: it visually separates SQL keywords from table/column names, making queries easier to scan. Some modern teams prefer lowercase for less visual noise. Either is fine — the key is consistency within a codebase.

Should I use SELECT * or list columns explicitly?▾

Always list columns explicitly in production code. SELECT * returns all columns including ones you may not need, increases network payload, breaks if columns are added/removed/reordered, and makes it impossible to understand what data the query actually uses without running it. The only acceptable use of SELECT * is in ad-hoc exploratory queries, never in application code.

Where should I put JOIN conditions vs WHERE conditions?▾

JOIN conditions (how tables relate) go in the ON clause. Filter conditions (which rows you want) go in the WHERE clause. Mixing them with implicit joins (comma-separated tables with all conditions in WHERE) makes queries hard to read and is a common source of accidental cartesian products. Explicit JOINs with ON conditions are unambiguous about intent.

Browse all developer guides →