Skip to content

SQL Formatter

Paste or open SQL to lay it out one clause per line, indented, with keywords in the case you choose. Pick your dialect, from PostgreSQL to SQL Server and BigQuery, so its quotes and placeholders read right. Comments stay, errors show their line, and nothing is run.

Runs locally in your browser

More options

Input

3 lines · 483 characters
-- Top customers of the last 30 days
with recent as (select customer_id, sum(total) as spent, count(*) as order_count from orders where created_at >= current_date - interval '30' day and status <> 'cancelled' group by customer_id) select c.id, c.name, r.spent, r.order_count, case when r.spent >= 1000 then 'gold' when r.spent >= 250 then 'silver' else 'standard' end as tier from recent r join customers c on c.id = r.customer_id where c.country = ? order by r.spent desc limit 20;

Output

Formatted SQL appears here

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

  1. Paste SQL, or open or drop a .sql file.
  2. Choose the Dialect of the database the query is for. If Problems reports something your database accepts, select Try other dialects.
  3. 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.
  4. Copy the result, or download it as a .sql file.

Examples

A one-line query, one clause per line

Input

select id,name from users where active=1 order by name

Output

SELECT
  id,
  name
FROM
  users
WHERE
  active = 1
ORDER BY
  name
A PostgreSQL placeholder and comment stay as written

Input

select * from orders where customer_id = $1 -- by customer

Output

SELECT
  *
FROM
  orders
WHERE
  customer_id = $1 -- by customer
A string that is never closed

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:

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

Options
OptionValuesDefaultWhen to change it
DialectStandard 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 / PrestoStandard SQLTo 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 caseUPPER, lower, As writtenUPPERlower or As written to match your team's style; data types and built-in functions follow it too.
Indent2 spaces, 4 spaces, Tab2 spacesTo match your editor or the code the query lives in.
Identifier caseAs written, UPPER, lowerAs writtenUPPER or lower to make table and column names consistent; quoted names never change.
Expression width50 characters, 80 characters, 120 characters50 charactersWider to keep longer function calls and IN lists on one line.
AND / ORStart of line, End of lineStart of lineEnd of line if your style guide puts operators last.
Between statements1 blank line, 2 blank lines1 blank line2 blank lines to set statements further apart in long scripts.

Common errors and fixes

Common errors and how to fix them
CodeWhat it meansHow to fix it
SQL_PARSE_ERRORErrorThe 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_DIALECT_MISMATCHNoteAfter Try other dialects: the query formats in the dialects listed.Choose the one your database runs; several may format the same query.
SQL_DEPTH_LIMITErrorBrackets 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_TOO_LARGEErrorThe 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.

Privacy

SQL Formatter runs entirely in your browser. What you type, paste or open is processed on your device: it is not sent to Toolzami, and Toolzami does not store it. Only the options you choose are remembered, in this browser.

Frequently asked questions

Can it convert a query from MySQL to PostgreSQL?

No. The dialect you choose decides how the query is read and laid out, not what it says: quotes, placeholders and functions stay exactly as written, so backticks remain backticks. Moving a query to another database means rewriting that syntax, by hand or with a migration tool.

Can it format stored procedures?

Partly. Each statement inside a procedure, function or BEGIN … END block is laid out on its own, because the formatter splits the text at every semicolon, but the block itself gets no extra indentation. Only spacing and letter case change, so the procedure still does the same thing.

Can I format SQL copied from my code?

Yes, once it is plain SQL: copy the query without the quotes, plus signs or escapes of the programming language around it. Placeholders such as ?, $1 or :name are fine and stay as they are; choose the dialect of the database the code talks to.

Last updated