Skip to main content
CalcChef

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

221 chars | 1 statement

Up to 50,000 characters. Formatting runs in this browser.

Formatted SQL
select u.id, u.name, count(o.id) as orders from users u left join orders o on o.user_id = u.id where u.active = 1 and o.created_at > '2024-01-01' group by u.id, u.name having count(o.id) > 2 order by orders desc limit 10;

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

LineIndent (spaces)Keywords kept as typedKeywords in UPPERCASE
10selectSELECT
22u.id,u.id,
32u.name,u.name,
42count(o.id) as orderscount(o.id) AS orders
50fromFROM
62users uusers u
72left join orders o on o.user_id = u.idLEFT JOIN orders o ON o.user_id = u.id
80whereWHERE
92u.active = 1u.active = 1
102and o.created_at > '2024-01-01'AND o.created_at > '2024-01-01'
110group byGROUP BY
122u.id,u.id,
132u.nameu.name
140havingHAVING
152count(o.id) > 2count(o.id) > 2
160order byORDER BY
172orders descorders DESC
180limitLIMIT
19210;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

DialectInputOutput (⏎ = new line)Statements counted
PostgreSQLselect 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
MySQLselect `id`, `name` from `users` where id = 1;SELECT ⏎ `id`, ⏎ `name` ⏎ FROM ⏎ `users` ⏎ WHERE ⏎ id = 1;1
SQL Serverselect top 5 [id], [name] from [dbo].[users] order by [id];SELECT ⏎ TOP 5 [id], ⏎ [name] ⏎ FROM ⏎ [dbo].[users] ⏎ ORDER BY ⏎ [id];1
SQLiteinsert into t (a, b) values (1, 'x'), (2, 'y');INSERT INTO ⏎ t (a, b) ⏎ VALUES ⏎ (1, 'x'), ⏎ (2, 'y');1
Standard SQLselect '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 tools