SQL Formatting Best Practices: Write Clean, Readable Queries

· 7 min read

Writing SQL that works is one thing; writing SQL that others can read, understand, and maintain is another. In collaborative environments, poorly formatted queries create confusion, hide bugs, and slow code reviews. This guide covers essential formatting practices that transform messy SQL into clean, professional code.

Why SQL Formatting Matters

SQL is often treated as "write once, run forever," but queries are read far more often than written. A complex report query might be written once but reviewed and debugged dozens of times. Consistent formatting reduces cognitive load, making it easier to spot logical errors, understand join relationships, and identify performance bottlenecks. Studies show developers spend 70% of their time reading code — formatted SQL cuts query comprehension time in half.

🛠️ Format your SQL instantly

SQL Formatter →

Indentation and Line Breaks

Each major clause (SELECT, FROM, WHERE, GROUP BY, ORDER BY) should start on a new line at the base indentation level. Column lists and conditions indent one level deeper. Use 2 or 4 spaces consistently — never tabs, as they render differently across tools.

Place each column on its own line for queries with more than three columns. This makes commenting out columns during debugging easy and shows the full column list in version control diffs. Each WHERE condition should occupy its own line with AND/OR at the beginning:

SELECT
    u.user_id,
    u.first_name,
    u.last_name,
    o.order_date,
    o.total_amount
FROM users u
INNER JOIN orders o
    ON u.user_id = o.user_id
WHERE o.order_date >= '2026-01-01'
    AND o.status = 'completed'
    AND u.country = 'US'
ORDER BY o.order_date DESC;

Keyword Casing

The most adopted convention is UPPERCASE for SQL keywords (SELECT, FROM, WHERE, JOIN) and lowercase for identifiers (table names, column names, aliases). This creates strong visual distinction between query structure and data elements. Some teams prefer all-lowercase for a softer style — the key rule is consistency. Our SQL Formatter can automatically apply your preferred casing.

Aliasing Best Practices

Use meaningful short aliases — "users" becomes "u", "order_items" becomes "oi". Always use explicit AS for column aliases. For derived columns, always provide descriptive names: SUM(oi.quantity * oi.unit_price) AS total_revenue is far clearer than an unnamed expression.

Subqueries and CTEs

Common Table Expressions (CTEs) are almost always preferable to nested subqueries. CTEs let you name intermediate results and reference them like tables, creating a top-down narrative:

WITH monthly_revenue AS (
    SELECT
        DATE_TRUNC('month', order_date) AS month,
        SUM(total_amount) AS revenue
    FROM orders
    WHERE status = 'completed'
    GROUP BY DATE_TRUNC('month', order_date)
),
revenue_growth AS (
    SELECT month, revenue,
        LAG(revenue) OVER (ORDER BY month) AS prev_revenue
    FROM monthly_revenue
)
SELECT month, revenue, prev_revenue,
    ROUND((revenue - prev_revenue) / NULLIF(prev_revenue, 0) * 100, 1) AS growth_pct
FROM revenue_growth ORDER BY month;

Key Takeaways

Related Tools

SQL Formatter JSON Formatter

Frequently Asked Questions

Should SQL keywords be uppercase or lowercase?

The most common convention is UPPERCASE for keywords and lowercase for identifiers. This creates clear visual distinction. The most important rule is consistency — choose one style and enforce it with automated formatters.

How many spaces for SQL indentation?

Either 2 or 4 spaces are standard. Avoid tabs as they render differently across tools. Choose based on team preference and stick with it.

When should I use CTEs vs subqueries?

Use CTEs when subqueries are nested more than one level deep, when you reference the same subquery multiple times, or when complex logic benefits from named intermediate steps.

Trailing or leading commas in SQL?

Leading commas make it easy to comment out the last column and produce cleaner diffs. Trailing commas are more traditional. Both are fine — be consistent.