SQL Formatter
What Is SQL Formatting?
SQL formatting is the process of arranging a query's clauses, keywords, and expressions onto consistent lines with predictable indentation so the logic is easy to read. A query that works perfectly can still be painful to maintain when it arrives as one long unbroken line, with mixed-case keywords and no spacing. A formatter rewrites that same query — without changing what it does — so that each major clause such as SELECT, FROM, WHERE, and the various JOIN types starts on its own line, selected columns are indented beneath SELECT, and comma-separated items in the top-level clause break onto separate lines.
This formatter works by tokenizing your SQL: it scans the text and breaks it into meaningful pieces — keywords, identifiers, string and numeric literals, operators, punctuation, and comments — before reassembling them with consistent layout. Because it recognizes string literals (single, double, and backtick-quoted) and both single-line -- and multi-line /* */ comments as distinct tokens, it won't mistakenly reformat the contents of a string or a comment. It understands multi-word keywords like LEFT OUTER JOIN, GROUP BY, and UNION ALL, and it manages indentation around CASE expressions and parenthesized subqueries so nested logic stays readable.
How to Use This SQL Formatter
Paste your query into the Input SQL box on the left. Click Format to produce a beautified, indented version in the read-only Formatted Output box on the right, with each major clause on its own line and keywords upper-cased. Click Minify instead to collapse the query down to a single line with comments stripped out — handy for embedding SQL in code or a config value.
The Uppercase Keywords button converts recognized SQL keywords to uppercase while leaving the rest of your layout, identifiers, and values untouched, which is useful when you only want to apply the common keyword-casing convention without reflowing the whole query. Use Copy Output to copy the result to your clipboard and Clear to reset both boxes. If you click a button with an empty input, the tool shows a prompt asking you to enter a query first.
Real-World Use Cases
- Cleaning up generated SQL — reformat queries produced by an ORM or query builder that arrive as a single dense line.
- Code review and pull requests — apply consistent formatting before committing so reviewers can read changes clearly.
- Debugging complex queries — break a tangled query with multiple JOINs and subqueries onto separate lines to spot logic errors.
- Embedding SQL in application code — minify a query to a single line to drop it into a string literal or configuration file.
- Standardizing team style — uppercase keywords across a batch of queries so everyone's SQL follows the same convention.
Tips for Formatting SQL
- Keep your string literals and comments intact — the tokenizer recognizes
'...',"...", backtick identifiers, and both comment styles, so their contents are preserved exactly. - Use Minify when you need a compact one-liner, but remember it strips comments, so keep a formatted copy if those notes matter.
- Run Uppercase Keywords alone when your indentation is already fine and you only want to normalize casing.
- The formatter indents columns after
SELECTand dedents beforeFROMandWHERE, so very deeply nested subqueries read most clearly when you format the whole statement at once. - If the result looks off, check for unbalanced parentheses or an unterminated string in the source — malformed input can confuse any structural formatter.
Features
- Format — adds line breaks and indentation, placing each major clause on its own line and upper-casing keywords.
- Minify — compresses the query to a single line and removes comments.
- Uppercase Keywords — converts recognized keywords to uppercase without otherwise changing your layout.
- Token-aware parsing — correctly handles string literals, backtick identifiers, and both single-line and multi-line comments.
- Multi-word keyword support — recognizes phrases like
LEFT OUTER JOIN,GROUP BY, andUNION ALLas single keywords. - Copy and clear — one-click copy of the output and reset of both panes.
- 100% client-side — your data never leaves your browser.
Frequently Asked Questions
Does this tool validate or execute my SQL?
No. This is a formatter, not a validator or query runner. It restructures the text for readability without checking whether the syntax is correct or running the query against any database.
Which SQL dialects does it support?
It works with standard SQL keywords shared across MySQL, PostgreSQL, SQLite, and SQL Server, including JOINs, subqueries, CASE expressions, common table expressions, and DDL statements like CREATE and ALTER. It is dialect-agnostic and formats by recognizing common keywords rather than enforcing one vendor's grammar.
What is the difference between Format and Minify?
Format expands the query with line breaks and indentation to make it readable, while Minify collapses it back to a single line and removes comments to make it compact. They are opposites — one is for reading, the other for embedding.
Will formatting change what my query does?
No. The tool only changes whitespace, line breaks, and keyword casing. It preserves your identifiers, string values, and the logical structure, so the formatted query is functionally identical to the original.
Are comments and string contents preserved?
When formatting, both single-line -- comments and multi-line /* */ comments are kept and placed on their own lines, and the contents of string literals are never reformatted. Minify, however, strips comments to produce the most compact output.
Is my SQL sent to a server?
No. All parsing and formatting runs locally in your browser using JavaScript, so your queries are never uploaded or stored anywhere and the tool works even offline once loaded.