SQL Formatter
Pretty-print and format SQL queries with keyword casing.
Recommended tools
Free CDN, DNS, and edge hosting for developers.
Affordable domains and SSL certificates.
Affiliate links — we may earn a commission if you sign up.
About the SQL Formatter
The SQL Formatter turns a cramped query into properly indented, consistently-cased SQL in real time. Paste any query into the SQL input textarea and the tokenizer reformats it instantly: reserved words like SELECT, FROM, WHERE, LEFT JOIN, GROUP BY, HAVING, and ORDER BY are uppercased and placed on their own lines, parenthesized conditions are indented as blocks, each comma in a select list starts a new line, and comments are preserved exactly where they sit. Strings in single quotes, double quotes, or backticks are treated as opaque literals, so a WHERE clause containing the word select is never mangled, and multi-word keywords such as LEFT OUTER JOIN are merged before formatting so they break lines correctly. An Indent size dropdown switches between 2 and 4 spaces, and the sample query — a join with aggregation, a date filter, HAVING, ORDER BY, LIMIT, and OFFSET — demonstrates the whole feature set at once. Stats track output lines and keyword count, and the formatted result downloads as formatted.sql. For code review or documentation, it is the fastest way to make SQL readable.
Examples
select u.id, u.name, count(o.id) as order_count, sum(o.total) as spent from users u left join orders o on o.user_id = u.id and o.status = 'paid' where u.created_at >= '2024-01-01' and (u.country = 'US' or u.country = 'CA') group by u.id, u.name having count(o.id) > 0 order by spent desc limit 10 offset 20;
SELECT u.id, u.name, COUNT(o.id) AS order_count, SUM(o.total) AS spent FROM users u LEFT JOIN orders o ON o.user_id = u.id AND o.status = 'paid' WHERE u.created_at >= '2024-01-01' AND ( u.country = 'US' OR u.country = 'CA' ) GROUP BY u.id, u.name HAVING COUNT(o.id) > 0 ORDER BY spent DESC LIMIT 10 OFFSET 20;
update accounts set status = 'active', plan = 'pro' where id = 42;
UPDATE accounts SET status = 'active', plan = 'pro' WHERE id = 42;
select id, case when score >= 90 then 'A' else 'B' end as grade from exams order by id;
SELECT id, CASE WHEN score >= 90 THEN 'A' ELSE 'B' END AS grade FROM exams ORDER BY id;
How to use
- 1
Paste your raw SQL into the SQL input textarea, or press Load sample.
- 2
Choose 2 spaces or 4 spaces from the Indent size dropdown.
- 3
Watch the Formatted SQL result box update live as you type.
- 4
Check the Lines, Keywords, and Indent stats for a quick quality glance.
- 5
Download the output as formatted.sql for commit or documentation.
Common use cases
- Reformatting one-line queries pasted from ORM logs before debugging them.
- Standardizing keyword casing across a team's SQL for code review.
- Preparing readable queries for documentation, issues, or pull requests.
- Indenting nested subqueries so their structure is visible at a glance.
- Cleaning up generated SQL from migration tools before archiving it.
- Comparing two queries side by side after formatting them consistently.
Best practices
- The formatter does not parse SQL semantics — verify the query still runs after formatting, especially with vendor extensions.
- Keep string literals exactly as they are: the tokenizer preserves quotes, doubled quotes, and backticks verbatim.
- For deeply nested subqueries, use the 4-space indent so block structure is easier to follow.
- Comments are kept where they appear; use them as markers when you want context preserved in the output.
- Do not rely on this tool to detect syntax errors — unterminated strings or comments may simply fail to format.
- Reformat before diffing two versions of a query so the comparison shows real logic changes only.
Tips
- Load the sample query first — it exercises joins, aggregation, HAVING, LIMIT, and OFFSET in one shot.
- Format queries before pasting them into a chat or issue tracker; readers spot logic errors faster in structured SQL.
- Use the Keywords stat to sanity-check that your clauses were recognized after a big refactor.
- For long scripts with multiple statements, the semicolon resets indentation, so each statement starts clean.
Frequently asked questions
Explore more developer tools
Browse the full collection of developer tools on the hub, or jump back to all categories.