How to Format SQL for Readability: A Practical Style Guide

4 50
calendar_today agoschedule2 min read

SQL doesn't enforce any whitespace rules — the engine treats a one-liner and a 40-line query exactly the same. But your teammates and future self will thank you for the 40-line version.

Here's a concise SQL formatting guide that works across MySQL, PostgreSQL, SQLite, and SQL Server.

Core rules

1. Uppercase keywords, lowercase names

This single convention does the most work:

``sql

SELECT u.name, o.total

FROM users u

JOIN orders o ON u.id = o.user_id

WHERE o.status = 'completed'

ORDER BY o.total DESC;

`

SQL keywords stand out visually; your table and column names don't compete for attention.

2. One clause per line

Each major clause (SELECT, FROM, WHERE, JOIN, GROUP BY, HAVING, ORDER BY, LIMIT) starts on a new line at the left margin.

3. Indent continuation items

Items in a SELECT list or multiple WHERE conditions are indented one level:

<code>sql <p>SELECT</p> <p>u.name,</p> <p>u.email,</p> <p>o.total,</p> <p>o.created_at</p> <p>FROM users u</p> <p>WHERE</p> <p>o.status = 'completed'</p> <p>AND o.created_at > '2024-01-01';</p> </code>

4. Alias everything consistently

If you alias users as u, use u.column everywhere in that query. Mixing users.id and u.name in the same query causes unnecessary cognitive load.

5. Use CTEs for complex queries

Named CTEs (WITH ... AS (...)) replace nested subqueries with human-readable steps:

<code>sql <p>WITH active_users AS (</p> <p>SELECT id, name</p> <p>FROM users</p> <p>WHERE last_login > NOW() - INTERVAL '30 days'</p> <p>),</p> <p>their_orders AS (</p> <p>SELECT user_id, COUNT(*) AS order_count</p> <p>FROM orders</p> <p>GROUP BY user_id</p> <p>)</p> <p>SELECT u.name, t.order_count</p> <p>FROM active_users u</p> <p>JOIN their_orders t ON u.id = t.user_id</p> <p>ORDER BY t.order_count DESC;</p> </code>

Each CTE reads like a named step. The final SELECT is obvious.

Formatting inherited SQL

When you receive SQL from an ORM, export tool, or legacy codebase, it often arrives as one long string. The SQL Formatter on SnappyTools takes minified or unformatted SQL and outputs properly indented, keyword-uppercased SQL for MySQL, PostgreSQL, SQLite, or T-SQL. Paste in one query or a full migration file — it runs in your browser with no upload.

What to avoid

  • Mixed case keywords (Select, WHERE, from)
  • Comma-first style (debated — choose one and stick to it)
  • Deeply nested subqueries when a CTE would be clearer
  • Aliases that are cryptic abbreviations (u2, x, t3`)

SQL style is team infrastructure. Establish a convention in your team's contributing guide, enforce it with a formatter in CI, and you'll spend less time decoding queries and more time understanding what they do.

1.7k Points54 Badges4 50
71Posts
0Comments
SnappyTools builds free, fast, browser-based tools for developers, writers, and designers. No signup required, no data uploaded, no nonsense — just clean tools that work instantly in your browser. We cover the full developer workflow: JSON formatting, Base64 encoding, URL encoding, HTML entity encoding, CSS and HTML minification, Markdown conversion, UUID generation, and more. Plus writing tools like readability checkers, word counters, and keyword density analysis. New tools added every week b...
Build your own developer journey
Track progress. Share learning. Stay consistent.
🔥 Join developers growing publicly
Share your knowledge, build in public, and grow your developer presence with a global community.

More Posts

I’m a Senior Dev and I’ve Forgotten How to Think Without a Prompt

Karol Modelskiverified - Mar 19

How I Built a React Portfolio in 7 Days That Landed ₹1.2L in Freelance Work

Dharanidharan - Feb 9

The Sovereign Vault — A Comprehensive Guide to Protocol-Driven AI

Ken W. Algerverified - Jun 4

5 Things This Playwright SQL Fixture Does So You Don't Have To

vitalicset - Apr 13

Architecting a Local-First Hybrid RAG for Finance

Pocket Portfolio - Feb 25
chevron_left

Related Jobs

View all jobs →

Commenters (This Week)

1 comment
1 comment
1 comment

Contribute meaningful comments to climb the leaderboard and earn badges!