What this SQL formatter does
A sql formatter takes a query that is hard to read, often one long line copied from a log, an ORM or a chat message, and lays it out so each clause starts on its own line and nested parts are indented. The query means exactly the same thing afterwards. Only the spaces and line breaks between words change.
Here is a one-line query and what the default settings give back:
select c.name, count(o.id) as orders from customers c join orders o on o.customer_id = c.id where o.status = 'paid' and o.total > 50 group by c.name order by orders desc;SELECT
c.name,
COUNT(o.id) AS orders
FROM
customers c
JOIN orders o ON o.customer_id = c.id
WHERE
o.status = 'paid'
AND o.total > 50
GROUP BY
c.name
ORDER BY
orders DESC;What is never changed
The formatter reads the SQL as a list of tokens first: words, numbers, operators, strings, quoted names, parameters and comments. It only rewrites the space between tokens, so these always come out exactly as you typed them:
- Text inside 'strings', including line breaks and doubled quotes.
- Quoted names such as
"Order Date",`order date`in MySQL and[Order Date] in SQL Server. - Comments, both
-- lineand/* block */, unless you choose Remove comments. - PostgreSQL
$$ ... $$function bodies and Oracleq'[ ... ]'strings. - Table, column and alias names. Keyword case only touches reserved words such as SELECT, FROM and JOIN, and never a name written after a dot like
t.order.
The tool also checks that reading the formatted result gives the same token list as your input. Formatting and then minifying a query always leads back to the same tokens.
The layout options explained
- Keywords: UPPER, lower, or As typed. Names are never changed.
- Indent: 2 spaces, 4 spaces or a tab for each nested level.
- One column per line: puts every column, GROUP BY and ORDER BY item on its own line. Turn it off for a compact layout where items stay on the clause line.
- AND / OR on new line: starts each condition in WHERE, HAVING and JOIN ... ON on a fresh line. The AND inside
BETWEEN 1 AND 5stays where it is. - Commas: trailing puts the comma at the end of the line, leading starts the next line with it, which makes it easy to comment out one column.
- Line width: short parenthesized groups, IN lists and CASE expressions stay on one line when they fit in 60, 80, 100 or 120 characters. Longer ones are broken up. Off never wraps.
- Align keywords: right-aligns clause keywords so the query reads down one column.
- Uppercase functions: writes built-in functions such as COUNT(), COALESCE() and DATE_TRUNC() in capitals. Your own functions keep the case you used.
With Align keywords turned on, the same query looks like this:
SELECT c.name,
COUNT(o.id) AS orders
FROM customers c
JOIN orders o ON o.customer_id = c.id
WHERE o.status = 'paid'
AND o.total > 50
GROUP BY c.name
ORDER BY orders DESC;And with One column per line turned off, it becomes a compact block:
SELECT c.name, COUNT(o.id) AS orders
FROM customers c
JOIN orders o ON o.customer_id = c.id
WHERE o.status = 'paid'
AND o.total > 50
GROUP BY c.name
ORDER BY orders DESC;Choosing a dialect
SQL databases agree on most syntax but not on quotes, comments and parameters. The dialect setting only changes how those are read:
- Standard SQL: 'strings', "names", plus `backtick` names and $$ bodies, so most scripts format safely without picking anything.
- PostgreSQL: $tag$ dollar quotes, E'...' escape strings, nested block comments,
::casts and operators such as@>,<->and!~*. - MySQL / MariaDB: backtick names, double-quoted strings, backslash escapes,
#comments,<=>and@uservariables. - SQL Server: [square bracket] names, N'unicode' strings,
@variables,#temptables and GO batch separators. - SQLite: square brackets, backticks and the
?1,:name,@nameand$nameparameters. - Oracle: q'[...]' quoting,
:1bind variables andtable@dblinknames.
If the SQL contains something that suggests another dialect, such as backticks while PostgreSQL is selected, the summary shows a note.
Minify SQL to one line
Minify mode removes line breaks and every space that is not needed, which is handy when a query has to fit in a config value, a log line or a string in code. It still keeps a space where removing it would join two tokens, for example between - and -1 so it never turns into a -- comment:
SELECT c.name,COUNT(o.id) AS orders FROM customers c JOIN orders o ON o.customer_id=c.id WHERE o.status='paid' AND o.total>50 GROUP BY c.name ORDER BY orders DESC;A -- comment runs to the end of its line, so minified output keeps a line break after each one. Turn on Remove comments to get a true single line.
Errors the formatter catches
Before formatting, the tool checks that every string, quoted name and block comment is closed and that parentheses are balanced. The error names the line and column. For example, a missing closing parenthesis:
Line 1, column 34: This ( is never closed. Add a ) where the group should end.It does not check table names, column names or whether the database would accept the query. For that, run it against your database or use the SQL query optimizer, which reads the query more deeply.
Limits worth knowing
- Input is capped at 2,000,000 characters. A script of that size usually formats in about a second or less, depending on the device.
- Procedural code in stored procedures (IF, LOOP, BEGIN ... END blocks) is kept in order and spaced, but it is not indented as blocks.
- Pasted text uses LF line breaks on this page, so Windows CRLF endings are not kept.
- Removing comments keeps optimizer hints such as
/*+ INDEX(t i) */and MySQL/*! ... */, because deleting them changes how the query runs.
Working with data next to your SQL? JSON to SQL turns JSON rows into INSERT statements, and SQL to JSON turns INSERT statements back into JSON.
Privacy
Your SQL is formatted in your browser. Nothing is uploaded or stored on a server. See the Privacy Policy for details.