SQL Formatter

Paste a long query or a whole script and get it back laid out for reading: each clause on its own line, one column per line, AND and OR conditions lined up, subqueries and CASE expressions indented, and keywords in the case you prefer. Strings, quoted names and comments are copied exactly, so the formatted query does the same thing as the original.

Action

Decides which quotes and comments are recognised.

SQL
Formatted SQL
Highlighted
SELECT
  o.id,
  c.name AS customer,
  sum(i.qty * i.price) AS total,
  count(*) items
FROM orders o
JOIN customers c
  ON c.id = o.customer_id
LEFT JOIN order_items i
  ON i.order_id = o.id
WHERE
  o.created_at >= '2024-04-01'
  AND o.status IN ('paid', 'shipped')
  AND c.state = 'MH' -- Maharashtra only
GROUP BY
  o.id,
  c.name
HAVING
  sum(i.qty * i.price) > 5000
ORDER BY
  total DESC
LIMIT 20;

Processing runs in your browser. What you type never leaves this device.

Features

  • Clauses, columns, joins and conditions on their own lines

  • Indented subqueries, CTEs and CASE expressions

  • PostgreSQL, MySQL, SQLite, SQL Server and standard SQL quoting

  • UPPER, lower or unchanged keywords

  • Highlighted output for keywords, strings, numbers and comments

  • Strings and comments never altered

When to format SQL

Reading generated queries

ORMs and BI tools log queries on one long line; format them to see what they do.

Code review

Agree on one layout so reviews show logic changes, not whitespace.

Debugging slow reports

Spot a missing join condition or a misplaced OR once each condition has its own line.

How the formatter lays out SQL

Layout rules

  • SELECT, FROM, WHERE, GROUP BY, HAVING, ORDER BY and LIMIT start new lines.
  • Each selected column, GROUP BY and ORDER BY item goes on its own line (you can turn that off).
  • JOIN conditions after ON and every AND / OR in WHERE get their own indented line; BETWEEN ... AND stays together.
  • Statements are separated by a blank line.

Dialects

The dialect decides what counts as a string or name: backticks and # comments for MySQL, [brackets] for SQL Server, $$ dollar quotes for PostgreSQL. Choose yours so they are kept whole.

Related tools

Collapse a query onto one line with the SQL minifier.

SQL Formatter - FAQ