Why format SQL?
SQL queries copied from application logs, ORMs or reports often arrive as one long line. Long queries with several joins and conditions are hard to read, debug and review in that state. Formatting puts every clause on its own line and indents the details, so you can see at a glance which tables are joined, which filters apply and how the results are grouped and sorted.
How to format a query
- Paste your query into the box. Several statements separated by semicolons are fine.
- Choose the indentation and whether keywords should be UPPERCASE, lowercase or left as typed.
- Click Format SQL. Use Compact to one line when you need to paste a query into code or a command line.
How the formatter lays out your query
- Main clauses such as
SELECT,FROM,WHERE,GROUP BY,HAVINGandORDER BYstart a new line, and their contents are indented underneath. - Each selected column goes on its own line after its comma, which makes it easy to add or comment out a column.
- Every
JOINstarts a new line and itsONcondition is indented below it. ANDandORconditions are placed on separate lines, except theANDinsideBETWEEN … AND ….- Subqueries in brackets are indented one extra level.
- Text in quotes and comments is never changed.
Example
SELECT
u.id,
COUNT(o.id) AS orders
FROM
users u
LEFT JOIN orders o
ON o.user_id = u.id
WHERE
u.active = 1
Frequently asked questions
Which SQL dialects are supported?
The formatter works with standard SQL used by MySQL, PostgreSQL, SQL Server, SQLite and Oracle. It formats by keywords and does not need to understand every vendor-specific feature.
Does it check my SQL for errors?
No. It only changes spacing and keyword case. Run the query in your database to check that it is correct.
Is it safe to paste production queries?
Yes. Formatting happens in your browser and the query is not sent anywhere.