sql-formatter
Paste a messy SQL query to format and indent it, or minify it for logging and storage. Keyword capitalisation, clause indentation, and JOIN formatting handled automatically.
client-side only T-SQL / PostgreSQL / MySQL format + minify no signup
Indent spaces
Comma
input.sql
output.sql
// what this formats
- Clause keywords —
SELECT,FROM,WHERE,JOIN,GROUP BY,ORDER BY,HAVING,LIMIT,UNION, and all their variants each start on a new line. - Compound joins —
LEFT JOIN,INNER JOIN,LEFT OUTER JOIN, etc. are detected as single tokens and formatted correctly, not split across lines. - Subqueries — parenthesised
SELECTstatements are indented one level deeper than the enclosing query. - String literals and comments — single-quoted strings, double-quoted strings,
-- line comments, and/* block comments */are preserved exactly as written, never modified. - Bracket and backtick identifiers —
[column name](T-SQL) and`table`(MySQL) are preserved as-is.
// what this does not do
// minifying SQL
Minified SQL is identical in execution to formatted SQL — it's just whitespace removed. Useful when logging queries (a compact string is much easier to search in a log aggregator than a formatted multi-line one), embedding SQL in application config files where line breaks would be awkward, or storing parameterized query templates where size matters. Minifying does not remove comments — strip those manually if you don't want them in the output.
This is a keyword-aware formatter, not a full SQL parser. It does not resolve table aliases, validate that column names exist, or understand stored procedure / function body blocks (
BEGIN…END) as deeply as a dialect-specific tool would. For complex PL/pgSQL, T-SQL stored procedures with control flow, or dynamic SQL, the formatting output is best-effort — check the result before using it in a migration or deployment script.