SQL Formatter & Beautifier - Format SQL Online
Format SQL queries in your browser
Queries copied out of logs or ORM debug output usually arrive on one line. This formatter puts each clause on its own line, indents joins and subqueries, and gives every selected column its own row. Paste a query on the left and the formatted version appears on the right as you type.
Choose your SQL dialect
MySQL quotes identifiers with backticks, SQL Server uses square brackets and @variables, and PostgreSQL has the
:: cast operator and $$ dollar-quoted strings. The parser only accepts the syntax of the dialect you
pick: select a::int from t is a parse error under Standard SQL and formats fine under PostgreSQL. If you are not
sure, start with Standard SQL and switch when you hit an error.
Keyword case and indentation
The keyword setting covers keywords, function names (count becomes COUNT) and data types. Preserve
leaves them as you typed them. Table and column names are never re-cased. Indentation can be two spaces, four spaces or a
tab.
How to use the SQL formatter
Paste a query (or use the Paste button), pick the dialect, then set keyword case and indent. The output updates on every change. Copy or Download takes the result back to your editor. A script with several statements separated by semicolons is formatted statement by statement with a blank line between each, which helps with migration files and seed scripts.
Example
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 (MySQL, uppercase keywords, 2 spaces):
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; Minify SQL to a single line
Minify does the reverse: one line, no comments. It removes -- and /* */ comments and collapses
whitespace, but leaves the inside of quoted strings and identifiers alone, so 'a -- b' survives intact.
Tips
- Commit SQL in formatted form. With one clause per line, a changed
WHEREcondition shows up as a one-line diff. - Placeholders follow the dialect:
$1under PostgreSQL,@idunder SQL Server,:nameunder Oracle.?works in most. - Need to tidy other code? Try the JavaScript beautifier or the XML formatter.
Related Tools
Epoch / Unix Timestamp Converter
Convert between Unix epoch timestamps and human-readable dates.
Regex Tester
Test regular expressions against text with match highlighting.
UUID Generator
Generate random v4 UUIDs for use in databases and APIs.
Colour Converter
Convert colours between HEX, RGB, and HSL formats.