SQL Formatter
Format SQL queries and scripts with one clause per line, indented joins, subqueries and conditions. Highlight the syntax, browse each statement as a tree, or compact it back to one line.
How to Use
- Paste a query or a whole script into the box, or drop a .sql file. The result updates as you type.
- Choose Format for indented text, Highlight for colour-coded SQL, Tree to browse each statement’s clauses, or Compact for one line per statement.
- Pick the keyword case (UPPERCASE, lowercase or as typed), the indent (2, 4 or 8 spaces, or a tab) and, if your team uses them, leading commas.
- Read the Show Work section to see which clauses were found and which line-break rules were applied.
- Press Copy result or Download .sql. Strings, quoted names and comments are never changed.
Worked Example
Format this 171-character query, copied from an application log:
select u.id, u.name, count(o.id) as order_count from users u left join orders o on o.user_id = u.id where u.active = true group by u.id order by order_count desc limit 50;
The tokenizer finds 47 tokens: 11 keywords, 21 names, 1 number and 14 symbols. Seven of the keywords start clauses: SELECT, FROM, LEFT JOIN, WHERE, GROUP BY, ORDER BY and LIMIT. Each clause starts a new line; the join is indented one level under FROM with its ON condition on the same line; the 2 commas in the SELECT list each start a new line, indented 2 spaces. With UPPERCASE keywords the 1 line becomes 9:
SELECT u.id, u.name, count(o.id) AS order_count FROM users u LEFT JOIN orders o ON o.user_id = u.id WHERE u.active = TRUE GROUP BY u.id ORDER BY order_count DESC LIMIT 50;
A subquery adds one level of indent and keeps its closing bracket on its own line, and an AND inside BETWEEN 1 AND 10 stays on its line because it is part of the range, not a new condition.
The common mistake: upper-casing keywords with a find-and-replace. A regular expression that turns every select into SELECT also rewrites WHERE status = 'select' to 'SELECT', and on a case-sensitive collation the query silently returns different rows. A tokenizer knows where each string starts and ends, so text inside quotes is never touched.
Show Work
Formatting Rules
Fifty Years of SQL
Edgar F. Codd’s 1970 paper “A Relational Model of Data for Large Shared Data Banks” described tables of rows queried by set operations. At IBM, Donald Chamberlin and Raymond Boyce designed a language for it, SEQUEL, published in 1974 and later shortened to SQL because the name was a trademark of another company. Relational Software, Inc. (later Oracle) shipped the first commercial SQL database, Oracle V2, in 1979.
SQL became an ANSI standard in 1986 and an ISO standard in 1987. Later editions added common table expressions and recursive WITH (SQL:1999), window functions (SQL:2003), JSON functions (SQL:2016) and property-graph queries (SQL:2023). Keywords are case-insensitive in the standard, which is why upper-case keywords are a convention rather than a rule — a habit from the days of fixed-width terminals and printed listings, kept because it makes the clause structure easy to scan.
About This Tool
This formatter reads SQL with a real tokenizer, so strings, quoted names, dollar-quoted bodies and comments are recognised before any line is broken. It then lays out each statement clause by clause, indents joins, conditions and subqueries, and applies your keyword case and indent. Highlight colours the result, Tree lists every statement with its clauses, and Compact puts each statement back on one line.
It replaces the separate SQL Viewer, whose highlighting, tab indent and comment handling are all here. Everything runs in your browser; queries and .sql files are never uploaded, so production table names and sample values stay on your machine.
Related tools: Database Viewer, JSON Formatter, and Minifier / Beautifier.
Frequently Asked Questions
Can formatting change what my query does?
No. Only the spaces, line breaks and the case of SQL keywords change. String literals, quoted names such as "Order Total" or `order`, comments and every identifier are copied exactly, so WHERE status = 'select' still compares against lower-case select. Function names such as count and now are identifiers too, so they keep the case you typed.
Should SQL keywords be UPPERCASE or lowercase?
It is a style choice with no effect on the result: every major database treats SELECT and select the same. Upper-case keywords are the long-standing convention because they stand out from table and column names; many teams using dbt or sqlfluff now prefer lowercase. Pick one, set it here, and the whole script follows it.
Does it check that my SQL is valid?
It formats rather than validates, but it does warn about the mistakes that break formatting: a string, quoted name or /* comment */ that is never closed (with its line number), and brackets that do not balance. For full validation run the query, or prefix it with EXPLAIN, against your database.
Which SQL dialects does it understand?
The tokenizer knows MySQL `backticks`, SQL Server [brackets] and @variables, PostgreSQL $$dollar quotes$$, ::casts, $1 parameters and JSON operators such as ->>, named :params, and both -- and /* */ comments. Stored-procedure bodies (PL/SQL, T-SQL blocks) are formatted as plain statements and may need a manual touch.
What do the Highlight and Tree views show?
Highlight colours keywords, strings, numbers and comments in the formatted SQL, as the old SQL Viewer did. Tree lists every statement as a collapsible entry with its clauses in order, joins indented and each subquery folded inside its parent: a 3-statement migration (INSERT, UPDATE, DELETE) shows 3 entries with 2, 3 and 2 clauses.
How do I use the SQL Formatter?
Just type your numbers. The answer shows up right away — there is no button to press. Change anything and it updates by itself.
Do I need to install or sign up for anything?
Not at all — it runs in the browser with nothing to install and no account. After it loads once, it even works without an internet connection.
Is my information private?
Yes. Everything happens in your browser. Nothing you type is sent to a server or saved anywhere.
Common Use Cases
ORM and log output
Hibernate, ActiveRecord and slow-query logs print SQL on one line. Paste a 171-character query and read it as 9 lines, one clause each.
Code review
Format before a pull request so a reviewer sees each JOIN and WHERE condition on its own line, and a diff shows only the condition that changed.
Migration scripts
Drop a .sql file and check every statement in Tree view: an INSERT with 2 rows, an UPDATE with 2 SET columns and a DELETE are listed separately.
Query tuning
Format a slow query before running EXPLAIN ANALYZE, so each line of the plan maps to one clause and nested subqueries stand out by indent.
Docs and tickets
Use Compact to put a 6-line query on one line for a log search or chat message, or Format to make an example readable in documentation.
Last updated: