SQL Formatter

Format SQL queries for readability.

About the SQL Formatter

Paste a query written on one line and get it back with each clause on its own line and its columns indented two spaces. Six dialects are available, so PostgreSQL casts, T-SQL bracket identifiers, MySQL backticks and BigQuery syntax are all parsed rather than mangled. Queries often carry table names and literal values from production, and this one is formatted in your browser instead of being posted to a server.

How to use the SQL Formatter

  1. Paste your query into the input panel, or press Sample to load an example.
  2. Choose the dialect that matches your database from the Dialect selector.
  3. Press Format.
  4. Press Copy for the clipboard, or Download to save formatted.sql.

Frequently asked questions

What does the formatted query look like?
Each keyword clause starts a line and its arguments are indented two spaces beneath it. select id,name from users where id=1 order by name becomes six lines: select, then id, and name indented, then from, users, where, id = 1, order by and name. Join conditions are indented under the join, and operators get spaces around them.
Does it uppercase my keywords?
No. Keyword case is left exactly as typed, so a lowercase select stays lowercase and a shouted SELECT stays shouted. Only whitespace, line breaks and spacing around operators change, which means the tool cannot be used to normalise a codebase to uppercase keywords.
Which dialects are supported?
Standard SQL, PostgreSQL, MySQL, SQLite, T-SQL and BigQuery. The choice matters for quoting and placeholders: [dbo].[t] parses under T-SQL, backticked identifiers under MySQL, and a ::text cast under PostgreSQL. Oracle and Snowflake are not on the list, so format those as Standard SQL and expect vendor-specific syntax to survive as plain tokens.
Why does my query fail with a parse error?
Almost always an unbalanced parenthesis or a truncated statement. select * from (select 1 reports a parse error naming the token and the line and column where the parser gave up, in that case column 24, at the end of the input. Close the parenthesis or paste the whole statement and it formats.
Are placeholders and comments preserved?
Yes. Numbered placeholders such as $1 and positional ? are left in place rather than treated as syntax errors, so a prepared statement can be formatted without substituting values first. Note that a syntactically wrong query can still format: select from where is laid out across three lines rather than rejected, because the formatter only needs to tokenise it.

Related tools