When you need a SQL beautifier
Queries often reach you in their worst form. You may copy them from:
- the slow query log or the general log of MySQL, or
pg_stat_statementsin PostgreSQL, - the query log of an ORM, such as Laravel, Hibernate, Django or Entity Framework, where the SQL is one long line,
- a string in code, where the query was joined from pieces,
- a monitoring tool or an error report that prints the SQL without line breaks,
- a minified file, or an export from a database tool.
The beautifier adds the line breaks and the indentation that these sources remove, so that you can see which tables are joined, which conditions filter the rows and how the result is grouped and sorted.
Example
with recent as (select user_id, total from orders where created_at >= '2026-01-01') select u.name, case when sum(r.total)>1000 then 'vip' else 'regular' end as tier from users u join recent r on r.user_id=u.id group by u.name;
becomes:
WITH recent AS (
SELECT
user_id,
total
FROM orders
WHERE created_at >= '2026-01-01'
)
SELECT
u.name,
CASE
WHEN SUM(r.total) > 1000 THEN 'vip'
ELSE 'regular'
END AS tier
FROM users u
JOIN recent r
ON r.user_id = u.id
GROUP BY u.name;
How to read a long query
- Beautify it first, so that each clause has its own line.
- Start at
FROMand theJOINlines to see which tables are used and how they connect. - Read
WHEREto see which rows are kept, thenGROUP BYandHAVINGto see how they are combined. - Last, read the
SELECTlist to see what is returned, andORDER BYandLIMITfor the order and the size of the result.
This is also the order in which the database logically works through a query, which is different from the order in which the clauses are written.
Beautifier or formatter?
They are the same thing with different names. This page is set up for the common case of an unreadable query, and the SQL formatter page has the same options and is meant for tidying queries that you write yourself. Both keep the meaning of the query and change only the layout.
Options
Choose the dialect (MySQL, PostgreSQL, SQL Server, SQLite, Oracle or Standard SQL), the indent (two spaces, four spaces or a tab), the keyword case, the comma position and whether AND / OR and CASE use several lines. Table names, column names, aliases and strings are never changed.
Check the result
If the result looks strange, the query itself usually has a problem, such as an unclosed quote or a missing parenthesis. The tool shows a warning for these. Queries copied from logs may also contain placeholders such as ?, $1 or :name. They are kept as they are.
See also the SQL minifier for the reverse, SQL pretty print for line numbers and colors, and the SQL formatting guide.