justuse.me
TutorialApril 14, 2026

How to Format a Messy SQL Query So You Can Actually Read It

Unformatted SQL is a nightmare to debug. Here's how to clean it up fast, online, without installing anything.

Last week I was handed a SQL query that looked like someone had taken a perfectly good SELECT statement and put it through a blender. No line breaks, no indentation, table aliases that made no sense, and about six nested subqueries jammed into a single wall of text. I needed to understand what it was doing before I could fix a bug in it. That took way longer than it should have.

If you've been there, you know the feeling. Here's what I've figured out about cleaning up SQL fast.

Why does unformatted SQL happen in the first place?

Usually it's one of a few things. ORMs generate it. Minification tools strip whitespace to save bytes in logs or config files. Someone copied a query out of a database client that compresses output. Or honestly, some developers just never bother formatting as they write.

The result is the same: a query that's technically valid but practically unreadable. And unreadable code is where bugs hide.

What does "formatted" SQL actually mean?

Formatted SQL has a few consistent properties:

  • Each major clause (SELECT, FROM, WHERE, JOIN, GROUP BY, etc.) starts on a new line
  • Nested subqueries are indented to show hierarchy
  • Column lists are broken into one column per line
  • Conditions in WHERE and ON clauses are aligned
  • Keywords are consistently uppercase or lowercase

Here's a quick before and after. Before: