← Blog Dev

The Art of SQL Query Formatting

November 4, 2025 · 7 min read

I once spent forty-five minutes debugging a query that turned out to have a misplaced closing parenthesis buried in a wall of unformatted SQL. That experience changed the way I write queries forever. If you've ever stared at a five-hundred-line SQL statement and felt your eyes glaze over, this post is for you.

Why Formatting Matters More Than You Think

SQL doesn't care about whitespace. The database engine will happily chew through a query crammed into a single line. But you're not writing for the database alone. In my experience, most SQL is read far more often than it's written, especially in team environments where someone else will inevitably need to modify your query six months from now.

Poorly formatted SQL hides bugs. It makes code reviews painful. It turns a simple JOIN condition change into a twenty-minute archaeology expedition. Good formatting, on the other hand, makes the logic of a query visible at a glance.

The Basics: One Clause per Line

The single most impactful habit is putting each major clause on its own line. SELECT, FROM, WHERE, GROUP BY, ORDER BY, and HAVING should each start a new line at the same indentation level. I've found that this alone makes most queries twice as readable.

SELECT
    u.name,
    u.email,
    COUNT(o.id) AS order_count
FROM users u
JOIN orders o ON o.user_id = u.id
WHERE u.created_at >= '2025-01-01'
GROUP BY u.name, u.email
ORDER BY order_count DESC;

Compare that to the same query on a single line and the difference is night and day. Each clause is instantly identifiable, and you can scan the structure of the query without reading every word.

Formatting JOINs for Clarity

JOINs are where things start getting messy in real-world queries. I like to keep each JOIN on its own line and indent the ON condition slightly. When you have multiple JOIN conditions, putting each one on a separate line with AND at the start makes it obvious what's being matched.

FROM orders o
INNER JOIN users u
    ON u.id = o.user_id
    AND u.active = 1
LEFT JOIN products p
    ON p.id = o.product_id

Some teams prefer putting AND at the end of the previous line. That's fine too. What matters is that the team agrees on one style and sticks with it. Consistency beats personal preference every time.

Taming Subqueries

Subqueries deserve special attention because they're the number one source of formatting chaos. The approach that has worked best for me is indenting the entire subquery one level deeper than the surrounding code and keeping the opening parenthesis on the same line as the context.

When a subquery gets longer than about ten lines, it's usually a sign that you should consider extracting it into a CTE instead. More on that in a moment.

CTEs: Your Best Friend for Complex Queries

Common Table Expressions changed the way I think about complex SQL. Instead of nesting subqueries three levels deep, you can break the logic into named steps that read almost like pseudocode. I've found that even people who aren't SQL experts can follow a well-written CTE chain.

WITH active_users AS (
    SELECT id, name, email
    FROM users
    WHERE active = 1
),
recent_orders AS (
    SELECT user_id, COUNT(*) AS cnt
    FROM orders
    WHERE created_at >= '2025-01-01'
    GROUP BY user_id
)
SELECT
    au.name,
    au.email,
    ro.cnt AS order_count
FROM active_users au
JOIN recent_orders ro ON ro.user_id = au.id
ORDER BY ro.cnt DESC;

Each CTE has a descriptive name that tells you what it does. The final SELECT then reads like a simple join between two well-defined datasets. No nesting, no confusion.

Team Style Consistency

The hardest part of SQL formatting isn't learning the rules. It's getting everyone on the team to follow the same ones. I've been on teams where three different developers had three different indentation styles, and merging changes was a nightmare.

My recommendation is to pick an automated formatter and integrate it into your workflow. Whether you use an online tool or a CLI-based solution, having a shared configuration file that everyone runs before committing eliminates formatting debates entirely. The best style guide is one that's enforced automatically.

A few conventions worth agreeing on upfront: uppercase versus lowercase keywords, comma-first versus comma-last in SELECT lists, and whether to alias tables with meaningful abbreviations or single letters. Get these settled once and save your team hours of bikeshedding.

Wrapping Up

Clean SQL formatting is one of those skills that pays compound interest over time. Every query you format well is a query that's easier to debug, review, and maintain later. Start with the basics - one clause per line, consistent indentation, descriptive CTE names - and you'll be surprised how much smoother your database work becomes.