SQL Formatter
Beautify and minify SQL queries. Indent clauses, toggle uppercase keywords, choose indent size.
Use SQL Formatter
SELECT u.id, u.name, COUNT (o.id) AS orders FROM users u LEFT JOIN orders o ON o.user_id = u.id WHERE u.active = 1 AND u.created_at > '2024-01-01' GROUP BY u.id, u.name HAVING COUNT (o.id) > 3 ORDER BY orders DESC LIMIT 10;
Why formatting SQL matters
A well-formatted query is far easier to read, review, and debug than one crammed onto a single line. Putting each clause — SELECT, FROM, WHERE, JOIN, GROUP BY, ORDER BY — on its own line lets your eye jump straight to the part you care about. Breaking column lists after each comma makes it obvious which fields are selected and quick to add or remove one in a diff. Indenting AND/OR conditions under their clause shows the filter logic at a glance, and nesting parentheses one level deeper keeps subqueries visually distinct from the outer statement.
Format and minify in one place
Use Format while you are writing and reviewing queries: pick a 2-, 4-, or 8-space indent and decide whether keywords should be uppercase to match your team's style guide. When you need the opposite — a compact single-line query to drop into a config value, an environment variable, a log statement, or a quick script — switch to Minify. It strips the extra newlines and collapses runs of whitespace while keeping commas and parentheses readable. Both modes work on the same tokenized representation, so you can flip back and forth without losing anything.
How the tokenizer works
Rather than using fragile find-and-replace rules, the formatter walks your SQL character by character and splits it into tokens: keywords, identifiers, numbers, quoted strings, comments, commas, and parentheses. Strings and comments are captured whole so their contents are never reformatted — a comma or the word FROM inside a string literal will not trigger a line break. Multi-word keywords like LEFT OUTER JOIN and GROUP BY are recognised as single units. This token-aware approach is why the output stays correct even for queries the tool has never seen before.
Frequently Asked Questions
Find this useful?
These tools are free and ad-free. Support the project!