SQL Formatter

Format and beautify SQL code for dialects such as PostgreSQL and MySQL.

How to use the SQL formatter

  1. Pick the Dialect your database speaks, such as PostgreSQL, MySQL, SQL Server or BigQuery.
  2. Paste one or more SQL statements into the left box. The formatted query appears on the right as you type.
  3. Choose how keywords are printed: UPPER, lower or Preserve to keep your own casing.
  4. Set the indent to 2 spaces, 4 spaces or tabs, then press Copy to copy the result.

Example

This one-line query:

select name,email from users where active=true order by name

is formatted with each clause on its own line and its contents indented below it:

SELECT
  name,
  email
FROM
  users
WHERE
  active = TRUE
ORDER BY
  name

The formatter is sql-formatter, running in your browser. Your queries are never uploaded, so it is safe to paste SQL that names internal tables or contains real values.

Frequently asked questions

Why does the dialect matter?

Databases disagree on syntax. MySQL quotes identifiers with backticks, SQL Server uses square brackets andTOP, PostgreSQL has :: casts and dollar-quoted strings. Choosing the right dialect lets the formatter recognise those tokens instead of reporting an error. Standard SQL works for most portable queries.

Does formatting change what my query does?

No. Only whitespace, line breaks and keyword case change. Identifiers, string literals and comments are kept as written, so the formatted query runs exactly like the original.

What does a parse error mean?

The formatter could not make sense of a token, often an unclosed parenthesis or quote, or syntax from another dialect. The status line shows the line and column where it stopped, so you can check that spot first.