What is SQL formatting?
SQL formatting reprints a query with each clause on its own line, subqueries and lists indented, and reserved words such as SELECT, FROM, and WHERE in capitals, without changing what the query does. This formatter reads SQL in 20 dialects, keeps strings, names, and comments exactly as written, minifies queries, and reports syntax errors by line and column. Everything runs in your browser.
Why readable SQL matters
A SQL formatter reprints a query with each clause on its own line and everything inside it indented, so a long statement is easy to read and review. When SELECT, FROM, JOIN, WHERE, GROUP BY, and ORDER BY each start a line, the shape of the query, and any clause that's missing or in the wrong place, is visible at a glance.
It helps most with the SQL that arrives as one dense line: queries generated by an ORM, copied from a log, or built up in application code.
What formatting changes, and what it doesn't
Formatting changes only whitespace (line breaks, indentation, and spacing) and the letter case of the dialect's reserved words, such as SELECT, FROM, and WHERE. Strings, quoted names, numbers, operators, and comments are kept exactly as you wrote them, and so are function names, data types, and your table and column names.
Every result is checked against your input: it must contain the same words, symbols, strings, and comments in the same order. If formatting would change anything else, the tool shows an error instead of a result. The only other change is to the indentation of lines inside a multi-line comment, which follows the query's indentation.
Whitespace that changes what SQL means
Two kinds of whitespace are part of the query, so they're kept as you typed them. In MySQL, a space before a bracket decides what a name is: count(i) calls the COUNT function, while CREATE TABLE count (i INT) creates a table named count. And in PostgreSQL and standard SQL, two string literals separated by a line break are joined into one string, which only works while the line break is there.
Choosing a dialect
Databases quote and comment SQL differently. MySQL quotes names with backticks and starts comments with #, PostgreSQL has dollar-quoted strings like $$ … $$, and SQL Server quotes names in [brackets] and names variables with @. Choose your database's dialect so these are read correctly and its reserved words are uppercased. Standard SQL, the default, suits queries that use only the common syntax.
The dialects are Standard SQL, Amazon Redshift, BigQuery, ClickHouse, Couchbase N1QL, DuckDB, Hive, IBM Db2, IBM Db2 for IBM i, MariaDB, MySQL, Oracle PL/SQL, PostgreSQL, SingleStoreDB, Snowflake, Spark SQL, SQL Server (Transact-SQL), SQLite, TiDB, and Trino.
Minifying SQL
Minify puts the query on as few lines as possible. It removes comments and every space that isn't needed: a space goes only where the words and symbols on either side would read differently without it. Spaces stay between two words, between two operators (databases read runs of operator characters in different ways), and where you typed one between a name and an opening bracket. Strings and quoted names are left exactly as written.
Comments that are code are kept: MySQL and MariaDB executable comments (/*! … */ and /*M! … */) and optimizer hints (/*+ … */). An Oracle hint written as a --+ comment is kept too, and ends its line, since the comment runs to the end of the line. Two string literals separated by a line break keep the line break.
DELIMITER, GO, and psql commands
Some lines in SQL scripts are commands for the client program, not SQL, and they only work alone on their line. The tool keeps them that way: DELIMITER (or \d) in the mysql client, for MySQL, MariaDB, SingleStoreDB, and TiDB; GO and GO with a count in sqlcmd and SQL Server Management Studio; and commands that start with a backslash in psql, for PostgreSQL. After DELIMITER, each statement ends at the new delimiter, as it does in the mysql client.
Errors and limits
An unclosed string, quoted name, or comment is reported with its line and column, and Go to error moves the cursor there. So is anything the formatter can't parse, such as a bracket that's never closed.
This is a formatter, not a validator: a query can format without errors and still fail on your database, because names, types, and many rules aren't checked. The formatter also doesn't parse every feature of every dialect; Snowflake stage references like @stage, for example, are reported as unexpected. Minify only needs to read the tokens, so it still works on those queries.
How to format a SQL query
- 1Choose the dialect. Pick your database in the Dialect menu, or keep Standard SQL for common syntax.
- 2Paste your SQL. Paste one or more statements into the input. The formatted SQL appears as you type; choose Minify instead to put it on as few lines as possible.
- 3Fix any errors. If the SQL can't be read, the error shows its line and column. Use Go to error to jump to it.
- 4Copy the result. Use Copy output to copy the SQL, or Share link to share it.
Examples
Format a joined query
Input
select u.name, count(o.id) as orders from users u join orders o on o.user_id = u.id where u.active = 1 group by u.name order by orders desc
Output
SELECT u.name, count(o.id) AS orders FROM users u JOIN orders o ON o.user_id = u.id WHERE u.active = 1 GROUP BY u.name ORDER BY orders DESC
Minify a formatted query
Input
SELECT id, email FROM users WHERE active = 1 AND created_at >= DATE '2024-01-01';
Output
SELECT id,email FROM users WHERE active=1 AND created_at>=DATE '2024-01-01';
Minify MySQL with an optimizer hint
Input
SELECT /*+ INDEX(o idx_status) */ o.id, o.total FROM orders o -- only open orders WHERE o.status = 'open';
Output
SELECT /*+ INDEX(o idx_status) */ o.id,o.total FROM orders o WHERE o.status='open';
Keep MySQL's space before a bracket
Input
create table count (i int); insert into count (i) values (1)
Output
CREATE TABLE count (i int); INSERT INTO count (i) VALUES (1)
Strings and quoted names stay as written (PostgreSQL)
Input
select 'it''s', 'a b', "Order Date" from t
Output
SELECT 'it''s', 'a b', "Order Date" FROM t
An unclosed string
Input
SELECT 'abc FROM t
Output
Line 1, column 8: Unclosed string.
Clause order in a SELECT
The order clauses are written in. Databases evaluate them in a different order: FROM and JOIN first, then WHERE, GROUP BY, HAVING, SELECT, ORDER BY, and the row limit last.
| Clause | Purpose |
|---|---|
| SELECT | Columns to return |
| FROM | Source tables |
| JOIN … ON | Combine related tables |
| WHERE | Filter rows |
| GROUP BY | Aggregate into groups |
| HAVING | Filter groups |
| ORDER BY | Sort the result |
| LIMIT or FETCH FIRST | Cap the number of rows: FETCH FIRST in standard SQL, LIMIT in MySQL, PostgreSQL, and SQLite, and TOP (after SELECT) in SQL Server |
Frequently asked questions
Does formatting change what my query does?
No. Only whitespace and the letter case of the dialect's reserved words change. Every result is checked against your input: it must contain the same words, symbols, strings, and comments in the same order, or the tool shows an error instead. Whitespace that matters, such as MySQL's space between a name and a bracket, is kept as you typed it.
Which SQL dialects are supported?
Twenty: Standard SQL, Amazon Redshift, BigQuery, ClickHouse, Couchbase N1QL, DuckDB, Hive, IBM Db2, IBM Db2 for IBM i, MariaDB, MySQL, Oracle PL/SQL, PostgreSQL, SingleStoreDB, Snowflake, Spark SQL, SQL Server (Transact-SQL), SQLite, TiDB, and Trino. The dialect decides how strings, quoted names, comments, and parameters are read, and which words are uppercased.
Will it uppercase my keywords?
Yes, the reserved words of the dialect you choose, such as SELECT, FROM, and WHERE. Function names and data types are left as written, because they can also be table and column names: in MySQL on Linux, for example, a table named text is a different table from TEXT.
Does it change my table or column names?
No. Names, quoted or not, are left exactly as written. Only reserved words, which can't be used as names without quotes, change case.
What does Minify remove?
Comments, line breaks, and every space that isn't needed to keep words and symbols apart. It keeps comments that are code (MySQL and MariaDB executable comments, /*! … */ and /*M! … */, and optimizer hints, /*+ … */) and leaves strings and quoted names exactly as written.
What happens if my SQL has an error?
The tool shows the error with its line and column and marks it in the editor; Go to error moves the cursor there. It reports unclosed strings, quoted names, and comments, and anything the formatter can't parse, such as a bracket that's never closed.
Does it check that my query is valid?
No. It's a formatter: it reads the query's structure but doesn't check names, types, or most rules, so a query can format cleanly and still fail on your database.
Does it handle DELIMITER, GO, and psql commands?
Yes. DELIMITER in the mysql client (for MySQL, MariaDB, SingleStoreDB, and TiDB), GO in SQL Server tools, and backslash commands in psql (for PostgreSQL) are kept on their own lines, as those programs require.
Which formatter does it use?
The open-source sql-formatter library (version 15.9.0), with this site's own checks that the result keeps every token of your query, and its own minifier.
Is my query private?
Yes. Formatting runs in your browser, so your SQL and any data in it aren't sent to a server. The only exception is the Share link button, which uploads the current output when you click it.
Is it free?
Yes — free with no limits.