SQL prettify and format

Format and prettify your SQL queries online (it supports various SQL dialects).

loading SQL prettify and format
About this tool

What is an SQL formatter?

An SQL formatter rewrites a query so a person can read it: one clause per line, columns listed one under the other, joins and conditions aligned, keywords in a consistent case. The query itself does not change. Databases ignore white space, so a statement squeezed on one line and the same statement spread over twenty lines run identically; the second is just far easier to review, debug and discuss.

You open one when an ORM or a log hands you a long statement on a single line, when a query copied from a ticket has no structure, or before you commit a migration so that the diff in the pull request shows real changes instead of spacing noise. A formatted query also exposes mistakes: a missing join condition, a stray OR in a WHERE clause, a column selected twice.

This tool does the opposite too. Switch the output to one line and it collapses a formatted query into a single line, which is what you need to put a statement in a string, a shell command or a JSON field. Both directions run in your browser; the query you paste is not uploaded or saved, only the display options are remembered. It follows the grammar of the dialect you pick, because TOP 10 in SQL Server, LIMIT 10 in MySQL and FETCH FIRST in Oracle are not interchangeable. For other formats, see the JSON formatter and the YAML formatter.

How to format an SQL query online

  1. Paste your query in the input box. The page opens with a short query already loaded, so you see a result before typing anything; the clear button empties the box and the example button brings the query back.
  2. Pick the dialect that matches your database: BigQuery, DB2, Hive, MariaDB, MySQL, Couchbase N1QL, Oracle PL/SQL, PostgreSQL, Redshift, Spark, standard SQL, SQLite or SQL Server Transact-SQL. Standard SQL is the default.
  3. Choose the keyword case: uppercase, lowercase or kept as typed. Uppercase is the most common convention in shared code.
  4. Choose the indent style: standard, which puts each clause on its own line and indents what belongs to it, or one of the two tabular styles, which align the keywords in a column to the left or to the right.
  5. Read the formatted query in the output panel, which highlights the SQL. It updates a moment after you stop typing, so long queries do not slow the keyboard.
  6. Press the copy button. To get the query on a single line instead, change the output option from formatted to one line.

Your choices are remembered for the next visit. To see exactly what the formatter changed, paste both versions in the text diff.

Examples

The default example, formatted

Input:

select field1,field2,field3 from my_table where my_condition;

Output, with the default options (standard SQL, uppercase, standard indent):

SELECT
  field1,
  field2,
  field3
FROM
  my_table
WHERE
  my_condition;

A report query with a join, a filter and an aggregate

Input, on one line: select u.id,u.name,count(o.id) as orders from users u left join orders o on o.user_id=u.id where u.active=true and o.created_at>='2024-01-01' group by u.id,u.name having count(o.id)>3 order by orders limit 10;

Output:

SELECT
  u.id,
  u.name,
  COUNT(o.id) AS orders
FROM
  users u
  LEFT JOIN orders o ON o.user_id = u.id
WHERE
  u.active = TRUE
  AND o.created_at >= '2024-01-01'
GROUP BY
  u.id,
  u.name
HAVING
  COUNT(o.id) > 3
ORDER BY
  orders
LIMIT
  10;

PostgreSQL, lowercase keywords

Dialect PostgreSQL and lowercase. The :: cast and the $1 parameter are understood:

select
  id::text,
  nome
from
  clientes
where
  id = any ($1)
  and created_at > now() - interval '7 days'

Tabular, left

The same short query with the tabular left style:

SELECT    a,
          b
FROM      t
WHERE     x = 1;

One line

Output set to one line. Line comments become block comments, because a -- comment left on a one-line query would comment out everything after it. Text inside quotes is untouched:

SELECT id, name /* the user */ FROM users WHERE note = 'a  b
 c' AND id > 5;

Common errors and how to fix them

  • Could not format: unexpected [Name] fro at line 1, column 15: the query uses a syntax of another dialect. Square brackets around names are SQL Server; with MySQL selected they fail. Change the dialect to match the database.
  • Could not format: the query ends unexpectedly at line 1, column 37: something is not closed. For select * from users where id in (1,2 it is the parenthesis. Close it, or finish the clause you were writing.
  • Could not format: unexpected 'Ana at line 1, column 34: a quote was opened and never closed in where name = 'Ana. Close it with the same kind of quote.
  • Could not format: unexpected ) at line 1, column 23: an extra closing parenthesis, as in select a from t where ) x.

Frequently asked questions

How do I format SQL online?

Paste the query, choose your dialect and keyword case, and copy the result. Nothing has to be installed and the query stays in your browser.

Does formatting change what the query does?

No. Only white space and the case of keywords change, and the keyword case can be left as you typed it. Strings and numbers are not rewritten. One thing to know: a word that is only a column or alias name, such as data or name, keeps the case you typed; only real keywords change. The database ignores the difference.

Which SQL dialects are supported?

BigQuery, DB2, Hive, MariaDB, MySQL, Couchbase N1QL, Oracle PL/SQL, PostgreSQL, Redshift, Spark, standard SQL, SQLite and SQL Server Transact-SQL. Pick the one that matches your database so that its own syntax is understood.

Can I turn a formatted query back into one line?

Yes. Set the output to one line. It is useful to put the statement into code, a command line or a JSON value.

How to format SQL in code

JavaScript

The sql-formatter package is the library behind many editors.

import { format } from 'sql-formatter';

const sql = 'select id,name from users where active=true;';
console.log(format(sql, { language: 'postgresql', keywordCase: 'upper' }));

Python

sqlparse is the usual choice.

import sqlparse

sql = "select id,name from users where active=true;"
print(sqlparse.format(sql, reindent=True, keyword_case="upper"))

Bash

sql-formatter also ships a command-line tool.

npx sql-formatter --language postgresql query.sql
cat query.sql | npx sql-formatter --language mysql
Report a problem