SQL Formatter — make long queries readable
Formats SQL queries in the clause-per-line style, with keywords in upper case and joins, conditions and subqueries indented. Choose 2 spaces, 4 spaces or a tab; everything runs in your browser.
Related tools
- HTML Formatter Beautify messy or minified HTML with clean indentation of 2 spaces, 4 spaces or tabs, or minify it again.
- CSS Minifier Minify CSS by removing comments and whitespace, or beautify minified CSS again. Safe for string values.
- JavaScript Minifier Minify JavaScript with Terser: removes whitespace, comments and dead code, keeps names readable. Runs in your browser.
- Code Formatter Beautify and format HTML, CSS, JavaScript, XML, SQL code. Beautify or minify with customizable indentation and auto-format as you type.
Features
- Formats SQL with one clause per line
- Upper-cases SQL keywords, keeps your table and column names
- Indents subqueries, JOINs and AND/OR conditions
- Indentation with 2 spaces, 4 spaces or tabs
- Minify mode puts a query on a single line
- Runs entirely in your browser, nothing is uploaded
When a SQL Formatter Helps
SQL rarely arrives in a readable shape. Queries copied from application logs, ORM debug output or a slow-query report usually sit on one long line, and hand-written queries drift apart in style from one developer to the next. With one clause per line you see at a glance which tables are joined, which conditions filter the rows and where a subquery begins and ends.
This page opens the Code Formatter with SQL and beautify mode already selected. Paste a query, choose 2 spaces, 4 spaces or a tab, then copy the result or download it as a .sql file. Everything happens in your browser, so queries containing table names or real values never leave your machine.
How Your Query Is Laid Out
- Clauses such as
SELECT,FROM,WHERE,GROUP BY,HAVING,ORDER BY,LIMIT,INSERT INTO,VALUES,UPDATE,SET,WITHandUNIONstart a new line, with their content indented one level below. - Joins (
JOIN,LEFT JOIN,INNER JOINand the other variants) get their own line inside the FROM block; theONcondition stays on that line. - AND / OR at the top level of a condition start a new line. Inside parentheses, function calls and
INlists everything stays on one line. - Commas at the top level put each column or value on its own line.
- Subqueries: a
SELECTorWITHinside parentheses is indented one block deeper and closed on its own line. - Keywords are written in upper case; table names, column names and aliases keep your spelling.
- Strings and comments stay untouched. A
--comment ends the line, a/* … */comment stays where it was. - Several statements separated by
;are formatted one after the other, with a blank line between them.
Example
A typical one-line query from a log file:
select u.id, u.name, count(o.id) as orders from users u left join orders o on o.user_id = u.id where u.active = 1 and u.name like '%a%' group by u.id, u.name order by orders desc limit 10With 4 spaces of indentation the formatter returns:
SELECT
u.id,
u.name,
COUNT(o.id) AS orders
FROM
users u
LEFT JOIN orders o ON o.user_id = u.id
WHERE
u.active = 1
AND u.name LIKE '%a%'
GROUP BY
u.id,
u.name
ORDER BY
orders DESC
LIMIT
10 Limits and Tips
- Not a validator: the formatter only changes whitespace and the case of keywords. It does not check your syntax, so a typo such as
selecpasses through unchanged without an error. Test the query in your database. - Not dialect-aware: it works from a fixed keyword list, not a parser for MySQL, PostgreSQL or SQL Server. Column names that are also keywords, such as
dateorkey, are upper-cased as well, and inBETWEEN 1 AND 5theANDmoves to a new line. - Operator spacing: operators get a space on each side, so
a=1becomesa = 1. Check unusual forms:t.*comes out ast. *. - Minify: removes
--and/* */comments and collapses whitespace to single spaces. Text inside quotes stays exactly as written, comment markers and repeated spaces included. Keywords keep the case you typed.
Useful Resources