SQL Formatter — beautify, minify, and pretty-print SQL
Paste compressed SQL to pretty-print it for MySQL, PostgreSQL, SQLite, SQL Server, Oracle, BigQuery, or Snowflake. Choose keyword case, indent style, and tab width. The minifier strips comments and collapses whitespace without breaking strings. Nothing is uploaded.
How it works
The formatting engine on this page is sql-formatter (Apache-2.0), a dialect-aware reformatter used inside Prisma Studio, dbt and similar tools. It ships hand-written tokenizers for each dialect, so the parser knows that backticks open an identifier in MySQL but a string literal in BigQuery, that $$body$$ is a PostgreSQL multi-line string, and that [name] is a Transact-SQL identifier. After tokenising, it emits clauses onto fresh lines with consistent indentation and applies the chosen keyword case.
The minify button uses a small, dialect-agnostic walker written for this site. It steps through the input one character at a time and tracks which lexical state it is in: code, single-quoted string, double-quoted identifier, MySQL backtick, T-SQL bracket, PostgreSQL dollar-quoted block, line comment, or block comment. Only when the walker is in code does it collapse whitespace and drop comments — so a -- inside a string literal, or a ; inside a dollar-quoted body, survives untouched. The same walker powers the statements counter, so it never miscounts a semicolon that happens to sit inside a string.
Every successful format also runs an idempotency check: the formatter is invoked a second time on its own output, and the two outputs are compared byte-for-byte. A properly designed pretty-printer is supposed to reach a fixed point — re-running it should not keep changing the text. The status tile confirms that property held for your query. If it ever fails, the page surfaces a warning so you can report a real parser bug rather than chasing a phantom diff.
Output stats include the number of statements (driven by the string-aware counter, not a naive split on ;), the output byte count and percentage delta versus the input, and a list of top-level SQL keywords that appeared. The keyword detector runs against the minified form of the input so words inside string literals and comments do not get counted as keywords.
One caveat: this version does not handle PostgreSQL’s nested block comments. Those are rare in human-written queries — the ANSI standard does not allow them at all — but if your input contains them, switch to plain line comments first or pre-strip them in your editor. Everything else in the lexical specification of the supported dialects is honoured.
Worked examples
Frequently asked questions
Sources & references
- sql-formatter — the underlying open-source library (Apache-2.0)
- ISO/IEC 9075 — ANSI/ISO SQL:2016 standard index
- PostgreSQL — SQL syntax (lexical structure, dollar quoting, E-strings)
- MySQL 8.4 Reference Manual — comment syntax
- SQLite — keywords and lexical structure
- Microsoft Transact-SQL — comments and identifiers reference
The formatter library is pinned at the version installed in this build (visible in package.json); the minifier and statement counter are written for this site. Lexical references last reviewed on 2026-05-11.
Related tools
Comments & feedback
Spotted a bug or want an improvement? Tell us — our team reviews every comment, and good ideas get built. Comments are public and anonymous.
Found a bug, edge case, or want to suggest an improvement?
Email me at [email protected] — most fixes ship within 24 hours.