Skip to content

SQL Formatter

Format SQL queries with consistent keyword casing and indentation.

Updated · Examples checked by automated tests

LOCAL WORKSPACENO UPLOAD REQUIRED

Formatted SQL

Paste a query on the left.

About SQL Formatter

A long query written on one line is almost impossible to review. Putting each major clause on its own line and indenting nested expressions turns the same query into something you can read top to bottom.

This formatter upper-cases keywords, breaks before the major clauses, indents parenthesised subqueries, and leaves your literals and comments untouched.

How SQL Formatter works in your browser

The formatter tokenises your statement and re-indents it around SQL’s structural keywords: SELECT, FROM, WHERE, GROUP BY, HAVING, ORDER BY and the JOIN family each start a new line, and the parenthesis depth of subqueries drives the indentation so nested queries visibly nest.

String literals and quoted identifiers are preserved untouched, so a keyword appearing inside a quoted string is never mistaken for a structural keyword and reformatted.

It is a formatter, not a parser or a linter: it will not tell you that a column does not exist or that a join is missing a condition. It changes only whitespace and keyword casing, so the statement it produces is the statement you gave it.

When to use it

  • Making a long generated query — from an ORM or a BI tool — readable enough to review.
  • Normalising formatting before committing a migration, so future diffs show logic changes rather than reflowed whitespace.
  • Understanding an inherited report query by seeing its subquery structure laid out.
  • Formatting a query containing real table names and business logic without sending it to a hosted formatter.

Worked examples

Format a one-line query

Settings: Format, 2 spaces

Input

select id, name from users where active = 1 and role = 'admin' order by name

Expected output

SELECT id, name
FROM users
WHERE active = 1
AND role = 'admin'
ORDER BY name

Keywords are uppercased and each clause starts a new line. The string literal is left exactly as written.

Minify a query for a log line

Settings: Minify

Input

SELECT id
FROM   users
-- only active
WHERE  active = 1

Expected output

SELECT id FROM users WHERE active = 1

Line breaks and repeated spaces collapse to single spaces, and the -- comment is removed, since on one line it would comment out everything after it.

Limits and checks before you use the result

  • The formatter does not validate SQL. A query with a syntax error is formatted, not rejected.
  • Vendor-specific syntax, such as PL/pgSQL blocks or T-SQL batches, is formatted as generic SQL and may need manual touch-ups.
  • Identifiers are not re-cased, so a quoted "UserId" stays as written.

Frequently asked questions

Which SQL dialects are supported?

The formatter is dialect-agnostic. It recognises standard clauses and operators shared by PostgreSQL, MySQL, SQL Server and SQLite.

Will it change my query?

No. Only whitespace and keyword casing change; string literals, identifiers and comments are preserved exactly.

Does it handle subqueries?

Yes. Nested parentheses increase the indentation level, so common table expressions and subqueries stay readable.

Related tools