SQL Formatter: How to Tidy a Long Query Without Changing What It Does

You copy a query from an application log or a reporting tool and find it as one long tangled line. You try to work out which conditions apply, where the joins end and where the grouping starts, and get lost. Then you need to review it with a colleague or put it in a pull request. The answer is to format it. Sahel's SQL formatter lays the query out in logical lines and indentation, or squeezes it onto one line, and checks that nothing in its content changed.
Why SQL formatting matters
The database does not care about spaces and new lines, so it runs a tidy query and a tangled one with the same result. A human reader cares a great deal. A tidy query puts each part on its own line: what is selected, from where, how it joins, what the conditions are, how it groups and sorts. The reviewer sees the structure at a glance and spots a misplaced condition or a join missing its condition, mistakes that long lines hide.
What formatting does
- Starts a new line for each main clause: SELECT, FROM, WHERE, GROUP BY, ORDER BY and so on.
- Puts each column on its own line: in the SELECT list, which makes adding or removing a column and reviewing the diff in version control easier.
- Indents conditions and joins: so what belongs to what is visible.
- Unifies keyword case: you can write them in capitals, the common convention, or leave them as they are.
- Lets you choose the indent: two spaces or four.
Minifying: when you need it
The opposite of formatting is minifying: it squeezes the query onto one line with the fewest spaces. It helps when you put a query in a config file, an environment variable, a log or a text field that does not accept several lines. The tool shows how much shorter it got. Note that minifying removes comments, so keep a formatted copy if your comments matter.
The key guarantee: the content does not change
The most dangerous thing a formatter can do is corrupt the query while tidying it. Imagine one that changes a quoted string like 'New York' by dropping the space between the words, or changes the case of a comparison value such as 'Admin' to 'ADMIN'. The query would still run but silently give a different result, which is worse than an obvious error.
The tool avoids this with a strict rule: it does not touch quoted strings, numbers, column names or operators, and changes only spacing and keyword case. After every format the tool itself compares the query's "tokens" before and after, one by one, and tells you plainly whether they match. You see a message that no string, number or operator changed, or a warning not to use the output if anything differs.
A good formatter does not change what your query means, and a better one proves it to you every time.
Does it validate the SQL?
No. A formatter lays out; it does not understand your database or know your tables. A misspelled table name stays wrong after formatting. If there is a syntax error such as an unclosed bracket, formatting often makes it obvious through odd indentation, which is a side benefit. For real validation, run the query against a test copy of your database.
Supported dialects
The formatter works with the common syntax shared by MySQL, PostgreSQL, SQL Server and SQLite, enough for most daily queries: selecting, joining, grouping and subqueries. It may not recognise syntax specific to one database, and in that case it treats what it does not know as plain text and leaves it unchanged.
Comments while formatting
When formatting, comments are kept rather than dropped; each one appears on its own line near its original place, which makes formatting safe for the documented queries you keep in a project. A comment that starts with two dashes ends at the end of its line, which is why lines after it are never merged into one line except when minifying, after the comment itself has been removed.
Tips for writing readable queries
- Use meaningful aliases: u for users and o for orders, not a and b.
- Name columns explicitly: avoid SELECT * in lasting code.
- Make joins clear: use JOIN ... ON rather than comma-separated tables with the condition in WHERE.
- Comment non-obvious logic: and do not minify commented queries you will come back to.
How to use it
- Paste the query in the box.
- Choose "Format" or "Minify".
- For formatting, choose upper-case keywords or not, and the indent.
- Check the verification message, then copy the result.
A realistic scenario
A data analyst opens a report query inherited from a former colleague: a single line with a three-table join, two nested conditions and grouping. She pastes it into the tool and it appears in fifteen tidy lines, and at first glance she notices that the date condition is written against the wrong table. Without formatting she would have spent an hour searching for why the numbers did not match the report. After the fix she minifies the query and puts it in the config file.
Frequently asked questions
Does the formatter change what the query means?
No. Only spacing and keyword case change, and the tool verifies that itself.
Does it validate the SQL?
No. It formats; it does not analyse or run the query.
Which SQL dialects does it work with?
The common syntax shared by MySQL, PostgreSQL, SQL Server and SQLite.
Does minifying remove comments?
Yes, because a comment can run to the end of the line and break what follows if the query is squeezed. Keep a formatted copy if you want your comments.
Are my queries sent to a server?
No. Everything stays in your browser, which matters if they contain internal table names.
Related articles

How to Test a Regular Expression: Making Sure Your Pattern Matches Only What You Mean

Markdown Previewer: Write in a Simple Syntax, See the Result Instantly, and Copy Clean, Safe HTML

Lorem Ipsum Generator: Why Designers Use Placeholder Text and How to Use It Properly
