SQL Formatter & Beautifier
Paste SQL and it is laid out on the spot: every major clause starts a new line, select lists get one column per line, subqueries and CASE branches are indented, and keywords come out in the case you choose. The example is 418 bytes of one-line SQL; formatted with 2 spaces it is 442 bytes over 18 lines, and minified it is 396 bytes. Only white space and the case of keywords change: strings, quoted names and comments are copied character for character, so the query means exactly what it did before. You can also minify to one line, remove comments, and copy or download the result. Your SQL never leaves the browser.
Updated Runs in your browser: nothing you enter is uploadedHow this tool works
Replace the example: paste or type SQL. It is formatted as you type.
Your SQL is processed on this device and never uploaded. It is not saved either: reload the page and it is gone.
Result
Formatted: 1 statement, 18 lines
Size and structure
- Before (UTF-8)
- 418 bytes
- After (UTF-8)
- 442 bytes
- Change
- +24 bytes (+5.7%)
- Statements
- 1
- Lines
- 18
- Tokens
- 119
- Comments
- 1
What each option produces
| Option | Result | Effect |
|---|---|---|
| 2 spaces | SELECT ⏎ a, ⏎ b ⏎ FROM t ⏎ WHERE x = 1 ⏎ AND y = 'z' | The default: one indent level is two spaces. |
| 4 spaces | SELECT ⏎ a, ⏎ b ⏎ FROM t ⏎ WHERE x = 1 ⏎ AND y = 'z' | One indent level is four spaces. |
| Tab | SELECT ⏎ →a, ⏎ →b ⏎ FROM t ⏎ WHERE x = 1 ⏎ →AND y = 'z' | One tab per indent level (shown as →). |
| Minify | SELECT a,b FROM t WHERE x=1 AND y='z' | One line, with a space only where two tokens would otherwise run together. |
| lowercase keywords | select ⏎ a, ⏎ b ⏎ from t ⏎ where x = 1 ⏎ and y = 'z' | Reserved words in lower case; names and strings are untouched. |
| As typed | Select ⏎ a, ⏎ b ⏎ From t ⏎ where x = 1 ⏎ and y = 'z' | Only the layout changes; every letter keeps its case. |
Where the line breaks go
| Part of the query | Input | Result | Rule |
|---|---|---|---|
| SELECT list | select a, b, c from t | SELECT ⏎ a, ⏎ b, ⏎ c ⏎ FROM t | With two or more columns the keyword stands alone and each column gets a line. A single column stays on the SELECT line. |
| FROM and JOIN | select * from a join b on a.x = b.x left join c on c.y = a.y | SELECT * ⏎ FROM a ⏎ JOIN b ON a.x = b.x ⏎ LEFT JOIN c ON c.y = a.y | FROM and every kind of JOIN start a line; the ON condition stays with its join. |
| WHERE and HAVING | select * from t where a = 1 and b = 2 or c = 3 | SELECT * ⏎ FROM t ⏎ WHERE a = 1 ⏎ AND b = 2 ⏎ OR c = 3 | The first condition follows the keyword; each AND and OR starts an indented line. BETWEEN … AND is kept together. |
| GROUP BY, ORDER BY, LIMIT | select a from t group by a order by a desc limit 5 offset 10 | SELECT a ⏎ FROM t ⏎ GROUP BY a ⏎ ORDER BY a DESC ⏎ LIMIT 5 ⏎ OFFSET 10 | Each starts a line; their lists stay on one line. |
| UNION, INTERSECT, EXCEPT | select a from t union all select b from u | SELECT a ⏎ FROM t ⏎ UNION ALL ⏎ SELECT b ⏎ FROM u | The set operator is on its own line between two statements. |
| Subqueries | select * from (select a from t) x where a in (select 1) | SELECT * ⏎ FROM ( ⏎ SELECT a ⏎ FROM t ⏎ ) x ⏎ WHERE a IN ( ⏎ SELECT 1 ⏎ ) | The inner query is indented one level inside its parentheses; the closing parenthesis returns to the outer level. |
| WITH (CTEs) | with a as (select 1), b as (select 2) select * from a, b | WITH a AS ( ⏎ SELECT 1 ⏎ ), ⏎ b AS ( ⏎ SELECT 2 ⏎ ) ⏎ SELECT * ⏎ FROM a, b | Each common table expression starts at the left edge and its query is indented. |
| CASE | select case when a = 1 then 'x' when a = 2 then 'y' else 'z' end from t | SELECT CASE ⏎ WHEN a = 1 THEN 'x' ⏎ WHEN a = 2 THEN 'y' ⏎ ELSE 'z' ⏎ END ⏎ FROM t | With two or more WHEN branches each branch starts a line; a one-branch CASE stays on one line. |
| INSERT … VALUES | insert into t (a, b) values (1, 2), (3, 4) | INSERT INTO t (a, b) ⏎ VALUES ⏎ (1, 2), ⏎ (3, 4) | Several rows go one per line; a single row stays beside VALUES. |
| UPDATE … SET | update t set a = 1, b = 2 where id = 3 | UPDATE t ⏎ SET ⏎ a = 1, ⏎ b = 2 ⏎ WHERE id = 3 | Several assignments go one per line. |
| CREATE TABLE | create table t (id int, name varchar(20)) | CREATE TABLE t ( ⏎ id INT, ⏎ name VARCHAR(20) ⏎ ) | The column list goes one definition per line. |
Text that is never touched
| Kind | You type | Copied unchanged |
|---|---|---|
| String with a doubled quote | select 'it''s -- /* here */' | 'it''s -- /* here */' |
| Double-quoted name | select "Order Total" from t | "Order Total" |
| Backtick name (MySQL) | select `my col` from t | `my col` |
| Bracketed name (SQL Server) | select [my col] from t | [my col] |
| Dollar-quoted string (PostgreSQL) | select $$a; b$$ | $$a; b$$ |
| Block comment | select 1 /* keep me */ | /* keep me */ |
| Line comment | select 1 -- keep me | -- keep me |
How to format SQL online
- Paste your SQL. Replace the example with your own query or script. It is formatted as you type; for very large input, press Format instead.
- Choose the layout. Pick 2 spaces, 4 spaces or a tab to indent, or Minify to put everything on one line.
- Choose the keyword case. Select UPPERCASE, lowercase or As typed. Names, strings and comments are never changed by this setting.
- Adjust the options. Tick “Remove comments” to strip them, or the MySQL backslash option if your strings use \' as an escape.
- Copy or download. Copy the result to the clipboard or download it as a .sql file. Nothing is uploaded at any step.
Frequently asked questions
- How do I format SQL?
- Paste it into the box. The formatter splits the text into words, numbers, strings, names, comments and symbols, then prints them again with each major clause on its own line, one column per line in a select list, and nested queries indented. Pick the indent and the keyword case with the options above the result.
- Can formatting change what my query does?
- It is built not to. Only the white space between tokens and the case of reserved words change. A space is never removed where it separates two tokens (a - -1 stays a - -1, never the comment a--1), and strings, quoted names and comments are copied character for character. The test suite checks that the tokens of the output equal those of the input for hundreds of random and hand-written queries.
- Which databases does it support?
- It is dialect-neutral: it follows the lexical rules shared by PostgreSQL, MySQL, SQL Server, SQLite, Oracle and others, and understands their common quoting styles (double quotes, backticks, [brackets], E'…' and $$…$$ strings, the :: cast, JSON arrows, and ?, :name, @name and $1 parameters). It lays out the statement’s shape; it does not parse the grammar of a particular database.
- Does it check my SQL for errors?
- No. It does not know your schema or your database’s grammar, so a typo in a column name or a missing keyword passes through unchanged. It does warn about the things that make layout unreliable: a string, quoted name or comment that is never closed, and unbalanced parentheses.
- What happens to my comments?
- They stay where they are, on the line they were on. When minifying, a -- comment keeps the line break that ends it, because the rest of the line would otherwise become part of the comment, so the result is not strictly one line in that case. “Remove comments” strips them, except optimizer hints (/*+ … */) and MySQL version comments (/*! … */), which change what a statement does.
- Which words are upper- or lower-cased?
- 115 reserved words (SELECT, FROM, JOIN, WHERE, CASE, NULL and so on) and 54 common function names such as COUNT and COALESCE, the latter only when followed by a parenthesis. Words that are often column names (name, date, status, text, year) are left alone, and so is anything after or before a dot (t.order), in quotes or in a comment.
- Why does my string with a backslash look wrong?
- In standard SQL and PostgreSQL a backslash inside '…' is an ordinary character, and the quote is escaped by doubling it (''). MySQL and MariaDB also accept \' by default. If your strings use backslash escapes, tick the MySQL option so the formatter reads them that way; strings written E'…' are always read with backslash escapes.
- How big a script can I paste?
- Up to 300,000 characters the result updates a moment after you stop typing; above that the page waits for you to press Format so typing stays smooth. Parentheses nested more than 100 levels deep are returned unchanged.
- Is my SQL uploaded or saved?
- No. Formatting happens in your browser, the text is not stored anywhere (not even in local storage) and it is gone when you close or reload the page. The only thing the site records, if you allow analytics, is that a button was used, never any of the text.
How it works
The text is read by a small tokenizer written for this tool. It recognises words, numbers (including hex and exponents), strings ('…' with doubled quotes, optional E, N, X and U& prefixes, and $tag$…$tag$), quoted names ("…", `…`, […]), -- and /* … */ comments, parameters (?, ?1, :name, @name, $1) and operators, and it keeps the exact characters of each. It never throws: an unterminated string or comment simply runs to the end of the text and is reported as a warning. The tokens are grouped by parentheses, and each group is printed again with the chosen indentation: a parenthesis that begins with SELECT, WITH or VALUES is a subquery and is indented; any other is kept inline, except the column list of CREATE TABLE. Spacing is decided per token pair: no space after ( or before ), no space around a dot or ::, a space on both sides of an operator, and a glued opening parenthesis stays glued after a function name. Minifying uses the opposite rule, removing a space only where the two neighbouring tokens cannot merge into one. Reserved words are matched case-insensitively against a fixed list. The result is checked in the tests by tokenizing the input and the output and comparing the token streams, with keyword case folded.
Sources
- PostgreSQL documentation: Lexical Structure (strings, dollar quoting, identifiers, comments, operators)
- MySQL reference manual: String Literals and Schema Object Names
- Microsoft Learn: Database identifiers (delimited identifiers in [brackets])
- SQLite documentation: SQL As Understood By SQLite (comments and literals)
More free tools
- XML Formatter & ValidatorCheck XML is well-formed with the exact line and column of any error, then pretty-print with 2 spaces, 4 spaces or a tab, or minify. Runs in your browser.
- CSS Minifier & BeautifierMinify CSS safely or beautify it with 2 or 4 space indents. Strings, url() and calc() stay intact. Shows bytes saved and gzip size. Runs in your browser.
- Regex TesterTest JavaScript regular expressions: highlighted matches, capture groups, replacements with $1 or $<name>. Stops runaway patterns. Runs in your browser.
- CSV to JSON ConverterConvert CSV to JSON in your browser: quoted fields, line breaks, any delimiter, and the exact line and column of an error. Converts JSON to CSV too.
- HTML Formatter & BeautifierBeautify messy HTML with 2 spaces, 4 spaces or tabs, or minify it. Script, style, pre and textarea content stays exactly as typed. Runs in your browser.
- JSON to TypeScript Interface GeneratorPaste JSON and get TypeScript interfaces or types: named nested interfaces, ? on optional keys, unions for mixed arrays. Runs in your browser.
More: Formatters and data convertersAll developer toolsAll toolsHome & DIY calculators