SQL Formatter

Format and beautify SQL in your browser — clauses on their own lines, keywords uppercased, subqueries indented, string literals left byte-identical, nothing uploaded. Free, no sign-up — runs locally in your browser.

Your work is auto-saved in this browser only, so a refresh won't lose it. Nothing is uploaded.

…or drop a file onto the box above. Files are read locally, never uploaded.

What is a SQL formatter?

SQL that comes out of an ORM log, an application config file or a colleague's chat message is written in one breath: one enormous line where SELECT, FROM and WHERE blur together, or the opposite — a query exploded across forty lines by hand, every contributor with a different idea of where things line up. A SQL formatter (also called a beautifier or pretty-printer) rewrites the query with a consistent clause layout: each major clause starts on its own line, the item lists break one per line, and subqueries step in by one level — so the shape of the query, which tables feed which filters, is visible at a glance.

This one is deterministic: the same query always produces the same output, regardless of how the input happened to be wrapped. That makes formatted SQL safe to commit — diffs show real changes, not reflow noise — and safe to review, because the layout never depends on who pressed the button.

How to use this tool

  1. Paste your SQL into the box above (or press Load sample to see a worked example). You can also upload an .sql file or drop one onto the box — the file is read in your browser and never sent anywhere.
  2. Choose an indent — 2 spaces is the common default; 4 spaces and tabs are one click away.
  3. Press Format. The result appears below with a line count; your input above is never modified — the result is a separate copy.
  4. Tick Auto-format as I type if you would rather not press the button: the result updates as you edit (very large inputs are still formatted on demand, to keep typing smooth).
  5. Use Copy result or Download result to take the formatted query with you.

How the layout works

The formatter reads the query as a stream of tokens — words, numbers, string literals, quoted identifiers, comments, operators and parentheses — and reassembles it by clause. SELECT, FROM, WHERE, GROUP BY, ORDER BY, HAVING, LIMIT, the JOIN family and the set operations (UNION, INTERSECT, EXCEPT) each begin a new line. Inside a WHERE clause, AND and OR start their own lines one level in, so long filter chains read as a list of conditions instead of a paragraph. A parenthesised subquery is indented as a block; ordinary parentheses — function calls, IN lists, column lists — stay on the line, and a known function name's parenthesis binds tightly (count(*), not count (*)).

Clause keywords are uppercased wherever they appear as standalone words — that includes AND, OR, NOT, NULL, AS and ON. Everything else keeps the author's spelling: UserName stays UserName, because in a case-sensitive collation that can be a different column. String literals are opaque: the words select and from inside 'a select from nowhere' are data, and the formatter treats them that way.

Examples

This one-line query:

select u.name, o.total from users u left join orders o on o.user_id = u.id where o.total > 10 and u.active = 1 order by o.total desc

formats into:

SELECT u.name,
  o.total
FROM users u
LEFT JOIN orders o ON o.user_id = u.id
WHERE o.total > 10
  AND u.active = 1
ORDER BY o.total DESC

What this formatter does not do

Honest limits, in the open. This is a clause formatter, not a SQL parser: it does not validate your query, so a misspelled column or a missing comma passes through laid out but still wrong. CASE expressions, common table expressions and CREATE TABLE definitions are laid out by the same clause rules rather than given bespoke layouts, so their interiors stay on fewer lines than a hand-tuned format would use. And it formats for people, not for wire size — if you need the opposite direction, what you want is a minifier.

Frequently asked questions

Is my SQL uploaded anywhere?

No. Formatting happens in your browser, on this page. Nothing is sent to a server — you can disconnect from the network and the tool keeps working. Your draft is auto-saved in this browser only, for 7 days, and the Clear saved draft button deletes it immediately.

Does this tool run or validate my query?

Neither. It never connects to a database and never executes anything, and it is a clause formatter, not a SQL parser: it lays out the clauses it recognises but does not check that your query is valid SQL. A syntactically wrong query gets reformatted, not rejected — if you need certainty, run the query against your database in a transaction you can roll back.

What exactly does formatting change?

Layout and keyword case only. Major clause keywords (SELECT, FROM, WHERE, GROUP BY, ORDER BY, the JOIN family, UNION and friends) start new lines, are uppercased wherever they appear, and the SELECT list and SET assignments break one item per line. Table names, column names and function names keep the spelling you wrote, and string literals, quoted identifiers and comments pass through byte-for-byte — a word like 'from' inside a string is never touched.

Will formatting change what my query does?

No. The output contains exactly the same tokens as the input, in the same order — whitespace and the case of unquoted keywords are the only differences, and SQL is case-insensitive for keywords in every mainstream dialect. One thing to know: if your SQL relies on a keyword-spelled identifier without quoting it (a column literally named order, say), the case change is visible in the output exactly as in every other formatter — quote such identifiers and they pass through untouched.

Which SQL dialects are supported?

The clause structure is shared by all mainstream dialects — PostgreSQL, MySQL, SQLite, SQL Server, BigQuery, Snowflake — and the formatter is dialect-neutral: it handles standard quoting ('strings', "identifiers", `backticks`, [brackets]) without needing to know which database you use. Dialect-specific procedural syntax (PL/pgSQL or T-SQL blocks, psql backslash commands) is outside its scope and will be laid out by the generic clause rules rather than understood.

Can I open an .sql file instead of pasting?

Yes — use the Upload button or drag a file onto the input box. The file is read locally by your browser (it is never uploaded to a server), its contents land in the input box, and formatting runs immediately. Download result does the same job in the other direction.

Related tools