My Tool Studio
Developer

SQL Formatter

This SQL formatter turns a long, single-line query into clean, indented SQL code that is easy to read and review. Paste a query or upload a .sql file, pick your database dialect and click Format. Each clause, such as SELECT, FROM, WHERE and GROUP BY, starts on its own line. You can set keyword case, indent, a standard or tabular layout, where AND and OR go, how many lines sit between queries and how long a list stays on one line. Remove comments strips -- and /* */ notes, and Minify to one line does the reverse job. Sixteen dialects are supported, including PostgreSQL, MySQL, SQL Server, Oracle, BigQuery, Snowflake and SQLite, and your last choice is remembered. Formatting happens in your browser.

Always freeNo sign upRuns in your browser
Output appears here...

How to use

01

Paste or upload your SQL

Paste one or more statements into the Input pane, or click Upload .sql. Try sample loads a commented, lowercase SELECT with a join, GROUP BY and HAVING, all on one line.

02

Choose dialect and style, then Format

Pick the Dialect your database uses and set Keywords, Indent, Layout and the other options. Click Format, or turn on Live mode. Minify to one line gives compact SQL without comments.

03

Copy or download the SQL

Click Copy above the output or Download .sql, and paste the SQL into your editor, migration file, ticket or pull request. If you see an error, check that the dialect matches the query.

Why SQL Formatter

Formatting SQL code for different databases

Pick the dialect that matches where the query runs. The formatter then understands that database's quoting style, parameter placeholders and special keywords, which avoids errors and odd line breaks. If you are unsure, Standard SQL works for most simple SELECT, INSERT, UPDATE and DELETE statements. Switch dialects and format again if a query fails.

Dialect optionUse it for
Standard SQLGeneric queries, or when you are not sure
PostgreSQLPostgreSQL, including :: casts and $1 style parameters
MySQLMySQL, with backtick-quoted names
MariaDBMariaDB
SQLiteSQLite databases, including many mobile and desktop apps
SQL Server (T-SQL)Microsoft SQL Server and Azure SQL, with [bracket] names
Oracle PL/SQLOracle Database
BigQueryGoogle BigQuery
SnowflakeSnowflake data warehouses
Amazon RedshiftRedshift data warehouses
Spark SQLApache Spark and Databricks SQL
Apache HiveHive and HiveQL on Hadoop
Trino / PrestoTrino, Presto and Amazon Athena
SingleStoreDBSingleStore (formerly MemSQL)
IBM Db2Db2
Couchbase N1QLCouchbase queries

Before and after: a messy query, formatted

The sample query arrives the way many queries do, copied from a log or a BI tool as one long lowercase line: select u.id, u.email, count(o.id) as orders from users u left join orders o on o.user_id = u.id where ... limit 10;

After Format with the default settings, SELECT, FROM, WHERE, GROUP BY, HAVING, ORDER BY and LIMIT each start a new line in capitals. Each selected column gets its own indented line, the LEFT JOIN sits indented under FROM with its ON condition, and the two GROUP BY columns are listed one per line. Nothing about the logic changes, but a reviewer can now see at a glance which table is joined, what is filtered and how the results are sorted. Switch Keywords to Keep as typed if your team prefers lowercase SQL.

Common questions

Which SQL dialects can this formatter handle?
Sixteen: Standard SQL, PostgreSQL, MySQL, MariaDB, SQLite, SQL Server (T-SQL), Oracle PL/SQL, BigQuery, Snowflake, Amazon Redshift, Spark SQL, Apache Hive, Trino / Presto, SingleStoreDB, IBM Db2 and Couchbase N1QL. Picking the right one matters because each treats quoting, parameters and some keywords differently.
Why does my query fail to format in one dialect but not another?
Because parsing is dialect-aware. Square-bracket names only make sense to T-SQL, backticks belong to MySQL and MariaDB, and PostgreSQL's :: cast is not part of standard SQL. If you get an error, first select the dialect the query was written for.
Does formatting SQL change what the query returns?
No. The formatter changes whitespace, line breaks and, if you choose, the case of keywords. SQL keywords are not case-sensitive, so results stay the same. It does not reorder joins, rewrite conditions or touch string literals.
Is it safe to format SQL with production table names online?
Here, yes, because nothing is uploaded. Parsing and formatting run entirely in your browser, so schema names, table names and literals stay on your machine. Still, remove real customer values from literals before sharing a query anywhere else.
Can the SQL formatter change keywords to uppercase?
Yes. Set Keywords to UPPERCASE, the default, to write SELECT, FROM and WHERE in capitals, or choose lowercase. Keep as typed leaves keywords exactly as you wrote them. Table and column names are not changed.
How wide is the indentation in formatted SQL?
Two spaces per level by default, and you can switch to 4 spaces or tabs with the Indent option. With Layout set to Standard, clauses start at the left edge and the items under them are indented one level; the tabular layouts line items up in a column after the keywords instead.
Can I format several SQL statements at once?
Yes. Paste them together, separated by semicolons, and they are formatted in one go with a blank line between statements.
What makes a formatted SQL query easier to review?
Each clause on its own line means a reviewer can comment on a single join or condition instead of the whole statement. Long AND and OR chains become easy to scan, and a missing join condition is much harder to overlook when every JOIN sits at the same indent.
Does the SQL formatter check that my query is correct?
No. It reads the query only well enough to lay it out and knows nothing about your tables. Serious syntax errors can stop formatting, but a misspelled column or missing table formats without complaint. Run the query, or EXPLAIN it, in your database to check it.
Can the SQL formatter minify a query to one line?
Yes. Click Minify to one line to collapse the query, remove -- and /* */ comments and tidy spaces around commas and brackets. Text inside quotes, backticks and PostgreSQL dollar quotes is never changed. It is handy for pasting SQL into a log search, a JSON string or a single-line config value.

More Developer tools

View all