Unreadable SQL

How to write Unreadable SQL… and why you shouldn’t
We’ve all seen it: a 200-line SQL query with no indentation, cryptic column aliases like a1, x, tmp, no comments, and nested subqueries that make your eyes bleed.

It runs. It works. But six months later, nobody, not even the original author, can understand it.

I’ve spent 35 years in banking IT reviewing SQL code. Unreadable queries cause bugs, slow reviews, and late-night firefighting.

Let’s look at how to write terrible SQL… and then how to write great SQL instead.

Unreadable query

select c.name,p.productname,o.orderdate,sum(od.quantityod.price) as tot from customers c join orders o on c.id=o.custid join orderdetails od on o.id=od.orderid join products p on od.productid=p.id where o.orderdate>’2024-01-01′ group by c.name,p.productname,o.orderdate having sum(od.quantityod.price)>1000 order by tot desc;

Problems:
One long line. impossible to scan.
Short aliases (c, o, od), no meaning.
No comments.
All lower case, no formatting.
Hard to debug or modify.

Readability rules: write SQL like a Pro

The same query, clean and maintainable:
Total sales per customer and product in 2024
Excludes low-value orders (< $1000)

— High-value sales report for 2024
— Shows total sales per customer and product
— Only includes orders over $1000 total
SELECT
c.name AS “Customer”,
p.productname AS “Product”,
o.orderdate AS “Order Date”,
SUM(od.quantity * od.unit_price) AS “Total Sales”
FROM
customers AS c
JOIN
orders AS o ON c.id = o.custid
JOIN
orderdetails AS od ON o.id = od.orderid
JOIN
products AS p ON od.productid = p.id
WHERE
o.orderdate > ‘2024-01-01’ — current year only
GROUP BY
c.name,
p.productname,
o.orderdate
HAVING
SUM(od.quantity * od.price) > 1000 — filter low-value orders
ORDER BY
“Total Sales” DESC — highest sales first

Why this is better:
Consistent formatting: aligned keywords, indentation.
Meaningful aliases: c → customers AS c (still short but clear).
Descriptive column aliases: quoted for sorting.
Comments: explain intent.
Logical sectioning: SELECT, FROM, JOIN, WHERE, GROUP BY, etc.

Key readability rules

Format consistently.
Uppercase keywords (SELECT, FROM, JOIN).
One clause per line.
Indent joins and conditions (not really visible in HTML).

Use meaningful names.
Table aliases: short but descriptive (cust instead of c, ord instead of o).
Column aliases: clear and quoted if needed.

Add comments
Explain why, not what (the code shows what).
Business logic, assumptions, known limitations.

CTEs vs. Subqueries
Use Common Table Expressions for clarity:

Bad (nested subqueries):
SELECT * FROM (SELECT * FROM (SELECT …)) sub1;

Good (CTEs):
WITH active_customers AS (
SELECT * FROM customers WHERE status = ‘active’
),
recent_orders AS (
SELECT * FROM orders WHERE order_date > ‘2024-01-01’
)
SELECT …
FROM active_customers ac
JOIN recent_orders ro ON …
CTEs read top-to-bottom like steps, much easier to follow.

Why readability matters (especially in teams)

Faster code reviews.
Fewer bugs when modifying.
New developers onboard quickly.
Easier debugging at 2 AM.

In banking, bad SQL caused real outages, unreadable queries hid logic errors that cost hours.

Final thought

Write SQL for the person who reads it next, often future-you.

Readable code is maintainable code.

Follow these rules and your queries will be clean, clear, and professional.

What’s the worst SQL you’ve ever inherited? Share in the comments!

This brings us to the end of my post on Unreadable SQL.

Thank you for taking the time to read my post on Unreadable SQL.

I hope you found it enjoyable and insightful.
Stay tuned for more content that is coming soon.
If you like what you read, please consider sharing it with others who might find it helpful.

Contact me

If you have any questions or want to contact me, please drop me an email at info@safecomputer.org

Stay updated with my monthly newsletter

Subscribe to Safe Computer’s monthly newsletter in the right sidebar for tips on job applications, cybersecurity, and more! Get summaries of my latest posts, like this Database Crimes, straight to your inbox. Join now at safecomputer.org!

Disclaimer

All tips and methods mentioned in this blog are tested on Windows 11. Please note that results may vary on other operating systems or versions of Windows. Adapt the instructions accordingly.

Copyright

© 2025 Henny Staas/safecomputer.org. Unauthorized use and/or duplication of this material without express and written permission from this site’s author and/or owner is strictly prohibited. Excerpts and links may be used, provided that full and clear credit is given to Henny Staas/safecomputer.org with appropriate and specific direction to the original content.