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.
Reviewed by the thecalcu.com team · Last updated June 20, 2026
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.
Why Use a SQL Formatter?
SQL that arrives as a single line takes significantly longer to read than SQL laid out with one clause per line. When reviewing a colleague's pull request that touches a complex query, or debugging a query that returns wrong results, you cannot hold the logic of a 200-character single-line statement in your head all at once. Formatted SQL lets you read the WHERE clause independently of the JOIN, verify the GROUP BY columns against the SELECT list, and trace the order of operations.
Formatted SQL is also essential for version control. An ORM migration that changes one WHERE condition in a multi-clause query will produce a one-line diff if the SQL is compact, making it impossible to see what changed without careful character-by-character reading. After formatting, the change shows as a single modified line in the WHERE section, immediately visible.
Standardised keyword casing removes ambiguity about what is a SQL reserved word and what is a user-defined identifier. When keywords and table names share similar names (e.g. a status column versus the keyword SET), uppercase keywords make the distinction obvious.
Who Should Use This Formatter?
Database developers and DBAs reviewing and optimising queries will find the formatter essential for reading ORM-generated SQL. Most ORMs produce single-line queries with mixed-case keywords that are difficult to audit. Paste the generated SQL into the formatter to see its structure before analysing the execution plan.
Back-end developers debugging slow queries or incorrect results will find that formatted SQL reveals structural issues that are invisible in compressed form, for example, a condition placed in the HAVING clause instead of WHERE, or an ON clause that references the wrong table alias.
Data analysts writing ad-hoc reports in BI tools often build queries iteratively, copying fragments between tools. The formatter provides a clean, readable version for documentation and sharing with colleagues who need to understand the query's logic.
Students learning SQL benefit from seeing well-structured examples where clause boundaries are unambiguous. Formatted SQL is also what most SQL textbooks and tutorials show, so this tool helps bridge the gap between a working query and the style used in learning materials.
What Insights Does the SQL Formatter Give You?
Formatted SQL makes the following structural elements immediately visible:
- Which columns are in the SELECT list, each column reads clearly instead of being buried in a comma-separated run
- How many tables are joined and on what conditions, JOIN/ON pairs stand out as distinct blocks
- How many conditions are in the WHERE clause, AND/OR connectors are indented under WHERE, making the filter logic readable at a glance
- Whether GROUP BY and HAVING are used, placed on their own lines, these are easy to spot in a long query
- The sort order and row limit, ORDER BY and LIMIT are clearly separated from the data selection logic
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
Frequently Asked Questions