SQL Formatter
A SQL formatter puts each main clause on its own line. For example, "select a from t where x=1" becomes 3 lines: "SELECT a", "FROM t", "WHERE x = 1".
How to use
- Paste your SQL into the input box. A sample query is loaded to show the result.
- Choose whether keywords are upper case, lower case or left as typed, and pick 2 spaces, 4 spaces or a tab for indentation.
- Copy the formatted query from the result box. It updates as you type, and you can paste several statements separated by semicolons.
How it works
The tool splits your SQL into tokens: words, numbers, quoted strings, quoted identifiers, comments, operators and brackets. Text inside quotes and comments is never changed.
Main clauses such as SELECT, FROM, WHERE, GROUP BY, ORDER BY, HAVING, LIMIT, UNION, INSERT INTO, VALUES, UPDATE, SET and every JOIN start on a new line. When a SELECT or SET list has more than one item, each item goes on its own indented line. AND and OR inside WHERE, HAVING or ON are indented under the clause, except the AND in BETWEEN x AND y.
A bracket that opens a subquery starts a new indented block and the closing bracket returns to the original indent. CASE expressions put each WHEN and ELSE on its own line. Function calls keep the bracket attached to the name, so COUNT(*) stays together.
Keywords and common function names are changed to the case you choose. Names of tables and columns are left as you wrote them, but a column called like a keyword (for example key or index) is also changed. The tool does not check that the SQL is valid, and it does not follow one dialect, so unusual syntax may be spaced a little differently.
What starts a new line
| Element | Layout |
|---|---|
| SELECT, FROM, WHERE, GROUP BY, ORDER BY, HAVING, LIMIT | New line at the current indent |
| JOIN, LEFT JOIN, INNER JOIN and other joins | New line, condition after ON stays on the line |
| AND, OR in WHERE, HAVING, ON | New line, one level deeper |
| Select list with 2 or more items | One item per line |
| Subquery in brackets | Indented block |
| CASE ... WHEN ... END | One WHEN per line |
| Semicolon | Blank line before the next statement |
FAQ
Does it change what my query does?
No. Only spaces, line breaks and the case of keywords change. Strings, comments and identifiers in quotes are kept exactly. Run the formatted query to double check if it matters.
Which databases does it support?
It is not tied to one dialect. It handles standard SQL, MySQL, PostgreSQL and SQLite style queries, including backticks, double-quoted names, ::casts and $1 or :name placeholders. Vendor-specific blocks such as stored procedure bodies may not be laid out well.
Why is a single SELECT item on the same line?
When the list has only one item, a line break adds nothing. With two or more items, each goes on its own line.
What does the unterminated warning mean?
A quote or a block comment was opened but never closed. Everything after it is treated as part of that string or comment, so check for a missing quote.
Is my SQL sent anywhere?
No. Formatting happens in your browser, so queries with table names or data stay on your device.