Queries generated by ORMs and pasted from logs tend to arrive as one endless line. This formatter puts each clause on its own line, indents joins and conditions, nests subqueries, and capitalises keywords if you like. It can also do the opposite and squeeze a query onto one line. It works on the standard clauses shared by MySQL, PostgreSQL, SQL Server and SQLite.
How to use it
- Paste your query.
- Choose format or minify.
- Decide whether keywords should be capitalised.
- Copy the formatted query.
When it helps
- Reading a query from an application log.
- Tidying SQL before a code review.
- Debugging a long query with several joins.
- Putting SQL into documentation.
Good to know
- Strings and comments are kept exactly as written.
- Subqueries in brackets get their own indented block.
- Only layout changes; the logic of the query is untouched, so it's safe to run before and after.
What formatting changes
The tool splits queries at major clauses, SELECT, FROM, WHERE, GROUP BY, ORDER BY and joins, and puts each on its own line. Columns in a select list go one per line, and conditions joined by AND and OR are indented. Subqueries in brackets get their own block.
Capitalising keywords makes the structure stand out from table and column names, and a consistent layout makes differences easier to see in code review.
Worked example: a report query
A one-line query selecting users and counting orders, with a left join, a filter, a group by and a limit, comes out as about twelve neat lines. You can see at once which tables are joined, which conditions apply, and how results are grouped.
Minify does the reverse, useful for embedding a query in a string.
Habits for readable SQL
Use meaningful aliases, qualify columns with their table alias, put one condition per line, and comment anything non-obvious. Avoid SELECT * in production code, because it hides which columns you need and can break when tables change.
Always test formatted queries before running them against live data, and use transactions for changes.
Reading query plans
Once a query is readable, you can reason about its speed. Look at joins, filters and sorting. Add indexes on columns used in WHERE and JOIN conditions, and use your database's EXPLAIN command to see how it runs the query.
A formatted query makes it easier to spot a missing join condition, which can produce huge, slow result sets.
Formatting conventions teams adopt
Many teams settle on capital keywords, one column per line, commas at the end of lines, and a blank line between statements. A few prefer leading commas, which make version-control diffs cleaner. Pick one style and apply it everywhere.
Automating the formatting in your editor means nobody has to think about it.
Questions
Does it run my query?
No, it only reformats the text.
Does it cover every dialect?
It handles common standard syntax. Unusual vendor features may not be laid out ideally.