Home / Developer tools / SQL formatter

SQL Formatter

Paste a SQL query and get it back clean and indented: one clause per line, aligned JOIN and WHERE conditions, nested subqueries, and your choice of keyword case and comma style.

Try:

Everything runs in your browser. Your SQL is not sent to our server.

What this SQL formatter does

The formatter takes a query that is crowded, on one line, or written in an inconsistent style, and lays it out so that you can read its structure at a glance. Keywords are written in the case you choose, each clause (SELECT, FROM, WHERE, GROUP BY, ORDER BY) starts on its own line, columns are listed one per line, JOIN and ON conditions are indented, and AND / OR conditions line up under the WHERE. Subqueries, CASE expressions, common table expressions (WITH) and CREATE TABLE column lists get their own indented blocks. It works for SELECT, INSERT, UPDATE, DELETE, CREATE, ALTER, DROP and UNION queries, and for files with many statements.

Before and after

A query as it often arrives, from a log or a code string:

select u.id,u.name,count(o.id) as orders,sum(o.total) as revenue from users u left join orders o on o.user_id=u.id and o.status='paid' where u.active=1 and (u.country in ('US','CA') or u.vip=1) group by u.id,u.name having count(o.id)>0 order by revenue desc limit 10;

The same query, formatted with the default options:

SELECT
  u.id,
  u.name,
  COUNT(o.id) AS orders,
  SUM(o.total) AS revenue
FROM users u
LEFT JOIN orders o
  ON o.user_id = u.id
  AND o.status = 'paid'
WHERE u.active = 1
  AND (u.country IN ('US', 'CA') OR u.vip = 1)
GROUP BY
  u.id,
  u.name
HAVING COUNT(o.id) > 0
ORDER BY revenue DESC
LIMIT 10;

Formatting options

  • SQL dialect. Choose Standard SQL, MySQL / MariaDB, PostgreSQL, SQL Server, SQLite or Oracle, so that comments, quoted names and strings are read the way your database reads them.
  • Indent. Two spaces, four spaces or a tab.
  • Keyword case. UPPERCASE, lowercase, or keep what you typed. Only keywords, data types and known function names change. Table names, column names, aliases and the text inside strings are never changed.
  • Comma position. Trailing commas (a,) or leading commas (, a). Leading commas make it easy to comment out one column and to see a missing comma.
  • Select list. Put every item on its own line, or keep lists on one line.
  • AND / OR on new lines, CASE on several lines and a blank line between statements can be switched on or off.

Supported dialects

DialectWhat it reads correctly
Standard SQLComments with -- and /* */, strings in single quotes, names in double quotes or backticks
MySQL / MariaDB# comments, backslash escapes in strings, strings in double quotes, backtick names
PostgreSQLDollar-quoted strings ($$ ... $$), :: casts, $1 parameters, -> and ->> JSON operators
SQL Server[bracket] names, @variables, TOP, CROSS APPLY
SQLiteBracket and backtick names, ? and :name parameters
Oracle:bind variables, VARCHAR2 and NUMBER types

What formatting changes, and what it does not

Formatting changes only white space, line breaks and the letter case of keywords. It does not reorder anything and it does not rewrite your query, so the result means exactly what the original meant. Comments are kept, and strings and quoted names are copied as they are. This is a formatter, not a validator: it does not know your tables or check that a query will run. It does warn you about an unclosed string, an unclosed comment and unbalanced parentheses, which are the usual cause of a query that looks wrong.

Limits

  • Procedural code, such as stored procedures with BEGIN ... END, loops and IF blocks, or SQL Server scripts with GO, is formatted statement by statement and token by token. Read the result before you use it.
  • Very unusual syntax can be laid out in a plain way. The result is still the same query.

Privacy

The formatting runs in your browser with plain JavaScript. Your query is not uploaded, stored or logged, so you can safely paste queries that contain table names or values from a real system. You may still want to remove real passwords or personal data before you share a query with other people.

Format SQL from the command line

pip install sqlparse
sqlformat --reindent --keywords upper query.sql

pg_format query.sql   # pgFormatter, for PostgreSQL

Other pages in this family: the SQL beautifier for messy or one-line queries, the SQL minifier, SQL pretty print with syntax colors, and the SQL formatting guide with style rules. For other languages, see the HTML formatter, CSS formatter, JavaScript formatter and YAML formatter.

Frequently asked questions

How do I format SQL online?

Paste your query into the box. It is formatted at once. Choose the dialect, indent size, keyword case and comma style, then press Copy or Download.

Does it work with MySQL, PostgreSQL and SQL Server?

Yes. Choose the dialect so that comments, quoted names, strings and variables are read correctly. SQLite and Oracle are included.

Will formatting change what my query does?

No. Only white space, line breaks and the letter case of keywords change. Strings, names and comments are kept as they are.

Is my SQL uploaded to a server?

No. The formatter runs in your browser, and nothing you paste leaves your device.

Can I use leading commas?

Yes. Set Comma position to Leading, and each new column starts with a comma on its own line.