SQL Query Beautifier
Reformat any SQL query into a column-aligned cheat-sheet style — SELECT columns pad so `AS alias` lines up, major clauses break onto their own lines, keyword case enforced (UPPER / lower / preserve), trailing or leading commas, configurable indent. Distinct from a basic formatter — built for code review and dashboards. 100% offline.
What is the SQL Query Beautifier?
SQL Query Beautifier is a column-aligned re-formatter. The tokeniser handles SQL strings (single + dollar-quoted), block + line comments (preserved), ~140 ANSI keywords, identifiers and operators. Pass 1 emits each major clause (SELECT / FROM / WHERE / GROUP / ORDER / JOIN / UNION / etc.) on its own line, breaks on commas at parenthesis-depth 0 (trailing or leading style), preserves comments at their relative position. Pass 2 (optional) aligns SELECT columns — pads each column expression to the longest in its group so `AS alias` lines up vertically. Configuration: keyword case (UPPER, lower, or preserve), comma style (trailing = PEP-like, leading = Joe Celko's classic style), indent (2 / 4 / tab). All inputs from one-liners to deeply nested CTEs flow through the same pipeline.
How to use it
- Paste a SQL query — single line or messy whitespace are both fine.
- Pick keyword case (UPPER is the SQL norm).
- Pick comma style (trailing fits most ORMs; leading reads cleaner for code review).
- Toggle 'Align SELECT columns' for the cheat-sheet style.
- Click Copy.
Benefits
- Column alignment in SELECT lists — `AS alias` lines up vertically for readability.
- Three keyword case options: UPPER, lower, or preserve original.
- Trailing or leading comma style (Joe Celko's classic).
- Configurable indent: 2 spaces, 4 spaces, or tab.
- Major clauses break onto their own lines (SELECT / FROM / WHERE / etc.).
- Preserves both `--` line comments and `/* */` block comments.
- Handles single-quoted strings, dollar-quoted strings (Postgres), and back-ticked identifiers.
- Live preview updates as you type.
- Settings persist in localStorage.
- Runs 100% in your browser — no server, no upload.
Frequently asked questions
How is this different from the SQL Formatter?
The SQL Formatter does basic indentation. This tool additionally aligns SELECT columns visually (padding so `AS alias` lines up), and supports comma-leading style + keyword case enforcement. It's the choice when output legibility matters more than terseness — code review, dashboard READMEs, or sharing in Slack.
What's the difference between trailing and leading commas?
Trailing: comma at line end. Leading: comma at next line start. Leading commas make it easy to comment out a column without breaking the rest, and the comma column visually anchors the SELECT list. Trailing is more common in ORM-generated code.
Does it understand my dialect (Postgres / MySQL / SQL Server / Oracle)?
The keyword list covers ANSI-standard SQL + the most common dialect keywords. Dialect-specific functions (e.g. `STRING_AGG`, `DATEADD`) are treated as identifiers — they're emitted unchanged, just not coloured as keywords.
Why does my formatted query still look messy?
Two common reasons: (1) the input has unclosed parens or quotes — the tokeniser falls back to permissive mode. (2) The query nests deep CTEs — the engine indents per-clause but not per-subquery.
Will it change my query's semantics?
No — only whitespace and keyword case change. The output produces identical results to the input on any SQL engine.
What about case-sensitive identifiers in PostgreSQL?
Back-ticked or double-quoted identifiers are preserved as-is. The case-folding only affects bare keywords.
Can I beautify multiple queries at once?
Separate them with `;` — the engine handles statement boundaries and emits each on its own block.
Why is the keyword set hand-rolled rather than dialect-aware?
Shipping a full grammar per dialect would balloon bundle size. The 140-keyword ANSI set covers the vast majority of real queries; everything else passes through as an identifier.
Does it preserve comments?
Yes — `--` line comments and `/* */` block comments are emitted in their original position within the structure.
Are my settings saved?
Yes — keyword case, comma style, indent, alignment toggle and the input all persist in localStorage.
Is anything uploaded?
No. Tokenisation and formatting run entirely in your browser.