SQL Formatting Is Not Optional: 5 Rules That Cover 90% of Cases

5 81
calendar_today agoschedule3 min read

Unformatted SQL is a time sink. You spend more time parsing what a query does than understanding whether it's correct. Here are 5 formatting rules that cover 90% of cases.

The Problem With Unformatted SQL

This is real SQL pulled from production code:

``sql

SELECT u.id,u.name,o.total,p.name as product FROM users u JOIN orders o ON u.id=o.user_id JOIN order_items oi ON o.id=oi.order_id JOIN products p ON oi.product_id=p.id WHERE u.created_at>'2025-01-01' AND o.status='completed' ORDER BY o.total DESC LIMIT 50

`

Questions you can't answer at a glance: How many tables are joined? What's the filtering condition?

Formatted:

<code>sql <p>SELECT</p> <p>u.id,</p> <p>u.name,</p> <p>o.total,</p> <p>p.name AS product</p> <p>FROM users u</p> <p>JOIN orders o ON u.id = o.user_id</p> <p>JOIN order_items oi ON o.id = oi.order_id</p> <p>JOIN products p ON oi.product_id = p.id</p> <p>WHERE</p> <p>u.created_at > '2025-01-01'</p> <p>AND o.status = 'completed'</p> <p>ORDER BY o.total DESC</p> <p>LIMIT 50;</p> </code>

Now you can see the structure immediately: 4 tables, 2 filters, ordered by total, capped at 50 rows.

Rule 1: One Clause Per Line

`sql
-- Wrong


SELECT id, name FROM users WHERE active = 1 ORDER BY name;

-- Right

SELECT id, name

FROM users

WHERE active = 1

ORDER BY name;

`

Rule 2: Indent ON Conditions

<code>sql <p>JOIN orders o</p> <p>ON u.id = o.user_id</p> </code>

Rule 3: Uppercase Reserved Words

`sql
-- Wrong


select * from users where id = 1

-- Right

SELECT * FROM users WHERE id = 1

`

Rule 4: One Column Per Line for Wide SELECTs

<code>sql <p>SELECT</p> <p>first_name,</p> <p>last_name,</p> <p>email,</p> <p>created_at</p> <p>FROM users;</p> </code>

Rule 5: Align WHERE Conditions

<code>sql <p>WHERE</p> <p>status = 'active'</p> <p>AND created_at > NOW() - INTERVAL 30 DAY</p> <p>AND role IN ('admin', 'editor')</p> </code>

SQL in Application Code

Python (multi-line strings)
<code>python <p>query = """</p> <p>SELECT</p> <p>u.id,</p> <p>u.email,</p> <p>COUNT(o.id) AS order_count</p> <p>FROM users u</p> <p>LEFT JOIN orders o ON u.id = o.user_id</p> <p>WHERE u.status = 'active'</p> <p>GROUP BY u.id, u.email</p> <p>HAVING COUNT(o.id) > 0</p> <p>"""</p> </code>

JavaScript (template literals)
<code>javascript <p>const query = </code></p> <p>SELECT user_id, SUM(amount) AS total</p> <p>FROM transactions</p> <p>WHERE created_at >= $1</p> <p>GROUP BY user_id</p> <p>ORDER BY total DESC</p> <code>; </code>

Dialect Differences Matter

| Feature | MySQL | PostgreSQL | SQL Server |

|---|---|---|---|

| Identifier quoting | name | "name" | [name] |

| Limit syntax | LIMIT 10 | LIMIT 10 | TOP 10 |

| Boolean | TINYINT(1) | BOOLEAN | BIT` |

A formatter that understands dialects handles these correctly rather than applying a one-size-fits-all style.

For formatting SQL in the browser — MySQL, PostgreSQL, SQL Server, SQLite, and Spark dialects, with both beautify and minify modes — try the SQL Formatter & Beautifier on SnappyTools. No signup required.

Originally published at https://snappytools.app/sql-formatter-beautifier/

🔥 Join developers growing publicly
Share your knowledge, build in public, and grow your developer presence with a global community.

More Posts

TypeScript Complexity Has Finally Reached the Point of Total Absurdity

Karol Modelskiverified - Apr 23

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

5 Web Dev Pitfalls That Are Silently Killing Your Projects (With Real Fixes)

Dharanidharan - Mar 3

Sovereign Intelligence: The Complete 25,000 Word Blueprint (Download)

Pocket Portfolio - Apr 1
chevron_left
2.2k Points86 Badges
93Posts
0Comments
SnappyTools builds free, fast, browser-based tools for developers, writers, and designers. No signup... Show more

Related Jobs

Commenters (This Week)

2 comments
1 comment

Contribute meaningful comments to climb the leaderboard and earn badges!