SQL Formatter

Beautify SQL for MySQL, PostgreSQL, SQL Server, SQLite, BigQuery and more. Strings and comments stay untouched.

When you need a SQL formatter

SQL rarely arrives in a readable shape. ORMs log queries as one long line, BI tools export them with random line breaks, and a query copied from a Slack thread loses its indentation. Reading a 400-character SELECT to find a missing join condition is slow and error-prone. This formatter rewrites the layout of the query — one clause per line, indented column lists and conditions, consistent keyword case — without changing what it does.

It is built on the open-source sql-formatter library, which tokenizes the query according to the dialect you pick. That is the important difference from simple search-and-replace formatters: text inside quotes such as 'active -- not a comment' and real comments are recognised as such, so they are never split, uppercased or turned into code.

How to use it

  1. Pick your database in Dialect. If you are not sure, Standard SQL works for most simple queries.
  2. Paste one or more statements into SQL input, or press Load example to see a sample.
  3. Choose Keyword case, Indent and where AND / OR should go. The result updates as you type.
  4. Use Copy or Download .sql to take the Formatted SQL back to your editor, migration file or pull request.

Worked example

With Dialect = MySQL, Keyword case = UPPERCASE, Indent = 2 spaces and AND / OR at the start of the line, this input:

select id, name from users where status = 'active' and age > 30 order by name;

becomes:

SELECT
  id,
  name
FROM
  users
WHERE
  status = 'active'
  AND age > 30
ORDER BY
  name;

Each clause keyword starts a line, its contents are indented one level, and every selected column gets its own line — which makes diffs in code review much easier to read when a column is added or removed. The literal 'active' is untouched.

What the options do

  • Dialect — controls which keywords, quote styles and placeholders are understood: backtick identifiers in MySQL, [brackets] in SQL Server, $1 parameters and :: casts in PostgreSQL, @param in T-SQL and BigQuery.
  • Keyword case — UPPERCASE is the most common convention because it separates keywords from table and column names at a glance. Preserve leaves keywords exactly as you typed them. Identifiers are never re-cased.
  • Indent — 2 or 4 spaces, or a tab character, to match your editor or style guide.
  • AND / OR — "Start of line" puts each logical operator at the beginning of the condition it introduces (easy to comment out a single condition); "End of line" keeps the operator after the previous condition.

Limitations

  • It is a formatter, not a validator. A query can be formatted perfectly and still reference a table that does not exist.
  • Procedural code (stored procedures, PL/pgSQL function bodies, T-SQL control flow) is formatted on a best-effort basis; complex blocks may keep a layout you would arrange differently by hand.
  • If the parser cannot understand part of the input, nothing is output and an error with line and column is shown instead — the tool never returns a half-formatted query.
  • Input is limited to 500,000 characters to keep the page responsive. For huge dump files, use a command-line formatter.

Tips and common mistakes

  • Parse error on valid SQL? Check the dialect first. SELECT [x] FROM t fails as MySQL but formats correctly as SQL Server.
  • Remove secrets before sharing. Formatting happens locally, but once you paste the result into a ticket, any customer data in literals goes with it.
  • Format before diffing. Formatting both versions of a query with the same settings removes whitespace noise, so a diff shows only real changes.
  • Need INSERT statements from a spreadsheet? Generate them with the CSV to SQL converter, then format them here. For query results exported as JSON, use the JSON formatter.

Frequently Asked Questions

Which SQL dialects does this formatter support?

Standard SQL, MySQL, MariaDB, PostgreSQL, SQLite, SQL Server (T-SQL), Oracle PL/SQL, BigQuery, Snowflake, Redshift, Spark SQL, Trino/Presto, DuckDB, IBM Db2 and Hive. Choosing the right dialect matters because quoting, placeholders and keywords differ between databases.

Will the formatter change my string literals or comments?

No. The query is tokenized per dialect before formatting, so text inside quotes, quoted identifiers and -- or /* */ comments is copied through unchanged. Only whitespace and, if you choose, keyword case are changed.

Does it validate SQL syntax?

Only partially. If the formatter cannot parse the input (for example an unclosed string or bracket) it shows a parse error with the line and column. It does not check table names, column names or types — only a database can do that.

Why do I get a parse error for valid SQL?

Usually because the dialect is wrong: square-bracket identifiers like [Order] are T-SQL, backticks are MySQL, and $1 placeholders are PostgreSQL. Switch the Dialect selector to your database and try again.

Can I format several statements at once?

Yes. Paste a whole script; statements separated by semicolons are formatted one after another with a blank line between them.

Is my SQL query sent to a server?

No. The formatting library is downloaded to your browser and runs there. Your queries, including any data inside them, never leave your device.