SQL Formatter and Beautifier
Part of Text tools: See all Text tools.
What is SQL Formatter?
SQL Formatter rewrites a query so it is easy to read: each main clause (SELECT, FROM, JOIN, WHERE, GROUP BY, ORDER BY and so on) starts on its own line, column lists and conditions are indented under it, subqueries and CTEs are nested, and keywords are set to the case you pick. It understands eleven dialects: Standard SQL, MySQL, MariaDB, PostgreSQL, SQL Server (T-SQL), Oracle PL/SQL, SQLite, BigQuery, Snowflake, Amazon Redshift and Spark SQL. Minify does the opposite and puts the query on one line with comments removed. Quoted strings, quoted names and comments are never changed by Format.
How to use SQL Formatter
- Paste one or more SQL statements into the box. Separate several statements with semicolons.
- Choose the dialect your database uses, the keyword case (UPPERCASE, lowercase or keep as typed) and the indent (2 spaces, 4 spaces or a tab).
- Click Format SQL, or Minify (one line) if you need a compact single line, for example to paste into a config value or a log search.
- Read the status line: it shows how many statements were formatted, or where the parser got stuck. Then use Copy result or Download .sql.
Why use this tool?
Queries copied from an ORM log, a BI tool or a teammate's message usually arrive as one long line, which hides how the joins and filters fit together. Putting every clause on its own line makes it quick to spot a missing join condition, an OR that needs brackets, or a filter in the wrong place, and it gives code reviews clean, line-by-line diffs. A consistent keyword case and indent also make saved queries easier to search and reuse.
SQL Formatter — In-Depth Guide
SQL is easy to write as one long line and hard to read that way. A query with three joins, a few filters and a GROUP BY can run to several hundred characters, and when it lands in a log file or a chat message the structure disappears. Formatting puts each clause on its own line and indents the parts that belong to it, so you can read the query from top to bottom: what is selected, from which tables, joined how, filtered by what, and grouped or sorted in which order.
The main settings are the dialect, the keyword case and the indent. The dialect tells the formatter which syntax to expect: PostgreSQL allows :: casts and $1 parameters, SQL Server uses [square brackets] for names and TOP instead of LIMIT, MySQL quotes names with backticks, and Oracle PL/SQL has its own keywords. Keyword case is a team convention; many style guides use uppercase keywords so they stand out from table and column names, while some teams prefer all lowercase. Two spaces is the most common indent, but match whatever your codebase already uses.
Two common bugs become obvious once a query is formatted. The first is mixing AND and OR without brackets: because AND binds tighter than OR, WHERE status = 'paid' OR status = 'shipped' AND country = 'US' does not mean what it looks like, and seeing each condition on its own line makes that easier to catch. The second is a LEFT JOIN that silently becomes an inner join because a filter on the joined table sits in the WHERE clause instead of the ON clause. Formatting does not fix either of these for you, but it makes them much easier to see.
Minify is useful when SQL has to fit on one line, such as a value in a YAML or JSON config, a command-line argument, or a search in a log tool. Because comments are removed and whitespace is collapsed, keep the formatted version as your source copy. To compare two versions of a query line by line, format both with the same settings and paste them into the Text Diff tool; for JSON or YAML files, use the JSON Formatter or YAML Formatter.
Frequently asked questions
- Which SQL dialects are supported?
- Eleven: Standard SQL, MySQL, MariaDB, PostgreSQL, SQL Server (T-SQL), Oracle PL/SQL, SQLite, Google BigQuery, Snowflake, Amazon Redshift and Spark SQL. Pick the one your database uses. Dialect matters for syntax such as PostgreSQL :: casts, T-SQL [bracketed names] and TOP, or MySQL backticks. If you get a parse error, switching to the right dialect usually fixes it.
- Does formatting change what my query does?
- It changes only line breaks, indentation and the case of keywords and data type names. Strings in quotes, quoted identifiers, numbers and comments are kept exactly as written, and function names keep the case you typed. Unquoted table and column names are not renamed. As with any tool, review the result before running it on a production database.
- Can I choose uppercase or lowercase keywords?
- Yes. UPPERCASE turns select into SELECT, lowercase does the reverse, and Keep as typed leaves every keyword alone. The same setting applies to data types such as varchar and int.
- Does it handle CTEs, subqueries, CASE and multiple statements?
- Yes. WITH clauses and subqueries are indented one level per nesting depth, CASE WHEN branches go on their own lines, and several statements separated by semicolons are formatted one after another with a blank line between them.
- What does Minify do?
- It puts the SQL on one line: comments are removed, runs of spaces and line breaks become a single space, and spaces next to commas and brackets are dropped. Text inside quotes, quoted names and optimizer hints such as /*+ INDEX(t idx) */ are kept exactly. In SQL Server mode a GO batch separator stays on its own line, and PostgreSQL $$ function bodies are left untouched.
- Why do I get a parse error?
- The formatter has to understand the query to indent it. An error usually means the dialect setting does not match the syntax (for example a :: cast in MySQL mode), a bracket or quote is not closed, or a template placeholder such as {{name}} is in the text. The message shows the line and column where it stopped. Placeholders like ?, :name and @name work in Standard SQL mode, and each dialect accepts its own style, such as $1 in PostgreSQL.
- Is my SQL uploaded or stored?
- No. Formatting and minifying run in your browser with the open-source sql-formatter library, which the page loads from a public CDN the first time you use the tool. Your query text is never sent to our server or anywhere else. Nothing touches any database.
Also try
Related tools that work well with this one: