How to use the SQL Formatter & Beautifier
- Paste a query, or load the sample.
- Choose 2 or 4 space indentation and whether keywords should be uppercase.
- Copy the formatted query or download it as a .sql file. Use Minify to collapse it to a single line.
How it works
The formatter first splits the query into tokens: keywords, identifiers, string literals, quoted names, numbers, operators, and comments. It then rebuilds the query with each major clause, such as SELECT, FROM, WHERE, JOIN, GROUP BY, and ORDER BY, on a new line, conditions joined by AND or OR on their own lines, and subqueries indented.
String literals ('...'), quoted identifiers ("...", `...`, [...]), and comments are copied exactly as written. Formatting changes only the whitespace between tokens and, if you choose, the case of keywords. Minifying turns -- line comments into /* */ comments so they cannot hide the rest of the query once it is on one line.
select id,name from users u join orders o on o.user_id=u.id where o.total>100
→
SELECT id, name
FROM users u
JOIN orders o ON o.user_id = u.id
WHERE o.total > 100Worked example: making a one-line query reviewable
A query copied from application logs often arrives on one long line. After formatting, each join and condition sits on its own line, which makes a missing join condition, the cause of accidental cross joins, much easier to spot in review.
Limitations
- The formatter does not validate or run SQL. Syntax errors are reformatted as they are.
- Vendor-specific syntax, procedural code (PL/pgSQL, T-SQL blocks), and very unusual constructs may not be laid out ideally.
- Comments are kept, but a comment at the end of a line may move to a different line after formatting.
Frequently asked questions
Should SQL keywords be uppercase?
It is a style convention, not a requirement; SQL keywords are case-insensitive. Uppercase keywords make it easier to tell commands from table and column names.
Does formatting change what my query does?
No. Only whitespace and keyword case change. String values and quoted identifiers are untouched.
Is my query sent anywhere?
No. Formatting happens in your browser.