To format a SQL query, put each major clause (SELECT, FROM, WHERE, JOIN, GROUP BY) on its own line, indent the columns and conditions under it, and use a consistent keyword case. The fastest way is to paste it into a SQL formatter that auto-detects your dialect and re-indents it in one click, entirely in your browser.
Formatting is not cosmetic. A query crammed onto one line hides the exact bugs that cost you hours: a missing join condition that quietly turns an inner join into a cross join, a WHERE clause that should have been an ON, or an OR that binds wider than you think. Spread the same query across indented lines and those mistakes jump out. Readable SQL is debuggable SQL.
Messy SQL vs Formatted SQL#
Here is a query the way it usually arrives, pasted from a log or an ORM dump:
select u.id,u.name,o.total from users u join orders o on u.id=o.user_id where u.active=1 and o.total>100 order by o.total desc;
It runs, but you cannot scan it. Now the same query, formatted:
SELECT
u.id,
u.name,
o.total
FROM users u
JOIN orders o ON u.id = o.user_id
WHERE u.active = 1
AND o.total > 100
ORDER BY o.total DESC;
Nothing about the logic changed. But now the join condition, the two filters, and the sort are each on their own line, the AND is indented under WHERE so you can see it is a second condition, and uppercase keywords separate structure from your column and table names. If that JOIN were missing its ON, the gap would be obvious at a glance.
The Core Formatting Rules#
Good SQL formatting comes down to a handful of consistent decisions. Apply them the same way every time and your queries become predictable to read.
- One clause per line.
SELECT,FROM,WHERE,GROUP BY,HAVING,ORDER BY, and eachJOINstart a new line. - Indent the body. Columns in the select list, conditions in the where clause, and joined tables sit indented under their clause.
- Leading boolean operators. Put
ANDandORat the start of the line, not the end. You read top-to-bottom, so the operator that connects two conditions belongs where your eye lands. - Consistent keyword case. Uppercase keywords (
SELECT,FROM) against lowercase or mixed-case identifiers is the common convention because it visually separates language from data. Pick one case and never mix. - Align comparisons.
column = valuewith spaces around the operator reads faster thancolumn=value.
| Style choice | Common options | Why it matters |
|---|---|---|
| Keyword case | UPPERCASE / lowercase | Separates SQL keywords from your identifiers |
| Indentation | 2 or 4 spaces | Shows clause-body hierarchy; pick one and stay consistent |
| Boolean operators | leading / trailing | Leading AND/OR reads better in stacked conditions |
| Comma position | leading / trailing | Leading commas make it easy to comment out a column |
Dialect Quirks That Generic Beautifiers Break#
This is where most one-size-fits-all formatters fall down. SQL is not a single language; each database has syntax a naive formatter mangles. If your beautifier does not know the dialect, it can break valid SQL or leave it ugly.
- T-SQL (SQL Server) uses square-bracket identifiers like
[Order Details]and[user]. A formatter that does not recognize them may add stray spaces inside the brackets or treat them as separate tokens. T-SQL also usesTOPand proceduralBEGIN ... ENDblocks that need their own indentation. - PostgreSQL has dollar-quoted strings (
$$ ... $$) for function bodies,::cast syntax, and array literals. A formatter has to leave the contents of a$$block alone rather than reformatting it as if it were a statement. - MySQL uses backtick-quoted identifiers (
`select`) and has its own functions andLIMITsyntax. - Oracle (PL/SQL) wraps logic in
DECLARE/BEGIN/ENDblocks and uses(+)for legacy outer joins, neither of which a plain SELECT formatter handles. - SQLite is mostly standard but tolerates double-quoted strings in places others reject.
A formatter that auto-detects which dialect you pasted, then applies the right tokenizing rules, is the difference between clean output and a broken query. That is why dialect detection matters more than the indentation settings.
Tip: if a formatter ever changes what your query does, not just how it looks, it parsed the dialect wrong. Formatting must be purely presentational. Test the formatted version against your database before trusting it.
Why Browser-Side Formatting Matters for SQL#
There is a privacy angle specific to SQL that does not apply to formatting, say, generic JSON. Your queries contain your schema. Real table names, column names, and sometimes literal values like email addresses or IDs sit right there in the text. Pasting that into a random web tool means the query, and everything it reveals about your data model, hits someone else's server.
A formatter that runs 100% in your browser never uploads the query. The text is tokenized and re-indented by JavaScript in the page, and nothing leaves the tab. For anyone formatting production queries, internal schema names, or anything under an NDA, that is the only safe default. The Molixa SQL formatter works entirely client-side for exactly this reason: your query never touches a server.
Step 1: Paste the raw query and let it detect the dialect#
Drop your unformatted SQL into the free SQL formatter. It detects whether you pasted PostgreSQL, MySQL, T-SQL, Oracle, or SQLite from the syntax (bracket identifiers signal T-SQL, backticks signal MySQL, dollar-quoting signals PostgreSQL) and applies the matching tokenizer. If detection guesses wrong on an ambiguous snippet, set the dialect manually.
Step 2: Set your case and indentation, then format#
Choose uppercase or lowercase keywords and 2- or 4-space indentation to match your team's style. Run the format. The clauses break onto their own lines, the body indents, and boolean operators move to leading position. Read the result top to bottom and confirm every join has its ON and every condition is where you expect.
Step 3: Copy it back and verify against the database#
Copy the formatted query into your editor or migration file. Because formatting is presentational only, the query behaves identically, but run it once to confirm, especially if the tool had to guess the dialect. Then commit the readable version so the next person to touch it does not inherit a one-line wall.
Formatting in Your Workflow#
A formatter is most useful at two moments: when you inherit someone else's compressed query, and right before you commit your own. Formatting on the way in helps you understand foreign SQL; formatting on the way out keeps your repository readable. Many teams also run a formatter in a pre-commit hook so every committed query follows one house style automatically.
The same browser-side, no-upload principle applies across developer utilities. If you are also cleaning up API responses or config alongside your SQL, the JSON formatter handles those, and the regex tester is handy when you are pulling structured values out of query results or logs. For a related conversion task, our guide on turning JSON into TypeScript types covers the same in-browser, privacy-first approach.
Frequently Asked Questions#
How do I format a SQL query for readability?
Put each major clause (SELECT, FROM, WHERE, each JOIN, GROUP BY, ORDER BY) on its own line, indent the columns and conditions beneath their clause, place AND and OR at the start of stacked condition lines, and use a consistent keyword case. The quickest way is a SQL formatter that re-indents the whole query in one click.
Should SQL keywords be uppercase or lowercase? Either works; consistency is what matters. Uppercase keywords against lowercase identifiers is the most common convention because it visually separates SQL language from your table and column names. Pick one style, apply it to every query, and let a formatter enforce it so you never have to think about it again.
Is it safe to format SQL in an online tool? Only if it runs in your browser. Queries expose your schema, real table and column names, and sometimes literal values, so a tool that uploads the query sends all of that to a server. A client-side formatter like the Molixa SQL formatter processes everything in the page and never transmits your query, which is the safe choice for production SQL.
Does formatting change what my SQL query does? No. Correct formatting is purely presentational; it adds line breaks, indentation, and case changes without touching the logic. If a formatter ever alters the result, it parsed your dialect incorrectly. That is why dialect-aware formatting matters, and why you should run the formatted query once to confirm it behaves identically.
Can one formatter handle PostgreSQL, MySQL, and T-SQL? Yes, if it detects the dialect and applies the right rules. T-SQL uses square-bracket identifiers, MySQL uses backticks, and PostgreSQL uses dollar-quoted blocks, so a formatter has to recognize each to avoid mangling them. The Molixa formatter auto-detects PostgreSQL, MySQL, T-SQL, Oracle, and SQLite, and lets you override the choice manually.
Why put AND and OR at the start of the line? Because you read SQL top to bottom, and a leading boolean operator sits where your eye lands at the start of each condition, making the logical structure obvious. Trailing operators hide at the end of the previous line, where they are easy to miss. Leading commas in the select list work the same way and make commenting out a column trivial.



