SQL Formatter · QueryTidy
Turn long SQL statements into something readable, with clause highlighting.
Updated:
Input SQL
Formatted SQL
What the SQL formatter does
QueryTidy is a SQL formatter: it takes a database query written as one long, hard-to-read line and rewrites it with each clause (SELECT, FROM, WHERE, JOIN...) on its own line and consistent indentation, so you — or whoever reviews your code — can actually follow its structure. SQL is the language used to ask a database for data; this tool doesn't run the query, it just makes the text easier to read.
How to use the SQL formatter
- Paste your SQL into the Input SQL panel, or click Example to try a sample query.
- Choose 2 or 4 spaces of indentation, and toggle Uppercase to control whether keywords like SELECT/WHERE are capitalized.
- Read the formatted, clause-by-clause result in the Formatted SQL panel on the right.
- Click Copy to copy it, or Download to save it as a file.
How SQL formatting works
QueryTidy tokenizes the query respecting strings and comments, and places each major clause (SELECT, FROM, WHERE, JOIN, GROUP BY, ORDER BY...) on its own line, indenting AND/OR conditions and column lists so the structure is obvious at a glance:
-- Before select id, name, email from users where active = 1 and role = 'admin' order by name asc -- After SELECT id, name, email FROM users WHERE active = 1 AND role = 'admin' ORDER BY name ASC
The tokenizer and indentation rules run entirely in your browser with native JavaScript — no external libraries, no server calls.
Examples
1. A one-line report query. Each main clause starts a new line, select columns and AND conditions are indented under their clause, and keywords are uppercased (the default):
-- Input select u.id, u.email, count(o.id) as orders from users u left join orders o on o.user_id = u.id where u.active = 1 and o.created_at >= '2026-01-01' group by u.id, u.email having count(o.id) > 3 order by orders desc limit 20 -- Output (13 lines) SELECT u.id, u.email, count(o.id) AS orders FROM users u LEFT JOIN orders o ON o.user_id = u.id WHERE u.active = 1 AND o.created_at >= '2026-01-01' GROUP BY u.id, u.email HAVING count(o.id) > 3 ORDER BY orders DESC LIMIT 20
2. A subquery. A SELECT inside parentheses gets its own indentation level, so you can see where the inner query starts and ends:
-- Input select name from customers where id in (select customer_id from invoices where total > 1000) -- Output SELECT name FROM customers WHERE id IN ( SELECT customer_id FROM invoices WHERE total > 1000)
3. Keep your team's lowercase style. Uncheck Uppercase and only the layout changes; the words stay as you typed them:
-- Input insert into audit_log (user_id, action, created_at) values (42, 'login', now()) -- Output (uppercase off) insert into audit_log(user_id, action, created_at) values (42, 'login', now())
Common problems when formatting SQL
- Expecting the formatter to find syntax errors. A formatter re-lays out tokens; it doesn't run the query against a database grammar, so a missing comma or a misspelled column comes out nicely indented and still broken. Formatting does make the mistake easier to see, because each column and condition sits on its own line.
- Strings and identifiers changed by uppercasing. Only SQL keywords are uppercased. Text inside quotes (
'login'), quoted identifiers and function names you typed in lowercase (count,now) keep their case — which matters in PostgreSQL, where"UserId"anduseridare different columns. - Dialect-specific syntax. The layout rules cover standard clauses (
SELECT,JOIN,WHERE,GROUP BY,HAVING,ORDER BY,LIMIT,INSERT,UPDATE,DELETE). Vendor extensions are kept but not laid out: T-SQL'sTOP 10stays lowercase on theSELECTline, and Oracle'sSTART WITH … CONNECT BYgets a misleading line break beforeWITH, becauseWITHalso opens a common table expression. The tokens are unchanged, so the query still runs; just don't read structure into those lines. - Diffs full of whitespace changes. Reformatting a query that is under version control changes every line. Commit the formatting on its own, separate from logic changes, so reviewers can tell them apart.
- Pasting the formatted query back into code. If the SQL lives inside a string in your application, multi-line output needs a multi-line string (template literals in JavaScript, triple quotes in Python) or it will break the source file.
SQL formatter vs. other options
Most database IDEs (DBeaver, DataGrip, SQL Server Management Studio, Azure Data Studio) can format a query you already have open there. In a codebase, a formatter that runs on every commit keeps style consistent: npx sql-formatter -l postgresql query.sql for JavaScript projects or sqlfluff fix query.sql --dialect postgres, which also lints. This page is the quick option for a query copied from a log, an ORM's debug output or a ticket, when you just need to read it. To see the same query as a diagram of tables and joins, open it in the SQL Query Visualizer; for formatting HTML, CSS or JavaScript, use the HTML, CSS & JS Beautifier.
Frequently asked questions
What SQL dialects does it support?
Formatting is based on standard keywords (SELECT, FROM, WHERE, JOIN, GROUP BY...) common to MySQL, PostgreSQL, SQLite and SQL Server. It doesn't validate engine-specific syntax, only reorders and highlights.
Is it based on a full SQL parser?
No. It uses heuristic rules by keyword and parenthesis depth, similar to most online SQL formatters. For very complex, deeply nested queries the result may need manual tweaks.
Does it respect text inside strings and comments?
Yes, the tokenizer recognizes single/double-quoted literals and -- or /* */ comments as atomic blocks that don't get split or recolored as keywords.
Is it safe to paste queries with sensitive data?
Yes. All formatting happens in your browser; no query is ever sent to a server.