How to format a SQL query
- Paste your query into the Input box. It can be a single statement or several separated by semicolons.
- Click Format to beautify it, or Minify to collapse it into a single line.
- Copy the result with the copy button and paste it back into your editor, migration or pull request.
Before and after
Formatting puts each clause on its own line, indents the columns and conditions under it with two spaces, and writes SQL keywords in uppercase so they stand out from table and column names.
-- Before
select u.id, u.email, 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.email order by orders desc
-- After
SELECT
u.id,
u.email,
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.email
ORDER BY
orders DESCWhy format SQL?
- Readability: long queries copied from logs, ORMs or monitoring tools arrive as one line. Formatting reveals the structure at a glance.
- Code review: a consistent layout makes changes in joins and conditions easy to spot in a diff.
- Debugging: with one condition per line, it is easier to find a missing join condition or a misplaced parenthesis.
- Consistency: the whole team gets the same style without arguing about it.
Format vs minify
Format is for reading and maintaining queries. Minify collapses whitespace into a single line, which is handy for pasting a query into a log search, a one-line config value or a command-line client.
Frequently asked questions
- Which SQL dialects are supported?
- The formatter uses standard SQL rules, which cover the everyday syntax of MySQL, MariaDB, PostgreSQL, SQL Server, SQLite and Oracle queries. Very dialect-specific syntax may not be recognized and can produce an error.
- Does the formatter check if my query is correct?
- No. It is a formatter, not a validator. It does not know your tables or columns and does not run the query. A query that formats cleanly can still fail when executed.
- Can formatting change what my query does?
- No. Formatting only changes whitespace and the case of SQL keywords. Table names, column names, string literals and values are left exactly as you wrote them.
- Are comments preserved?
- Yes. Line comments (--) and block comments (/* */) are kept when you format. Minify is a simple whitespace collapse, so remove line comments before minifying or the rest of the line will be commented out.
- Is my query sent to a server?
- No. Formatting happens in your browser, so queries containing table names, customer data or credentials never leave your device.
- Why are some function names not uppercased?
- Only reserved SQL keywords such as SELECT, FROM and JOIN are converted to uppercase. Function names like count or coalesce keep the case you wrote.