SQL Formatter
Format · Beautify · Minify · 20 dialects
Paste SQL in the first box.

What the formatter does

Before

select c.name,count(o.id) orders,sum(o.total) as revenue from customers c left join orders o on o.customer_id=c.id and o.status='paid' where c.country in ('DE','FR') group by c.name having sum(o.total)>1000 order by revenue desc limit 20;

After

SELECT
  c.name,
  count(o.id) orders,
  sum(o.total) AS revenue
FROM
  customers c
  LEFT JOIN orders o ON o.customer_id = c.id
  AND o.status = 'paid'
WHERE
  c.country IN ('DE', 'FR')
GROUP BY
  c.name
HAVING
  sum(o.total) > 1000
ORDER BY
  revenue DESC
LIMIT
  20;
  • Every clause on its own line: SELECT, FROM, WHERE, GROUP BY, HAVING, ORDER BY and LIMIT start at the left, and what belongs to them is indented below.
  • One column per line in the select list, so a diff shows exactly which column changed.
  • Joins stay under FROM, and each AND / OR condition gets its own line.
  • Spaces around operators (=, >, ||) and after commas; short lists in brackets, such as IN ('DE', 'FR'), stay on one line.
  • Subqueries and CTEs (WITH ... AS (...)) are indented one more step, so you can see where each one starts and ends.
  • Nothing else changes: names, text in quotes, numbers and comments are kept exactly.

SQL style guide: the rules most teams use

RuleExampleWhy
Keywords in upper case, names in lower caseSELECT order_id FROM ordersKeywords and your own names look different, so long queries are easier to scan.
snake_case names, no spacescreated_at, not "Created At"Names with spaces or capitals need quotes in PostgreSQL and Oracle every time you use them.
Always write AS for column aliasessum(total) AS revenueWithout AS, a missing comma silently turns the next column into an alias.
Name the columns, avoid SELECT *SELECT id, nameThe query keeps working when someone adds a column, and it reads less data.
Explicit JOIN ... ONJOIN orders o ON o.customer_id = c.idOld comma joins (FROM a, b WHERE ...) make it easy to forget a condition and multiply rows.
Short, meaningful table aliasescustomers c, orders ot1, t2 force the reader to look up what each one is.
CTEs instead of deep nestingWITH paid AS (...) SELECT ...Each step has a name and can be read top to bottom.
End every statement with ;DELETE FROM t WHERE id = 7;Scripts with several statements need it, and some tools run only the current statement.

Leading commas (, name at the start of the line) and trailing commas are both common. Pick one and let a formatter apply it, so that code reviews are about logic and not about spaces.

SQL dialects: what is different

The formatter has to know your database, because the same characters mean different things. If you get a parse error, check this table first.

DialectQuoted namesParametersFirst rows
MySQL / MariaDB`order`?LIMIT 10
PostgreSQL"order"$1LIMIT 10
SQL Server (T-SQL)[order]@idSELECT TOP 10
Oracle PL/SQL"ORDER":idFETCH FIRST 10 ROWS ONLY
SQLite"order", `order` or [order]?, :id, @id, $idLIMIT 10
BigQuery`project.dataset.table`@idLIMIT 10
Snowflake"order"?, :1LIMIT 10

Your database is not in the list? Use the one it is based on: Aurora MySQL and PlanetScale use MySQL, Amazon RDS for PostgreSQL, Supabase and CockroachDB use PostgreSQL, Azure SQL and Synapse use SQL Server, Databricks uses Spark, and Athena uses Trino.

Format SQL in your editor and on the command line

ToolHow
DBeaverCtrl+Shift+F (Format SQL)
DataGrip and other JetBrains IDEsCtrl+Alt+L (Reformat Code)
Oracle SQL DeveloperCtrl+F7 (Format)
MySQL WorkbenchEdit > Format > Beautify Query
VS CodeInstall a SQL formatter extension, then Shift+Alt+F (Format Document)
SQL Server Management StudioNo built-in formatter; use an add-in such as Poor Man's T-SQL Formatter, or paste the query here
Node.js (the library this page uses)npx sql-formatter -l postgresql query.sql
Pythonsqlparse.format(sql, reindent=True, keyword_case='upper')
SQLFluff (lint + fix)sqlfluff fix --dialect postgres query.sql
pgFormatterpg_format query.sql

For a team, a formatter that runs in the editor or in CI (for example SQLFluff in a pre-commit hook) keeps every file in the same style. This page is for the quick cases: a query from a log, a colleague or an ORM that prints everything on one line.

Frequently asked questions

How do I format a SQL query?

Paste the query in the first box, or open a .sql file. Choose your database in "Dialect", for example MySQL or PostgreSQL. The formatted SQL appears at once in the second box, with every clause (SELECT, FROM, WHERE, GROUP BY ...) on its own line and the columns and conditions indented under it. Then copy it or download it as a .sql file.

Why does it say "Parse error"?

The formatter must understand the SQL to format it. A parse error usually means one of three things: the dialect is wrong (square brackets [name] are SQL Server, backticks `name` are MySQL, $1 parameters are PostgreSQL), a quote or bracket is not closed, or the text is not complete SQL, for example a template with {{ placeholders }}. Choose your database in "Dialect" first. "Show me" selects the place where the formatter stopped.

Does formatting change what my query does?

No. Only spaces, line breaks and the letter case of keywords change. Table names, column names, text in quotes, numbers and comments stay exactly as they are. SQL keywords are not case-sensitive, so SELECT and select mean the same. Function names keep their case unless you choose UPPER or lower under "Functions".

Should SQL keywords be upper case?

It is the most common style, and most style guides recommend it: upper-case keywords (SELECT, FROM, WHERE) stand out from lower-case table and column names, so a long query is easier to scan. The database does not care. Many teams now use lower case as well. The best choice is the one your team already uses, applied the same way everywhere.

How do I put a SQL query on one line?

Choose "Minify". All line breaks and extra spaces are removed and comments are deleted, so the whole statement fits on one line, for example to paste it into a log search, a JSON string or a command line. Text inside quotes is not changed. MySQL hints such as /*! ... */ and Oracle hints such as /*+ ... */ are kept, because they change how the query runs.

Which SQL dialects are supported?

Standard SQL, MySQL, MariaDB, PostgreSQL, SQL Server (T-SQL), Oracle PL/SQL, SQLite, BigQuery, Snowflake, Amazon Redshift, Spark SQL, Hive, Trino / Presto, IBM DB2, DB2 for IBM i, ClickHouse, DuckDB, TiDB, SingleStoreDB and Couchbase N1QL. If your database is not in the list, use the one it is based on: Aurora MySQL uses MySQL, Azure SQL uses SQL Server, Databricks uses Spark.

Is my SQL uploaded?

No. The formatting runs with JavaScript in your browser (the open-source sql-formatter library, MIT license), and nothing is sent to a server. You can safely format queries that contain table names, customer data or passwords. Only your settings, such as the dialect and indent, are kept in this browser for your next visit.

More developer tools