SQL Formatter
Format messy SQL into readable, indented statements — or minify it back into a compact single-line form. Handles strings, comments, identifiers and subqueries correctly. Runs entirely in your browser.
SQL
How it works
- A real tokenizer walks the SQL character by character, respecting single-quoted strings (with
''escapes), double-quoted identifiers, backtick and[bracketed]identifiers,--line comments,/* block */comments, numbers and punctuation. - Main clauses (
SELECT,FROM,WHERE,JOIN,GROUP BY,ORDER BY,HAVING,LIMIT,UNION,INSERT INTO,VALUES,UPDATE,SET,DELETE FROM,ON…) start a new line;AND/ORinsideWHERE/ONare indented one extra level and aligned. - Commas stay at the end of the line, subqueries are indented by parenthesis depth, and strings, comments and identifiers keep their original case.
- Minify strips whitespace and newlines outside strings and comments, keeping just enough spacing between tokens — the statement stays executable, just compact.
About SQL Formatter
SQL written by hand, dumped by ORMs, or pasted out of logs is almost always a single cramped line or an inconsistent jumble of casing and indentation. Formatting it safely is harder than it looks — a naive regex that adds newlines before keywords will happily split string literals that happen to contain those words. This tool tokenizes first, so quoted strings, comments and identifiers are treated as opaque units and never broken apart.
The same token stream powers the minifier, which collapses whitespace while keeping keyword/identifier boundaries distinct, so the minified statement parses identically. Use Format to review and edit your query, then Minify to shrink it for embedding in code, configuration files or logs. Both directions preserve comments, string content and identifier casing exactly as written.