What the SQL formatter does
The formatter lays out SQL the way most style guides do: every clause on a line of its own, what belongs to it indented below, and keywords in one case. It first reads the query with the rules of the dialect you choose, from Standard SQL to PostgreSQL, MySQL, SQL Server, Oracle, BigQuery and fourteen more, so their quotes, placeholders and comments are understood rather than broken apart. Only whitespace and letter case change. If the text cannot be read, every problem points to its line and column, and nothing is ever run or sent anywhere.
How to format SQL
- Paste SQL, or open or drop a
.sqlfile. - Choose the Dialect of the database the query is for. If Problems reports something your database accepts, select Try other dialects.
- Set Keyword case and Indent. More options has the case of names, the width of expressions kept on one line, where AND and OR go, and the blank lines between statements.
- Copy the result, or download it as a
.sqlfile.
Examples
Input
select id,name from users where active=1 order by name
Output
SELECT
id,
name
FROM
users
WHERE
active = 1
ORDER BY
name
Input
select * from orders where customer_id = $1 -- by customer
Output
SELECT
*
FROM
orders
WHERE
customer_id = $1 -- by customer
Input
select 'abc from t
Problem reported
Line 1, column 8: This string is never closed with '. Add ' where the string ends. A ' inside the string is written twice: ''. (SQL_PARSE_ERROR)
Choosing the dialect
SQL is a standard, but every database adds its own small print, and a formatter has to get it right before it can lay out anything: how names and strings are quoted, what a placeholder looks like, and which characters start a comment. The ones you meet most:
- PostgreSQL quotes names in
"double quotes", numbers its placeholders$1,$2, and has$$dollar-quoted$$strings for function bodies (lexical structure (opens in a new tab)). - MySQL and MariaDB quote names in
`backticks`, use?placeholders, accept\'inside strings and start a comment with#too (identifiers (opens in a new tab)). - SQL Server quotes names in
[brackets]or double quotes, and names its parameters@id(identifiers (opens in a new tab)). - Oracle binds parameters as
:nameand hasq'[…]'strings that need no doubled quotes inside (names (opens in a new tab)). - BigQuery writes table paths in backticks, like
`project.dataset.table`, takes@nameand?parameters, and escapes a quote in a string only with a backslash (lexical structure (opens in a new tab)). - SQLite accepts names in double quotes, backticks or brackets, and parameters as
?,?1,:name,@nameor$name(keywords and quoting (opens in a new tab)).
Standard SQL reads most portable queries. When the formatter stops at something your database accepts, such as $1, [name] or #, the dialect is the likely cause. Try other dialects then formats the query with PostgreSQL, MySQL, SQL Server, BigQuery and SQLite, and offers each one that reads it. Often several can, since formatting checks so little; choose the database the query actually runs on.
How the layout reads
Clause keywords such as SELECT, FROM, WHERE and GROUP BY start a line, and each column, table and condition under them gets a line of its own, so a diff shows exactly which one changed. Keyword case UPPER makes keywords stand out from names; lower suits teams that write everything in lower case, and As written changes no letter. It covers data types such as VARCHAR and built-in functions such as COUNT as well, while table and column names change only with Identifier case, and never inside quotes.
A bracketed expression, like a function call or an IN list, stays on one line while it fits in the Expression width, and otherwise breaks into one item per line. With AND / OR at the start of the line, a condition and its operator come and go together, which is why it is the default.
Formatting is not validating
The formatter reads a query only as far as laying it out needs: words, quotes, brackets and the order of clauses. It does not know your tables, columns or types, does not check that a function exists, and never runs anything, so a query can format cleanly and still fail in the database. What it does catch are mistakes that make the text unreadable: a string, quoted name or comment that never closes, a bracket or CASE without its end, and characters the dialect has no use for. To read the JSON or timestamps a query returns, the JSON Formatter and the Unix Timestamp Converter take over.
Comments and placeholders
Comments stay where they are, in all their forms: -- to the end of the line, /* … */ blocks, and # lines in MySQL, MariaDB and BigQuery. One thing moves: a comment written after a statement's semicolon belongs to the next statement, so it goes to the line above that one.
Placeholders of prepared statements, such as ?, $1, :name and @name, are never filled in or renamed. A query taken from code keeps its parameters, and the formatted version can go back into the code as it is. Strings stay exactly as written too, even when they hold a document; XML kept in a column is laid out by the XML Formatter.
Options
| Option | Values | Default | When to change it |
|---|---|---|---|
| Dialect | Standard SQL, BigQuery, ClickHouse, Db2, Db2 for IBM i, DuckDB, Hive, MariaDB, MySQL, N1QL (Couchbase), Oracle PL/SQL, PostgreSQL, Redshift, SingleStoreDB, Snowflake, Spark, SQLite, TiDB, SQL Server (T-SQL), Trino / Presto | Standard SQL | To the database the query runs on, when it uses that database's quotes, placeholders or comments. A dialect downloads the first time you choose it. |
| Keyword case | UPPER, lower, As written | UPPER | lower or As written to match your team's style; data types and built-in functions follow it too. |
| Indent | 2 spaces, 4 spaces, Tab | 2 spaces | To match your editor or the code the query lives in. |
| Identifier case | As written, UPPER, lower | As written | UPPER or lower to make table and column names consistent; quoted names never change. |
| Expression width | 50 characters, 80 characters, 120 characters | 50 characters | Wider to keep longer function calls and IN lists on one line. |
| AND / OR | Start of line, End of line | Start of line | End of line if your style guide puts operators last. |
| Between statements | 1 blank line, 2 blank lines | 1 blank line | 2 blank lines to set statements further apart in long scripts. |
Common errors and fixes
| Code | What it means | How to fix it |
|---|---|---|
SQL_Error | The query cannot be read in the chosen dialect: a string, quoted name or comment that never closes, a bracket or CASE without its end, or a character the dialect does not use. | Fix the place the message points to. If your database accepts it, choose its dialect or select Try other dialects. |
SQL_Note | After Try other dialects: the query formats in the dialects listed. | Choose the one your database runs; several may format the same query. |
SQL_Error | Brackets are nested more than 512 deep, or CASE expressions deeper than the browser can follow: laying them out takes too long. | Split the query into smaller ones, for example with a WITH clause. |
INPUT_Error | The SQL is over 1 MB, the most the formatter lays out in a few seconds. | Format the file in parts, a few statements at a time. |