SQL Query Formatter
Format SQL for Standard SQL, PostgreSQL, MySQL, SQLite or SQL Server, with keywords kept, upper-cased or lower-cased and 2-space, 4-space or tab indents. How it works
Use the SQL Query Formatter
Up to 50,000 characters. Formatting runs in this browser.
About the SQL Query Formatter
Paste SQL and it is formatted as you type: each clause, such as SELECT, FROM, WHERE or ORDER BY, starts a line and the columns and conditions under it are indented by 2 spaces. The SQL formatter uses the sql-formatter library with five dialects, Standard SQL, PostgreSQL, MySQL, SQLite and SQL Server, and can keep keywords as typed or change them to UPPERCASE or lowercase. It lays the text out and does not check that the query would run.
The sample query, formatted
| Line | Indent (spaces) | Keywords kept as typed | Keywords in UPPERCASE |
|---|---|---|---|
| 1 | 0 | select | SELECT |
| 2 | 2 | u.id, | u.id, |
| 3 | 2 | u.name, | u.name, |
| 4 | 2 | count(o.id) as orders | count(o.id) AS orders |
| 5 | 0 | from | FROM |
| 6 | 2 | users u | users u |
| 7 | 2 | left join orders o on o.user_id = u.id | LEFT JOIN orders o ON o.user_id = u.id |
| 8 | 0 | where | WHERE |
| 9 | 2 | u.active = 1 | u.active = 1 |
| 10 | 2 | and o.created_at > '2024-01-01' | AND o.created_at > '2024-01-01' |
| 11 | 0 | group by | GROUP BY |
| 12 | 2 | u.id, | u.id, |
| 13 | 2 | u.name | u.name |
| 14 | 0 | having | HAVING |
| 15 | 2 | count(o.id) > 2 | count(o.id) > 2 |
| 16 | 0 | order by | ORDER BY |
| 17 | 2 | orders desc | orders DESC |
| 18 | 0 | limit | LIMIT |
| 19 | 2 | 10; | 10; |
Standard SQL with a 2-space indent. The sample is the one-line query the page opens with. Function names such as count are not keywords and keep their case.
One query in each dialect, with UPPERCASE keywords
| Dialect | Input | Output (⏎ = new line) | Statements counted |
|---|---|---|---|
| PostgreSQL | select id, data->>'name' as name from users where created_at > now() - interval '7 days'; | SELECT ⏎ id, ⏎ data ->> 'name' AS name ⏎ FROM ⏎ users ⏎ WHERE ⏎ created_at > now() - interval '7 days'; | 1 |
| MySQL | select `id`, `name` from `users` where id = 1; | SELECT ⏎ `id`, ⏎ `name` ⏎ FROM ⏎ `users` ⏎ WHERE ⏎ id = 1; | 1 |
| SQL Server | select top 5 [id], [name] from [dbo].[users] order by [id]; | SELECT ⏎ TOP 5 [id], ⏎ [name] ⏎ FROM ⏎ [dbo].[users] ⏎ ORDER BY ⏎ [id]; | 1 |
| SQLite | insert into t (a, b) values (1, 'x'), (2, 'y'); | INSERT INTO ⏎ t (a, b) ⏎ VALUES ⏎ (1, 'x'), ⏎ (2, 'y'); | 1 |
| Standard SQL | select 'it''s; fine' as a; select 2; | SELECT ⏎ 'it''s; fine' AS a; ⏎ SELECT ⏎ 2; | 2 |
Indentation and blank lines are left out of this column. The semicolon inside the string it''s; fine does not end a statement.
Frequently asked questions
- Does the SQL formatter check my query for errors?
- No. It lays out what you paste: select from where comes back as three lines without complaint. It stops only at text it cannot split into tokens, such as an unclosed string, and then gives the position: select 'abc shows Syntax error at line 1, column 8, near "'abc".
- Which SQL dialect should I choose?
- The one your database uses, because quoting differs: [dbo].[users] is SQL Server, backticks around names are MySQL, and ->> is a PostgreSQL operator. Standard SQL is the default; when it cannot read your query, the error suggests picking a more specific dialect.
- How do I make SQL keywords uppercase?
- Set Keywords to UPPERCASE: select becomes SELECT, as becomes AS and left join becomes LEFT JOIN. Table, column and function names keep their case, so count(o.id) stays as written. lowercase does the reverse, and Keep as typed changes nothing.
- How long a query can it format?
- Up to 50,000 characters and 100 levels of nested parentheses; formatting time grows quickly with nesting. Formatting runs in a background worker so the page keeps responding, and a job that takes longer than 5 seconds is stopped with a message to split the query.
- How does it count statements?
- It counts the pieces between semicolons that hold something other than comments and spaces. A semicolon inside a string, a quoted name, a comment or a PostgreSQL dollar-quoted string does not count: select 'it''s; fine' as a; select 2; is 2 statements and select $$a;b$$ is 1.
- What do the Tabular layouts and Compact do?
- Layout moves the keywords. Tabular left writes each clause on one line with the keyword padded to 10 characters, so select u.id, u.name from users u gives select, four spaces and u.id on the first line and from, six spaces and users u on the last. Tabular right right-aligns the keywords so the names after them start in one column. Compact removes the spaces around operators, so u.active = 1 becomes u.active=1, and keeps a list or function call of up to 120 characters on one line; a bracketed group with AND or OR inside still breaks.
Related Tools
View all toolsCSS Minifier
Paste CSS to remove comments and unneeded whitespace while keeping descendant spaces, calc() operators and /*! */ license comments, with the characters saved.
JS Minifier
Paste JavaScript to remove comments and unneeded whitespace. Line breaks the code relies on and /*! license comments stay; variable names are not shortened.
HTML Minifier
Paste HTML to drop comments and collapse whitespace. The space between inline elements stays, pre and textarea are kept, and inline CSS and JS can be minified.
XML Formatter
Format or minify XML with 2 spaces, 4 spaces or tabs. Comments, CDATA, DOCTYPE, attribute values and mixed content stay as written, up to 300,000 characters.
SQL to JSON
Turn SQL INSERT statements, or a result table pasted from psql, MySQL or Markdown, into JSON. Quoted values stay strings; numbers, true and NULL get types.
JSON Path Query Tool
Test a JSONPath expression on your JSON and see the match count, each matching value and its path. Filters such as ?(@.price < 10) work. Up to 100 KB.