Saved

Press the star next to a tool's name to save it.

All my tools

SQL Formatter and Minifier

Paste a query into this SQL formatter and get it back with clean line breaks, indentation and consistent keyword case. Pick a dialect such as MySQL, PostgreSQL or SQL Server, or switch to Minify to strip comments and spaces.

Pick the dialect that matches your database so its keywords and quoting are recognised. Minify never changes text inside quotes.

Your code stays in your browser. Nothing is uploaded.

Result

Paste your input and press Run.

A SQL formatter takes a query that was written on one line, or pasted from a log, and lays it out so the structure is visible. Select lists, joins, conditions and subqueries each get their own lines and indentation. This SQL formatter also works the other way round: choose Minify and it squeezes a query down for embedding in code or a config file. Both modes run in your browser.

How the SQL formatter works

The formatting is done by the open source sql-formatter library, which pretty-prints SQL by adjusting white space. [1] You choose the dialect, the indent and the keyword case, paste your query and press Run. The result appears in a read-only box you can copy with one click, with a line count underneath.

Because it only changes white space and, optionally, the case of keywords, the statement means the same thing before and after. Identifiers, string values and numbers are never rewritten. That makes it safe to use on queries you plan to commit, review or paste into a ticket.

Choosing a dialect

SQL is not one language. MySQL quotes names with backticks, SQL Server uses square brackets, PostgreSQL has dollar-quoted strings and Snowflake, BigQuery and Trino each add keywords of their own. The library supports BigQuery, Db2, Hive, MariaDB, MySQL, PostgreSQL, Redshift, Snowflake, Spark, SQL Server, Trino and Oracle PL/SQL among others. [1] This tool offers the common ones in a drop-down. If the output looks odd or you see an error, the first thing to try is a different dialect.

Standard SQL is the safe default for plain SELECT, INSERT, UPDATE and DELETE statements. Use a specific dialect when your query contains vendor features such as window frame extras, array types or procedural blocks.

Indent and keyword case

The library lets you set the indentation width, switch to tabs and convert keywords to upper or lower case. [1] Two spaces suits most teams and keeps deep queries narrow. Four spaces or tabs are common in older style guides. Upper-case keywords make clauses stand out from table and column names, while lower case matches many modern code bases. Keep as typed leaves your case alone.

Minify SQL safely

Minify mode is our own routine, written for this page. It walks through the text once and removes line comments that start with two dashes and block comments between slash-star markers. It then collapses runs of white space. It copies single-quoted strings, double-quoted names, backtick names and, for the matching dialects, square-bracket names and dollar-quoted blocks without touching them.

Spaces are only removed around commas, brackets, semicolons and equals signs. A space between two words is always kept, so SELECT a FROM t never becomes SELECTaFROMt. In MySQL, MariaDB, BigQuery, Spark and Hive a backslash inside a string escapes the next character, so the minifier honours that when you pick one of those dialects. Comments that begin with a plus sign or an exclamation mark are kept because databases read them as optimizer hints or version-specific code.

After minifying, the page shows how many bytes were saved. Savings are modest for short queries and larger for generated SQL with heavy indentation and comments.

Worked example

Paste select id,name from users where active=1 order by name, choose MySQL and Format. You get SELECT on its own line with id and name indented below it, then FROM users, WHERE active = 1 and ORDER BY name, each clause starting a new line. Switch to Minify with the same text plus a comment and extra blank lines and you get a single tight line with the comment gone.

When to format and when to minify

  • Format SQL before code review so reviewers can see joins and filters at a glance.
  • Format queries copied from logs or ORM output to understand what actually ran.
  • Minify SQL that you embed in a string in application code or a shell script.
  • Keep comments in the version you store; minify only the copy you ship.

Common mistakes

  • Choosing the wrong dialect, which can mis-read quoted names or procedural syntax.
  • Expecting the tool to fix invalid SQL. It formats text; it does not check that tables or columns exist.
  • Minifying SQL that relies on a newline, such as a line comment at the end of a query that you then join onto another line. This minifier removes comments, so that case is safe, but other tools may not.
  • Pasting several statements with different dialect features in one go.

Privacy

Everything happens on your device. The formatting library is downloaded with the page and your queries are never uploaded, which matters when they contain customer names, internal table names or literal values.

To check that a statement parses, try the SQL validator. To compare two versions of a query after formatting both, a text diff makes the changes easy to see. If your query comes out of a JSON payload, run the payload through a JSON formatter first so you can read the embedded text.

Tips for readable SQL

  • Put one condition per line in long WHERE clauses; the formatter does this for AND and OR.
  • Alias tables with short, meaningful names so joins stay readable.
  • Use the same keyword case across a project so diffs stay small.
  • Format before you diff, so differences show real changes rather than white space.

What next

Use this often? Add SQL Formatte to your Home screen.

Frequently asked questions

What does a SQL formatter do?

It rewrites a query with line breaks and indentation so the clauses are easy to read. The statement does the same work; only the white space and, if you choose, the keyword case change.

Which SQL dialects does this SQL formatter support?

Standard SQL, MySQL, MariaDB, PostgreSQL, SQLite, SQL Server (T-SQL), BigQuery, Snowflake, Redshift, Oracle PL/SQL, IBM Db2, Spark, Hive and Trino. Choose the one that matches your database.

Does minifying SQL change what the query does?

No. Minify removes comments and extra white space and leaves quoted strings and names exactly as written. Optimizer hints in comments that start with a plus sign or an exclamation mark are kept.

Is my SQL sent to a server?

No. Formatting and minifying run in your browser, so queries with table names or values never leave your device.

Why does it show a parse error?

The formatter reads the query as tokens. An unclosed quote, a stray bracket or a keyword from a different dialect can stop it. Check the dialect setting first.

What went wrong?

We only receive the reason you pick, never what you typed into the tool.

Thanks for your feedback.

More Developer Tools