SQL Formatter
CodeFormat and beautify SQL queries with proper indentation and keyword casing. Runs entirely in your browser — your queries are never uploaded or stored anywhere.
What is a SQL?
The SQL Formatter restructures SQL queries by placing each major clause on its own line, adding consistent indentation under AND/OR conditions, and standardising keyword casing — transforming compact single-line queries into readable, structured statements that are easy to scan, review, and debug.
SQL is frequently written and transmitted as a single dense line. ORMs generate it that way, log files capture it that way, and developers under time pressure write it that way. The result is queries like:
select u.id, u.name, o.total from users u inner join orders o on u.id = o.user_id where o.total > 1000 and u.active = 1 order by o.total desc limit 10
That query has seven logical clauses — SELECT, FROM, INNER JOIN, ON, WHERE, ORDER BY, LIMIT — but none of them stand out when they are strung together on one line. The SQL Formatter turns this into a statement where each clause opens on its own line, AND conditions are indented to show they belong to the WHERE clause, and keywords are uppercase to distinguish SQL syntax from schema identifiers.
The formatter is dialect-agnostic and works with MySQL, PostgreSQL, SQLite, SQL Server, and Oracle SQL. It applies rule-based formatting rather than full grammar parsing, which means it handles the common clauses perfectly and does not crash on unfamiliar syntax. All formatting runs entirely in your browser — your queries are never sent to a server or stored anywhere.
Use the JSON Minifier for compacting JSON API payloads that accompany your SQL-backed services, or the JSON Formatter to inspect and debug JSON responses alongside formatted queries.
How to use this SQL calculator
- Paste your SQL query into the SQL Query input box — this can be a single query, a subquery, or a multi-statement script.
- Choose an Indent Size: 2 spaces for compact output, 4 spaces (default) for the most readable layout.
- Toggle Uppercase keywords on (default) to capitalise SQL reserved words, or off to keep them lowercase.
- The Formatted SQL output updates instantly as you type or change options.
- Click Copy to copy the formatted query to your clipboard.
Formula & Methodology
The formatter uses a single-pass regex replacement to insert newlines before major clause keywords. Multi-word keywords (GROUP BY,ORDER BY,LEFT JOIN,INNER JOIN, etc.) are matched before their single-word components to ensure the longer form is captured intact. Keywords that trigger a new line:SELECT,FROM,WHERE,JOIN(and all JOIN variants),ON,GROUP BY,ORDER BY,HAVING,LIMIT,OFFSET,UNION,UNION ALL,INTERSECT,EXCEPT,INSERT INTO,UPDATE,SET,VALUES,DELETE FROM,CREATE TABLE,ALTER TABLE,DROP TABLE.ANDandORare indented by the chosen indent size under the current clause, rather than starting a new top-level line. All whitespace is normalised (collapsed to single spaces) before formatting, so the output is consistent regardless of the original spacing. Before:sql select u.id, u.name, o.total from users u inner join orders o on u.id = o.user_id where o.total > 1000 and u.active = 1 order by o.total desc limit 10After (4 spaces, uppercase keywords):sql SELECT u.id, u.name, o.total FROM users u INNER JOIN orders o ON u.id = o.user_id WHERE o.total > 1000 AND u.active = 1 ORDER BY o.total desc LIMIT 10