SF
SQL Formatter & Minifier
Uppercase keywords and lay SQL out one clause per line, or collapse it to a single normalized-whitespace line.
More Developer Tools Tools
How to Use SQL Formatter & Minifier
Paste a SQL statement into the box. Format uppercases common ANSI SQL keywords (SELECT, FROM, WHERE, JOIN, GROUP BY, ORDER BY, and more) and puts major clauses on their own indented line. Minify collapses the statement to a single line with normalized single-space whitespace. This is a keyword-based textual formatter, not a full SQL parser.
About SQL Formatter & Minifier
SQL statements, especially ones written or generated in a hurry, are frequently either crammed onto one line (common in application code where a query string is built inline) or inconsistently cased and indented (common when a query has been edited by several people over time). Both make a query harder to read at a glance than it needs to be — a SELECT with several JOINs and a multi-condition WHERE clause is much easier to scan when each major clause starts its own line and keywords are visually distinct from column and table names via consistent capitalization.
This tool takes a deliberately pragmatic, keyword-based approach rather than attempting to build a real SQL parser. A genuine SQL parser would need to understand a specific database dialect's full grammar (MySQL, PostgreSQL, SQL Server and Oracle all differ in meaningful ways — different quoting rules, different function sets, different proprietary syntax extensions), correctly handle arbitrarily nested subqueries, common table expressions (CTEs), window functions, and vendor-specific clauses, which is a substantial undertaking and inherently dialect-specific. Instead, this formatter recognises a broad set of common ANSI SQL keywords (SELECT, FROM, WHERE, INSERT INTO, UPDATE, DELETE, JOIN and its variants — INNER JOIN, LEFT JOIN, RIGHT JOIN, FULL JOIN — ON, AND, OR, GROUP BY, ORDER BY, HAVING, LIMIT, UNION, and others), uppercases them for visual consistency, and places the major clause-starting keywords on their own line with indentation, while leaving everything else (identifiers, string literals, numbers, operators) exactly as written.
The formatter is careful to avoid the most common way a naive find-and-replace keyword formatter breaks real queries: matching a keyword-like substring inside a quoted string literal or a quoted identifier. A query containing WHERE name = 'FROM the start' should not have "FROM" inside that string literal uppercased or treated as a clause boundary — this tool tracks whether it is currently inside a single-quoted string (with support for the standard SQL escaping convention of doubling a quote, '' , to represent a literal quote character within a string) and skips keyword matching entirely while inside one. Keyword matching is also done on whole-word boundaries, so a column literally named fromage or a table named ordering is not mistaken for the FROM or ORDER BY keywords.
Because this is a textual, keyword-driven formatter rather than a parser that understands SQL's actual grammar and precedence rules, it does not track parenthesis nesting to intelligently indent deeply nested subqueries the way a true SQL-aware formatter would (a subquery inside a WHERE ... IN (SELECT ...) is formatted using the same flat clause-line rules as the outer query, rather than being indented as a nested block) — and it applies its keyword rules generically across ANSI SQL rather than tailoring output to any one specific database's dialect quirks or proprietary extensions. For a complex, deeply nested query or one using heavily vendor-specific syntax, the output may not look exactly the way a dedicated dialect-aware formatter (such as sql-formatter the npm package, or a database IDE's built-in formatter) would produce it, and you should sanity-check the formatted result before relying on it. For everyday queries — the kind most people actually paste into a formatter, a SELECT with a few JOINs and a WHERE clause — this approach produces clean, readable, correctly-cased output without needing a database connection or a dialect-specific parser.
Details & Tips
How Format works: the statement is tokenized while tracking single-quoted string boundaries (respecting the '' escaped-quote convention) so that keyword-like text inside string literals is never touched. Outside of strings, whole-word matches against a list of common ANSI SQL keywords are uppercased. A subset of these keywords — the ones that typically start a new clause (SELECT, FROM, WHERE, INNER JOIN/LEFT JOIN/RIGHT JOIN/FULL JOIN, ON, GROUP BY, ORDER BY, HAVING, LIMIT, UNION, INSERT INTO, VALUES, UPDATE, SET, DELETE FROM) — also triggers a line break and consistent indentation before them, so each major clause of the query starts on its own line. AND/OR inside a WHERE or HAVING clause are placed on their own indented continuation line as well, which is what makes multi-condition WHERE clauses readable.
How Minify works: the same string-aware tokenizer is used, but instead of inserting line breaks, all whitespace (including newlines) outside of string literals is collapsed to single spaces, and leading/trailing whitespace is trimmed, producing the statement as one normalized line. Keyword casing is left as originally written in Minify mode (only Format changes casing), since minify's purpose is compactness, not style normalization.
Worked example — formatting:
Input: `select id, name from users where status = 'active' and age > 18 order by name`
Formatted output:
```
SELECT id, name
FROM users
WHERE status = 'active'
AND age > 18
ORDER BY name
```
What is out of scope: this is a keyword-based textual formatter, not a SQL parser — it does not track parenthesis depth to indent nested subqueries as nested blocks, it does not understand or validate SQL semantics (it will happily 'format' a syntactically invalid query, since it is not checking validity), it does not resolve or reformat vendor-specific dialect extensions specially, and it does not touch identifiers, literals, comments, or operators beyond whitespace normalization. Deeply nested subqueries, CTEs (WITH clauses) with multiple named subqueries, and window function syntax (OVER (...)) will still be uppercased where keywords are recognised, but their layout will follow the same flat clause-based rules as the rest of the query rather than a query-plan-aware nested indentation. For complex queries in a real workflow, a dialect-aware formatter integrated into your database IDE or a package such as the sql-formatter npm library will produce more sophisticated nested layout.
Frequently Asked Questions
Is this a full SQL parser?
No, it is a keyword-based textual formatter. It recognises and uppercases common ANSI SQL keywords and lays out major clauses on their own line, but it does not parse or validate SQL grammar the way a real database-aware formatter would.
Will this mangle a string literal that contains a word like "FROM" or "WHERE"?
No, the tokenizer tracks single-quoted string boundaries (including the doubled-quote '' escape convention) and never applies keyword matching or casing changes inside a string literal.
Does it handle deeply nested subqueries with proper indentation?
Not with nested-block indentation — subqueries are formatted using the same flat clause-line rules as the rest of the query, since this tool does not track parenthesis nesting the way a full SQL-aware formatter does.
Which SQL dialect does this target?
A broad, common subset of ANSI SQL keywords shared across MySQL, PostgreSQL, SQL Server and similar databases. It does not specially handle any one dialect's proprietary syntax extensions.
Does Format validate that my SQL is correct?
No, it only recognises and reformats keyword text — it has no notion of whether the query is syntactically or semantically valid, and will format invalid SQL without complaint.
What does Minify do differently from Format?
Minify collapses all whitespace to a single normalized line without changing keyword casing, since its goal is compactness. Format uppercases keywords and adds line breaks/indentation for readability.
Will keyword matching accidentally match a column named "order" or "from"?
No, matching is done on whole-word boundaries, so identifiers that merely contain or resemble a keyword (like fromage or ordering) are not matched, though an identifier that is exactly a reserved word (e.g. a column literally named order) would be matched — quote such identifiers to protect them.
Are AND/OR in a WHERE clause placed on their own line?
Yes, in Format mode AND/OR are given their own indented continuation line, which is what makes multi-condition WHERE and HAVING clauses easy to scan.
Is my SQL sent to a server or database?
No, formatting and minifying both run entirely in your browser using JavaScript. Nothing is executed against a database and nothing is uploaded.
What should I use for complex, dialect-specific SQL formatting?
A dedicated dialect-aware tool, such as your database IDE's built-in formatter or a library like the sql-formatter npm package, will produce more sophisticated nested layout for complex queries with CTEs, window functions, and deeply nested subqueries.
Part of These Collections
Also Available As
sql formatter, sql minifier, sql beautifier, format sql online, sql query formatter, sql pretty print, sql indent
Found a Problem?
Let us know if something with SQL Formatter & Minifier isn't working as expected.
Something went wrong. Please try again.
Thanks — we'll take a look.