SQL Formatter

Paste a cramped one-line query and this basic SQL formatter lays it out properly: uppercase keywords, one clause per line, JOINs on their own lines and conditions indented — so the logic is visible at a glance.

1Paste your SQL
2Choose the list style and press Format
3Copy or download the tidy query

Private by design: 100% in your browser — nothing is uploaded, no sign-up. This tool runs entirely on your device.

How to use this SQL formatter

Paste your query into the top box and press “Format SQL”. The tool tokenises the statement — words, numbers, strings, operators — then rebuilds it under a few steady rules: keywords are uppercased; SELECT, FROM, WHERE, GROUP BY, ORDER BY, HAVING and LIMIT each begin a new line; JOINs get lines of their own; AND and OR conditions are indented under the clause they belong to; and, unless you untick it, the SELECT list is broken one column per line. The result appears below, ready to copy or download as a .sql file.

Call it what it is: a basic formatter. It does not connect to a database, check your column names, or understand every vendor's extensions. It makes ordinary queries readable — and ordinary queries, copied out of logs, ORM debug output and application code, are exactly the ones that arrive as unreadable single lines.

Example: one line in, structure out

This typical query, as it might come out of an application log:

select id, name, email from users where age > 30 and city = 'New York' order by name limit 10

formats to:

SELECT id, name, email FROM users WHERE age > 30 AND city = 'New York' ORDER BY name LIMIT 10

Same query, same results — but now the shape is obvious: three columns, one table, two conditions, one sort, a cap of ten rows. Notice what survived untouched: the string 'New York' kept its capitalisation and its space, because the tokenizer treats quoted text as a value, never as keywords waiting to be reformatted. A join behaves the same way — LEFT JOIN users u ON o.user_id = u.id starts its own line with its condition kept beside it, and a subquery opens up with its clauses indented a level deeper.

Why formatting is a debugging tool, not a vanity

Most SQL bugs are reading bugs first. A condition attached to the wrong clause, an AND that should have been an OR, a JOIN quietly missing its ON — all of them hide comfortably inside a 300-character line and all of them step forward when the query is laid out in clauses. Formatting is the cheapest code review a query can get: thirty seconds of layout before thirty minutes of staring. It also pays off socially. A formatted query pasted into a ticket, a chat or a pull request gets a useful answer faster, because the reader spends their attention on your problem instead of on parsing your punctuation.

The conventions behind the layout

The style here follows the habits most teams converge on. Keywords in capitals separate the language from your schema. One clause per line mirrors how the database itself thinks — it gathers rows (FROM), filters them (WHERE), groups (GROUP BY), filters again (HAVING), sorts (ORDER BY) and only then picks the columns to show (SELECT) — which is why a formatted query reads top to bottom like the plan it will execute. Conditions indented under their clause keep long WHERE blocks scannable without losing which clause owns them. None of this is law; it is convention, and convention is what makes the next person's reading fast.

Reading a formatted query, top to bottom

Once a query is laid out, a reliable reading order suggests itself. Start at FROM: which tables are involved, and how are they joined — every JOIN line should carry its ON condition, and a join without one is the first thing to question. Move to WHERE and read the conditions as a checklist, watching for the AND that narrows more than intended and the OR that widens more than expected. Then GROUP BY and HAVING if they are present, since aggregation quietly changes what a “row” means. Only then read the SELECT list, which is merely what the already-filtered rows will show. This order feels backwards compared with how SQL is written, but it matches how the database executes — rows are gathered and filtered before any column is chosen — and reading in execution order is how you catch the classic bug: a perfectly formatted query that answers a slightly different question than the one you asked.

Frequently asked questions

What does this SQL formatter do?

It rewrites a query in a consistent layout: keywords in uppercase, each major clause — SELECT, FROM, WHERE, GROUP BY, ORDER BY, LIMIT — starting on its own line, conditions indented under their clause, JOINs on their own lines, and the SELECT list broken one column per line (you can turn that off). It is deliberately a basic formatter: it does not change what the query means, only how it reads.

Will formatting change what my query does?

No. SQL is not sensitive to whitespace or to keyword case, and this tool changes nothing else: table and column names keep their spelling, string literals are copied character for character (including commas and keywords inside them), and operators and values stay in order. The formatted query returns exactly what the original returned.

Which SQL dialects does it support?

Standard, everyday SELECT-style SQL — the common ground of MySQL, PostgreSQL, SQLite, SQL Server and Oracle. Because it formats by tokenising rather than fully parsing a dialect, vendor-specific syntax (window functions, CTEs, stored procedures) will be laid out approximately rather than perfectly. For plain queries and joins, which are most of what people paste, the result is clean.

How are JOINs and subqueries handled?

Each JOIN — including LEFT, RIGHT, INNER and FULL variants — starts on its own line with its ON condition kept beside it. A subquery in parentheses is opened up with its clauses indented one level deeper, and the closing bracket returns to the outer level, so the nesting of the query is visible at a glance.

Why uppercase the keywords?

Purely for the reader. Databases accept any case, but a long-standing convention writes keywords in capitals so the skeleton of the query — SELECT, FROM, WHERE — stands out from the names and values that belong to your schema. If your team's style is lowercase keywords, treat the output as a layout draft and case it your way.

What does “one column per line” do?

It breaks the SELECT list so each selected column or expression sits on its own indented line. Long lists become scannable, and diffs between two versions of a query show exactly which column changed. Untick the option if you prefer the whole list on the SELECT line for short queries.

Is my SQL uploaded to a server?

No. The formatter runs in JavaScript in your own browser. Queries often contain real table names, customer columns and business logic, so keeping them on your device is the only sensible design — nothing is sent, stored or logged.

Does it keep my comments?

Yes. Line comments (--) are kept and placed at the end of their line, and block comments (/* */) are kept inline. String literals that merely look like comments are recognised as strings first, so a value like 'a--b' is never mistaken for one.

Can it find errors in my SQL?

No — a formatter is not a validator. It will lay out whatever you give it, including a query with a misspelled column or a missing comma, because it does not connect to a database or fully parse the grammar. If a query formats oddly around one spot, that spot is still a good place to look first when the database complains.

Related tools