SQL formatter/beautifier

Format messy SQL into clean, indented, readable queries. Great for debugging, code reviews and documentation. Works with MySQL, PostgreSQL and more. Free.

Make SQL readable again

SQL copied from logs, ORMs, BI tools or a colleague's message often arrives as one long line: select u.id,u.name,count(o.id) from users u left join orders o on o.user_id=u.id where u.active=1 group by u.id order by 3 desc. The SQL formatter breaks it onto separate lines, puts each clause (SELECT, FROM, JOIN, WHERE, GROUP BY, ORDER BY) in its place and indents it so you can follow the logic.

Readable SQL is easier to debug, review and document. Spotting a missing join condition or a misplaced WHERE is much faster when the query is laid out properly.

It works with standard SQL as used by MySQL, MariaDB, PostgreSQL, SQLite, SQL Server and others.

How to use the sql formatter/beautifier

  1. Paste your SQL query.
  2. Submit to beautify it.
  3. Read or copy the formatted SQL.

Examples

A one-line report query

Input:

select u.id, u.name, count(o.id) as orders from users u left join orders o on o.user_id = u.id where u.active = 1 group by u.id, u.name order by orders desc limit 10

Output:

select
  u.id,
  u.name,
  count(o.id) as orders
from
  users u
  left join orders o on o.user_id = u.id
where
  u.active = 1
group by
  u.id,
  u.name
order by
  orders desc
limit
  10

Each clause gets its own line and each column is indented. Keywords keep the case you typed.

An update

Input: UPDATE products SET price = price * 1.1 WHERE category_id IN (3, 7);

Output:

UPDATE
  products
SET
  price = price * 1.1
WHERE
  category_id IN (3, 7);

Common uses

  • Reading queries captured from application logs or ORM debug output.
  • Preparing SQL for code reviews and pull requests.
  • Writing documentation and tutorials with clear examples.
  • Debugging long queries with many joins and subqueries.
  • Tidying queries before saving them in a BI tool or notebook.
  • Reading queries copied from application logs, where they're often on one line.

Tips and things to know

  • Formatting only changes whitespace. It doesn't validate the SQL or change results.
  • Remove real customer data and passwords from queries before pasting them anywhere online.
  • Pick a consistent style for your team, such as uppercase keywords and one column per line.
  • Change identifier styles with the case converter, and format JSON results with the JSON beautifier.

Edge cases and limitations

  • Keywords aren't uppercased. If your team style uses uppercase keywords, type them that way.
  • Several statements separated by semicolons are formatted one after another.
  • Comments are kept. Strings and quoted identifiers aren't changed.

Frequently asked questions

Does formatting change what my query does?

No. Only spacing and line breaks change, so the query returns the same results.

Which SQL dialects are supported?

Standard SQL keywords used by MySQL, PostgreSQL, SQLite, SQL Server and similar databases format well.

Does it check my SQL for errors?

No. It formats the text; run the query in your database to check for errors.

Should SQL keywords be uppercase?

It's not required, but uppercase keywords are a common convention that makes queries easier to scan.

Related tools

Popular free tools