SQL Formatter
Put each SQL clause on its own line, collapse it back to one line, or escape it as a quoted string literal. Literals and comments stay intact.
Still loading. If the tool doesn’t appear, reload this page.
Planned features and workflows
- AltF SQL Playground
Coming soon
About this tool
SQL Formatter
Format SQL queries with readable line breaks, configurable indentation and keyword case — entirely in your browser.
A SQL formatter rewrites the whitespace in a query so each clause starts on its own line and every list item is indented consistently, without changing a single token. This one tokenizes the statement using SQL:2016 lexical rules — single-quoted literals with '' doubling, double-quoted and backtick identifiers, -- line comments and /* */ blocks — then re-emits it with SELECT, FROM, WHERE, GROUP BY, HAVING and ORDER BY on their own lines and JOIN, AND and OR indented under them. The same tokens can be collapsed into a one-line version with comments dropped, or escaped as a single-quoted string literal with every quote doubled, for embedding in code. It runs entirely in your browser, so the query is never uploaded.
How to use SQL Formatter
Paste your query into the "SQL to format" box, which starts on a sample SELECT with JOIN, GROUP BY, HAVING and ORDER BY crammed onto single lines.
Set "Indent width (spaces)" to anything from 1 to 8 and "Keyword case" to UPPERCASE, lowercase or "Leave as typed".
The Formatted panel prints the clause-per-line query with Statements, SELECT clauses, Tokens and the minified length beside it; "Copy SQL" takes the formatted text and "Copy one-line" takes the One-line version, and "One-line saves" shows how many characters minifying removed.
Under "As a string literal", copy the one-line query wrapped in single quotes with every ' doubled to ''; tick "Also double backslashes" if the target is MySQL in its default mode.
Why use SQL Formatter
Nothing leaves the browser
The tokenizer runs client-side, so production SQL is not sent to a server.
Token-safe
String literals, quoted identifiers and comments are copied through byte-for-byte.
Configurable
1 to 8 spaces of indent, and keywords in UPPERCASE, lowercase or exactly as typed.
Three outputs from one paste
Clause-per-line, one-line and quoted string literal versions are produced together, each with its own copy button.
Common use cases
- Read a 900-character single-line query pulled out of an application log or an ORM's debug output
- Normalise indentation and keyword case before committing a migration so code review shows real changes, not whitespace
- Flip a formatted query back to one line to paste into a shell, a JSON payload or a ticket
- Embed a fixed query in application code or a dynamic-SQL statement as a quoted string literal without breaking it on an apostrophe
Frequently asked questions
Does formatting change what my query does?
No. The formatter only changes whitespace between tokens and, optionally, the case of reserved words such as SELECT and JOIN. It never touches the contents of a string literal, a quoted identifier or a comment, and it never reorders or removes a token, so the parsed statement is identical.
Link to answerIs my SQL uploaded anywhere?
No. Tokenizing and formatting happen in JavaScript in your own browser tab; there is no network request in the process. That matters because real queries usually contain table names, column names and sometimes literal values from production.
Link to answerWhich SQL dialects does it handle?
The lexer is dialect-neutral and covers the syntax shared by PostgreSQL, MySQL, SQL Server and SQLite: '' string escaping, double-quoted and backtick identifiers, square-bracket identifiers, the :: cast operator and the -> and ->> JSON operators. Because it is lexical rather than a parser, dialect-specific keywords it does not recognise are simply passed through unchanged.
Link to answerWhy is AND on a new line but BETWEEN … AND kept inline?
A leading AND at the start of a line makes the conditions in a WHERE clause countable at a glance. The AND that belongs to BETWEEN is part of a single predicate, not a new condition, so the formatter tracks it and keeps it on the same line as its BETWEEN.
Link to answerWhat does the string literal output do to my query?
It takes the one-line version, which already has comments removed, doubles every single quote to '' (the standard SQL escape) and wraps the result in single quotes. Backslashes are left alone unless you tick "Also double backslashes", because they are ordinary characters in standard SQL and PostgreSQL but an escape character in MySQL's default mode. Escaping is not a substitute for parameterised queries; use bound parameters for anything containing user input.
Link to answerWhy are comments removed from the one-line version?
A -- comment runs to the end of the line, so on a single line it would comment out everything after it. The tokenizer knows where each comment starts and ends, so it drops them without touching a -- or /* that sits inside a string literal.
Link to answer