SQL Formatter
Format and beautify SQL queries
About SQL Formatter
SQL Formatter lays out a query with the open-source sql-formatter library, in your browser. It tokenizes the SQL for the dialect you pick, puts each clause (SELECT, FROM, WHERE, GROUP BY…) on its own line, indents columns and conditions by 2 spaces, and writes keywords in capitals. Text inside comments and string literals is not changed, and a comma inside a function call such as COUNT(a, b) or inside a string stays where it is. Twelve dialects are offered, from standard SQL to Db2. A query the dialect cannot parse gets the library's error message instead of output.
Example
Standard SQL: the comma inside the string literal does not start a new line.
Input
select id, count(*) as n from orders o join users u on u.id = o.user_id where o.status = 'paid, shipped' group by id
Output
SELECT id, count(*) AS n FROM orders o JOIN users u ON u.id = o.user_id WHERE o.status = 'paid, shipped' GROUP BY id
How to format a SQL query
- 1Choose your database in the dialect list; "Standard SQL" is selected by default.
- 2Paste the query into "Input".
- 3Click "Format".
- 4Click "Copy", or "Download" to save "formatted.sql".
Formats and limits
- Dialects
- Standard SQL, MySQL, MariaDB, PostgreSQL, SQL Server, Oracle PL/SQL, SQLite, BigQuery, Snowflake, Redshift, Spark, Db2
- Output
- SQL with uppercase keywords and 2-space indentation, saved as formatted.sql
- Preserved
- Comments, string literals, identifiers, and the case of function names
- Errors
- sql-formatter's own message, which can run to several lines
Where your query is processed
sql-formatter is a JavaScript library that the page downloads when you first click "Format"; your query is parsed and laid out in the browser and is not uploaded. When the library rejects a query, the error text shown is sent to our error log with quoted parts, long numbers, and addresses removed, along with the tool name and your browser's name and version.
Frequently asked questions
Can formatting break my query?
No. The formatter changes only whitespace and the case of keywords. Comments and string literals, including commas or quotes inside them, are copied as written, so the query returns the same result.
Which databases are supported?
12: Standard SQL, MySQL, MariaDB, PostgreSQL, SQL Server, Oracle PL/SQL, SQLite, BigQuery, Snowflake, Redshift, Spark, and Db2. Code Formatter uses the same engine with a longer list, adding ClickHouse, DuckDB, Trino, and Hive among others.
Does it work with any SQL syntax?
No. Each dialect has its own syntax, so T-SQL square brackets or PostgreSQL :: casts fail under Standard SQL, while MySQL backticks are accepted. Pick your database in the list and click "Format" again; the error message often suggests the same.
Tips and tricks
- ✓Function names keep the case you typed (count stays count); only keywords such as SELECT and GROUP BY are capitalized.