SQL Formatter

In short: paste a query and get it back readable — clauses down the left margin, lists one per line, subqueries indented, and string literals never touched.

What 'formatted' means here

The conventions are the ones most style guides converge on. Each major clause — SELECT, FROM, WHERE, GROUP BY, ORDER BY, HAVING, LIMIT, JOIN — starts on its own line at the margin, so the skeleton of the query can be read down the left edge without parsing a single condition. The SELECT list and SET assignments break one item per line, indented, because those are the lists people edit. AND and OR begin their own indented lines, so a five-condition filter reads as five lines rather than a paragraph. Subqueries indent as blocks, function calls stay tight (COUNT(*), never COUNT (*)), and string literals are never touched — a comma inside 'Doe, John' is data, not a list separator, and any formatter that splits on it is corrupting your query while tidying it. This one tokenises strings, quoted identifiers and escaped quotes ('' inside a literal) before it formats anything.

A worked example, read closely

The default query in the box above is the shape of most real SQL: a join, two filters, a grouping and a limit, all in one breathless line. Formatted, its logic sorts itself into the order a reader checks it — outputs first (two lines, one a COUNT(*) aggregate), then the two tables and their join condition riding on the JOIN line where it belongs, then the two filters with AND leading the second, then grouping, ordering and the limit, each a single scannable line. Nothing about the query changed; everything about auditing it did. If the join condition were wrong — customer_id matched against the wrong key — the formatted version is the one where a reviewer has a chance of seeing it, because the condition sits alone on a labelled line instead of hiding in the middle of a sentence.

Why bother formatting SQL at all

Because SQL is read far more often than it is written, and usually by someone debugging it at an unkind hour. The one-line query that made sense when you typed it becomes an archaeology project in a code review, a log file or an incident channel; the formatted version answers the three questions every reader asks — what is being read, from where, filtered how — in three glances at the left margin. Formatting is also a weak but real error-detector: a misplaced parenthesis or a condition swallowed into the wrong clause often becomes visible the moment the structure is laid out, which is why pasting a broken query into a formatter is a legitimate first debugging step. The formatter is not a validator — it will happily prettify SQL that no database would run — but structure you can see is structure you can check.

Frequently asked questions

How does the SQL formatter work?

It tokenises first, formats second. The tokenizer walks the input once, recognising string literals (including doubled-quote escapes like 'it''s'), quoted identifiers, operators, parentheses and words, so commas and keywords inside strings are treated as data and never split or uppercased. The formatter then lays the tokens out by rule: clauses start new lines at the margin, SELECT and SET lists break one item per line, AND/OR get their own indented lines, subquery parentheses open indented blocks, and function calls stay tight against their names. Keyword casing and indent width (two spaces, four spaces or tabs) are the two options. Nothing is sent anywhere — it runs in your browser, on text you paste.

Will the formatter change what my query does?

It should not, and the design choices are all aimed at that guarantee: string literals and quoted identifiers pass through byte-for-byte, comments and whitespace are the only casualties (whitespace is, of course, the point), and no tokens are added, removed or reordered — layout and keyword case only. Two honest caveats. Keyword uppercasing applies to words the formatter recognises as keywords, so an unquoted column actually named 'order' would be uppercased (its meaning is unchanged; SQL keywords are case-insensitive). And a formatter cannot fix or validate logic: it will format a query with a missing comma into a prettier query with a missing comma. Diff the formatted output against the original before committing it, as you would with any automated reformat.

What SQL dialects does it support?

The core grammar shared by MySQL, PostgreSQL, SQL Server, Oracle and SQLite: SELECT with joins, subqueries, grouping and ordering; INSERT with column lists and multi-row VALUES; UPDATE with SET lists; DELETE. Dialect-specific syntax — PostgreSQL dollar-quoting, MySQL backticks (supported as quoted identifiers), SQL Server bracket identifiers, PL/SQL blocks, CREATE TABLE definitions with long constraint lists — is handled gracefully rather than expertly: it will be laid out by the general rules without corruption, but exotic constructs may not break lines exactly where a dialect-native formatter would. For the everyday SELECT-INSERT-UPDATE work that makes up most pasted SQL, the general rules are the right ones.

Why do SELECT items go on separate lines but FROM stays on one?

Because they are different kinds of list. The SELECT list is the part of a query that grows — columns are added, removed and commented out in reviews — and one-per-line makes each change a one-line diff and lets a reader count and scan the outputs. The FROM clause names a handful of tables and reads naturally as a phrase; breaking it would add lines without adding clarity, except for joins, which do get their own lines here because each join is a decision. WHERE conditions split the difference: the first rides on the WHERE line, the rest get their own lines led by AND or OR, because a filter's logic lives in its connectives and those should be impossible to miss.

Can I format SQL with comments in it?

Partly — and the limit is worth knowing. This formatter tokenises strings and quoted identifiers carefully, but line comments (--) and block comments (/* */) are not preserved as first-class citizens: a -- comment will be treated as operators and words and may end up repositioned, and a block comment's contents can be re-spaced. If your query carries comments you care about, the safe workflow is to format a comment-free copy for structure, or strip and re-add comments after. Queries pasted from application logs and most hand-written SQL carry no comments, which is the case this tool is built for; comment-preserving formatting is a job for your IDE's dialect-aware formatter.

Related tools

Formatter last verified in Node against five test queries: 24 November 2026. Everything is calculated in your browser — nothing you type is sent anywhere.