SQL Formatter
Turn a long single-line query into readable, indented SQL with uppercase keywords and aligned clauses. Understands JOINs, CASE expressions, CTEs and sub-queries, with dialects for MySQL, PostgreSQL and SQLite.
Programming Language ToolsHow to use SQL Formatter
- Paste your query into the input panel.
- Pick a dialect: Generic, MySQL, PostgreSQL or SQLite.
- Choose 2 spaces, 4 spaces or tabs.
- Press Format, then copy the result or download it as formatted.sql.
Examples
Before
One long line, as queries usually arrive from a log.
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.created_at<now() group by u.id,u.email having count(o.id)>0 order by orders desc limit 50;
After
Clauses on their own lines, keywords uppercased.
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.created_at < NOW() GROUP BY u.id, u.email HAVING COUNT(o.id) > 0 ORDER BY orders DESC LIMIT 50;
Features
- Real SQL parsing via sql-formatter, in pure JavaScript
- SELECT, INSERT, UPDATE, DELETE, JOIN, GROUP BY, ORDER BY and HAVING
- CASE expressions, common table expressions (WITH) and nested sub-queries
- Dialect awareness for MySQL, PostgreSQL and SQLite, plus a generic mode
- Keywords uppercased consistently
- 2-space, 4-space or tab indentation
- Copy to clipboard or download formatted.sql
- Runs entirely in your browser — queries are never uploaded
Frequently asked questions
- Which dialects are supported?
- A generic standard-SQL mode plus MySQL, PostgreSQL and SQLite. The dialect affects how quoting, identifiers and dialect-specific keywords are treated, so picking the right one gives cleaner output.
- Does it handle CTEs and sub-queries?
- Yes. WITH clauses, nested sub-queries in FROM or WHERE, and CASE expressions are all recognised and indented rather than left on one line.
- Will it change what my query does?
- No. Only whitespace and keyword casing change. Identifiers, string literals and the query's structure are untouched.
- Why are keywords uppercased?
- It is the long-standing convention, and it makes the shape of a long query readable at a glance by separating keywords from your own identifiers.
- Can it format a stored procedure?
- Simple procedural blocks usually format cleanly, but dialect-specific procedural languages such as PL/pgSQL are only partially supported. Plain queries are the sweet spot.
- Is my query uploaded?
- No. The formatter is JavaScript running in your browser, so queries — which often contain real table and column names — never leave the tab.
Related tools
About SQL Formatter
SQL Formatter is part of Programming Language Tools on ToolHive, a growing collection of small, focused utilities for developers, designers and students. Formatters and playgrounds for Python, C, Java, Go, Rust and SQL. Everything runs client-side, so your data never leaves the browser tab — which makes these tools safe to use with work-in-progress code and private content.