01 / Layout
Clauses on their own lines
SELECT lists, JOINs, WHERE conditions and GROUP BY each get their own line and indent.
Free tool
Paste a SQL query or open a .sql file and it is laid out as you type, coloured, with SELECT, FROM, JOIN and WHERE on their own lines. Pick from 20 dialects, the case of keywords, functions and data types, a standard or tabular layout, and where AND and OR go. A query it cannot read is marked on its line. One line squeezes it without touching quoted text. Your queries stay in your browser.
Need a custom tool, app or website? Contact us →This file is big, so it is kept out of the text box to keep the page quick. The result is on the right.
The formatted query shows here.
Paste on the left, open a .sql file, or press Example.
Case
Layout
Keys
Formatted in your browser with sql-formatter (MIT). Your queries are never uploaded or saved. Only your settings are remembered on this device.
Readable SQL
01 / Layout
SELECT lists, JOINs, WHERE conditions and GROUP BY each get their own line and indent.
02 / Dialects
Each knows its own keywords, quotes and functions, so formatting does not break the query.
03 / Private
Queries can hold table names and data; they are formatted in your browser.
Guide
A SQL formatter rewrites a query so each clause starts on its own line, the parts inside are indented, and keywords share one case. It changes only spaces, line breaks and the case of keywords, never what the query does. This one uses the open source sql-formatter library in your browser and knows the quirks of thirteen dialects, from MySQL and PostgreSQL to SQL Server, BigQuery and Snowflake.
Dialect tells the formatter which words are keywords and how names and strings are quoted. MySQL wraps names in backticks, SQL Server uses square brackets, PostgreSQL has :: casts and dollar quoted function bodies. With the wrong dialect, a query that runs fine can fail to format, or a word can be read as a keyword when it is really a column name.
Keywords, Function names and Data types change only the case of those words, such as SELECT, count() and VARCHAR. Table names, column names and text inside quotes stay as you typed them. Capitals for keywords are the most common house style because they make the structure easy to scan. Pick whatever your team already uses.
Style sets the layout. Standard puts each clause keyword on its own line and indents what follows. The two tabular styles put the keyword and its first item on the same line, with the keywords lined up in a column on the left or the right, which some teams find faster to scan.
AND and OR chooses whether a condition line starts with AND or ends with it. Starting the line with it makes it easy to comment out one condition.
Keep short parts on one line up to controls bracketed parts, such as an IN ('US','CA') list, a function call or a short subquery. If the part fits within 30, 50, 80 or 120 characters, it stays on one line. If it is longer, it is broken over several lines and indented.
If you paste several statements separated by semicolons, each one is formatted and they are kept apart with a blank line.
Once each clause has its own line, it helps to know that the database does not run them in the order they are written. This is the logical order, which explains a lot of confusing errors:
| Step | Clause | What happens |
|---|---|---|
| 1 | FROM and JOIN | The tables are combined into rows. |
| 2 | WHERE | Rows that do not match are dropped. |
| 3 | GROUP BY | The remaining rows are put into groups. |
| 4 | HAVING | Groups that do not match are dropped. |
| 5 | SELECT | The columns and totals are worked out. |
| 6 | ORDER BY | The result is sorted. |
| 7 | LIMIT or TOP | Only the first rows are kept. |
That is why you cannot use a column alias from SELECT inside WHERE in most databases: WHERE runs first, before the alias exists. It is also why the sample query repeats sum(i.price*i.qty) in HAVING instead of writing total. Some databases, such as MySQL, do accept the alias in HAVING, so check yours.
{{user_id}} or ${table} that are not SQL. Swap them for a plain value while you format, then put them back.' makes the rest of the query look like a string. Look near the point the error names.The result side says where it stopped, with the line and column, and shows the lines around it. The same line is marked in red in the input, and Show in the input puts the cursor there. The point marked is where the formatter gave up, so the real slip, such as a missing comma or bracket, is often just before it. If the query runs fine in your database, pick that database under Dialect. Put it on one line instead skips the layout and still gives you a tidy single line.
One line squeezes every run of spaces and line breaks into a single space and removes comments. Text inside quotes, quoted names and dollar quoted bodies are left exactly as they are, so '--not a comment' and 'two spaces' survive. Optimizer hints like /*+ INDEX(t) */ and MySQL's /*! */ comments are kept. Tick Keep comments, as /* */ to keep every comment, with -- comments turned into /* */ ones so they cannot swallow the rest of the line.
Files over 150 KB, such as a schema dump, are formatted in the background so the page stays quick, and the result is drawn a screen at a time. Files over 2 MB are kept out of the input box. Cmd+Enter formats, Cmd+Shift+Enter puts it on one line, Cmd+S downloads, Cmd+Shift+C copies and / jumps to search. On Windows, use Ctrl for Cmd. Your settings are remembered on this device; your queries are not.
o for orders and c for customers, and use them on every column.To compare two versions of a query line by line, use the diff checker. If you share queries in slides or posts, the code to image tool turns the formatted result into a clean picture.
Questions
Paste it. It is formatted as you type; pick your dialect for the best result.
No. Only spaces, line breaks and keyword case change.
Standard SQL, MySQL, MariaDB, PostgreSQL, SQL Server, SQLite, Oracle PL/SQL, BigQuery, Snowflake, Redshift, DuckDB, ClickHouse, Spark, Hive, Trino and Presto, IBM Db2, Db2 for i, SingleStore, TiDB and Couchbase N1QL.
Usually the dialect: some syntax only exists in one database. The error shows the line and column and marks it in the input. Pick your database, or look just before that point for a missing comma, bracket or quote.
No. It is formatted in this tab and never stored. Only your settings are remembered on this device.
Yes. Set Style to one of the tabular layouts, with the keywords on the left or the right of the column.
SQL does not care, so it is a style choice. Capitals make the clauses stand out from table and column names, which is why many style guides use them, but plenty of teams write lower case. Pick one and use it everywhere.
Yes. Paste them all, separated by semicolons, and each statement is formatted with a blank line between them.
No. It only lays the query out. It can spot some broken syntax, like an unclosed quote, but it does not know your tables or run the query, so a query can format cleanly and still fail in the database.
Because WHERE runs before SELECT, so the alias does not exist yet. Repeat the expression in WHERE, or wrap the query in a subquery and filter on the alias outside it.