Skip to content
toolbelt
Back to Guides

Making a 400-Line Query Reviewable

By ·

Updated

Long queries rarely have logic bugs. They have formatting bugs — a join that binds to the wrong line and nobody notices in review.

What is this tool?

A SQL formatter lays out a query so clauses and joins are visually separated, which is how you spot a missing condition.

How it works

sql-formatter parses the dialect you pick and re-prints it with aligned keywords and indented subqueries. Dialect matters: MySQL backticks and T-SQL brackets are not interchangeable, and the wrong dialect produces confusing errors rather than helpful ones.

Example

Before:
select u.name,count(o.id) from users u left join orders o on o.user_id=u.id group by u.name having count(o.id)>0 order by 2 desc

After:
select
  u.name,
  count(o.id)
from users u
left join orders o on o.user_id = u.id
group by u.name
having count(o.id) > 0
order by 2 desc

When to use it

Before code review, always. When a query returns more rows than you expected, formatting usually shows you the missing WHERE clause within seconds.

Optimization Tips

Check that your LEFT JOIN did not become an INNER JOIN because of a condition in the WHERE clause. It is the single most common SQL bug, and it is nearly invisible in unformatted queries.

Frequently Asked Questions

Which dialects are supported?
MySQL, PostgreSQL, SQLite and T-SQL, plus several others. Pick the one your database actually uses.
Does it optimise my query?
No. It is purely visual. A formatted query can still do a full table scan.
Why does the wrong dialect matter?
Quoted identifiers and some functions differ. The wrong dialect will either fail or produce output your database rejects.
Does it touch my data?
There is no database involved. It parses text.

Ready to try it out?

Put this knowledge into practice with our free tool.

Go to Tool

Related tools