SQL Beautifier
Break a long SQL query onto one line per clause, with JOIN, ON, AND and OR lined up. String literals and comments are left untouched. Runs in your browser.
The interactive tool loads here when JavaScript runs.
About this toolWhat is the SQL Beautifier?
The SQL Beautifier takes a query that arrived as one long line, from an ORM log or a monitoring tool, and breaks it onto separate lines at major clauses such as SELECT, FROM, JOIN, WHERE, GROUP BY and ORDER BY so it can be read and reviewed. AND, OR and ON conditions are indented under their clause.
It is a fast, keyword-based formatter rather than a full SQL parser, so it works across dialects without configuration. String literals, quoted identifiers, PostgreSQL dollar-quoted bodies and comments are set aside before formatting and put back unchanged, so the text inside them is never altered.
Clause keywords are upper-cased; everything else keeps its original case. It does not indent subqueries or CTE bodies by depth, and BETWEEN x AND y is split at the AND like any other condition.
- Line breaks at major SQL clauses, joins and conditions
- String literals, quoted identifiers and comments preserved exactly
- Works with any SQL dialect, no configuration
- One-click copy
- Runs in your browser; queries are not uploaded
Updated
How to use the SQL Beautifier
- Paste your SQL into the input box.
- Click Format SQL.
- Read or copy the formatted query.
- Use Clear All to start over.
Worked examples
A query from an ORM log
Input
select o.id, c.name from orders o left join customers c on c.id = o.customer_id where o.status = 'shipped' and o.total > 100 order by o.id desc limit 20
Output
SELECT o.id, c.name FROM orders o LEFT JOIN customers c ON c.id = o.customer_id WHERE o.status = 'shipped' AND o.total > 100 ORDER BY o.id desc LIMIT 20
Each clause gets its own line and the join condition sits under its join, so a missing ON or a filter in the wrong place is easy to see.
Literals and comments are left alone
Input
select a -- pick a from t where name = 'Tom and Jerry'
Output
SELECT a -- pick a FROM t WHERE name = 'Tom and Jerry'
The double spaces and the word "and" inside the string are untouched, and the line comment still ends before FROM instead of commenting out the rest of the query.
Background and insight
SQL was designed at IBM in the 1970s by Donald Chamberlin and Raymond Boyce, originally as SEQUEL, and became an ANSI standard in 1986. It was meant to read like English, which is why a well-formatted query is easy to follow and a poorly formatted one is not.
Most SQL style guides, including Simon Holywell's widely shared sqlstyle.guide, agree on a few conventions: each major clause starts on a new line, keywords are written consistently (often upper case), and join conditions sit next to the joins they belong to. None of it changes what the database does. It changes how quickly a reviewer can spot a missing join condition or a filter in the wrong place.
Formatting matters most in two situations: code review, where a clean diff shows the one line that changed, and incident response, where you are reading a slow query out of a log under time pressure.
Common use cases
Reading ORM-generated queries
Make a long query from Hibernate, ActiveRecord, Prisma or Django logs readable.
Preparing queries for review
Format a query before pasting it into a pull request or ticket.
Investigating slow queries
Lay out a query from a slow-query log so you can see its joins and filters before running EXPLAIN.
Cleaning up ad hoc SQL
Tidy a query written in a hurry in a database console before saving it to a runbook.
Tips and limitations
- Log lines often replace parameters with ? or $1. Format first, then substitute values, so placeholders stay easy to spot.
- Nested subqueries are not indented by depth. For deeply nested SQL, format, then indent the inner SELECT by hand.
- MySQL # comments are not recognized as comments; use -- or /* */ if a comment contains SQL keywords.
- For a whole migration folder, use a parser-based formatter (sqlfluff, pgFormatter, or your IDE) in CI so style stays consistent.
- After formatting, run the query through EXPLAIN in a non-production database to see what it actually does.
Sources and further reading
- SQL style guide, Simon Holywell. The widely shared conventions for clause layout and keyword case
- Early History of SQL, IEEE Annals of the History of Computing. Donald Chamberlin's account of SEQUEL at IBM
- PostgreSQL documentation: 4.1. Lexical Structure, PostgreSQL Global Development Group. Why keyword case and whitespace do not change a query's meaning
Frequently asked questions
Does formatting change what my SQL query does?
No. The formatter only changes whitespace between tokens and the case of clause keywords. String literals, quoted identifiers and comments are protected and restored exactly, and line comments still end their own line, so the database sees an equivalent statement.
Which SQL dialects does the beautifier support?
Formatting is keyword-based, so it works on the SQL used by PostgreSQL, MySQL, SQL Server, SQLite, Oracle and others without configuration. Dialect-specific syntax is passed through as written; it is simply not given its own line breaks.
Does the SQL beautifier validate my syntax?
No. It formats text and does not parse the full grammar, check table names or run the query. A query with a typo will still be formatted. Run it with EXPLAIN against a development database to check that it is valid and to see its plan.
Should SQL keywords be written in upper case?
Databases do not care, because SQL keywords are case-insensitive. Upper-case keywords are a common convention because they make the structure of a query stand out from table and column names, and this formatter upper-cases clause keywords for that reason.
Can I format several SQL statements at once?
Yes. Paste a script with several statements separated by semicolons and each one is broken onto clause lines, with a blank line between statements. For whole migration files, a parser-based formatter in your editor or CI keeps style consistent across the codebase.
Is there an in-depth guide to the SQL Beautifier?
Yes. “SQL Formatting Conventions That Make Queries Easier to Review” covers the background, how it works, common pitfalls and worked examples. Read the full guide.
Related Tools You May Like
Free, in-browser tools that pair well with this one.