SQL Formatter

Paste any SQL query to instantly format it with keyword-based line breaks and indentation, choose UPPERCASE, lowercase or unchanged keyword casing, or switch to Minify for a compact single line. Nothing leaves your browser.

Indent
Keyword case
More options

Paste a SQL query above to format or minify it.

This is a formatter for readability, not a SQL validator or parser for every database dialect. Every query is formatted entirely in your browser — nothing is uploaded to a server.

Why a one-line query is hard to read — and how formatting fixes it

SQL written on a single line, or copy-pasted out of a log file, an ORM query builder, or a minified export, is technically correct but slow to read: the eye has to hunt for FROM, WHERE and JOIN buried in the middle of a run-on sentence. A formatter's job is to restore the visual structure that SQL's grammar implies — one clause per line, columns and conditions lined up under a consistent indent — without changing what the query does. Paste a query above and it is tokenized, reflowed around its keywords, and re-rendered with your chosen indent width and keyword casing.

How the line-breaking rules work

The formatter walks the query token by token and applies a small set of consistent rules:

Example — a join with a filter and an aggregate. Paste this single line:

SELECT u.id, u.name, COUNT(o.id) AS order_count FROM users u LEFT JOIN orders o ON o.user_id = u.id WHERE u.created_at BETWEEN '2024-01-01' AND '2024-12-31' GROUP BY u.id, u.name HAVING COUNT(o.id) > 5 ORDER BY order_count DESC;

and Format (2-space indent, UPPERCASE keywords) turns it into:

SELECT u.id,
  u.name,
  COUNT(o.id) AS order_count
FROM users u
LEFT JOIN orders o
  ON o.user_id = u.id
WHERE u.created_at BETWEEN '2024-01-01' AND '2024-12-31'
GROUP BY u.id,
  u.name
HAVING COUNT(o.id) > 5
ORDER BY order_count DESC;

Keyword case: UPPERCASE, lowercase, or Unchanged

Reserved words are the only thing the case option touches. UPPERCASE gives the classic textbook look many style guides recommend (SELECT ... FROM ... WHERE ...); lowercase reads a little more compact and matches how many query builders emit SQL; Unchanged reindents the query without touching a single letter's case, useful when a linter or teammate cares about a specific style already in the file. Table names, column names, aliases, and anything inside quotes are always left exactly as typed, because in a case-sensitive database (PostgreSQL's quoted identifiers, for example) changing that casing could silently point at a different column.

Minify: the opposite of formatting

Minify mode strips comments and collapses the query to a single line with only the whitespace SQL actually needs between tokens — handy for embedding a query string in application code, a curl command, or a URL parameter where extra newlines and indentation just waste space. Because it runs through the same tokenizer as formatting, quoted strings, doubled '' escapes, and quoted identifiers pass through byte-for-byte; only the surrounding formatting is removed, never the query's literal values.

What the tokenizer protects — and its honest limits

Before any line-breaking happens, the input is split into strings ('...', with the standard '' escape for an embedded quote), quoted identifiers ("..."/`...`/[...]), line comments (--) and block comments (/* ... */), numbers, operators, and words. Only words that match a list of common SQL reserved keywords are ever recased or used to decide where a line breaks — everything inside a string, comment, or quoted identifier is copied through untouched, so a comment that happens to contain the word SELECT, or a column literally named "order", won't be misread as a keyword.

That said, this tool is a formatter for readability, not a full parser or validator: it doesn't check whether a query is syntactically valid, doesn't know every keyword or function of every dialect (MySQL, PostgreSQL, SQL Server, SQLite, BigQuery and others all differ slightly), and complex nested expressions can occasionally format less elegantly than a hand-tuned style. If something looks off, the Unchanged case option plus a quick scroll through the output usually makes the cause obvious — and nothing about your query ever leaves your browser to be checked anyway.

Sources & further reading

Frequently asked questions

How does this SQL formatter decide where to break lines?

The formatter tokenizes your query and breaks a new line before major clause keywords — SELECT, FROM, WHERE, JOIN (and its INNER/LEFT/RIGHT/FULL/CROSS variants), GROUP BY, HAVING, ORDER BY, LIMIT, UNION and similar — and indents each one consistently. Inside SELECT, GROUP BY, ORDER BY and VALUES lists, every top-level comma starts a new indented line so long column lists are easy to scan, while AND/OR inside WHERE, ON and HAVING each get their own indented line too. Parentheses that open a subquery (SELECT or WITH) are expanded and indented one level deeper, but a plain function call or grouping like COUNT(*) or IN (1, 2, 3) is kept inline so short expressions don't get needlessly split apart.

Does the formatter understand string literals, comments, and quoted identifiers?

Yes. A dedicated tokenizer walks the query character by character and recognizes single-quoted strings ('it''s a test', with the doubled '' escape), double-quoted and backtick-quoted identifiers ("Order Date", `user table`), square-bracket identifiers ([Order Date]), and both -- line comments and /* block */ comments. Anything inside a string, a quoted identifier, or a comment is copied through exactly as typed — it is never reformatted, case-changed, or treated as a keyword — so a column literally named "select" or a comment containing the word FROM won't confuse the output.

What does the keyword case option (UPPERCASE / lowercase / Unchanged) actually change?

It only rewrites the casing of recognized SQL reserved words — SELECT, FROM, WHERE, JOIN, AND, ORDER BY, and the like. Table names, column names, aliases, string contents, and quoted identifiers are always left exactly as you typed them, because changing their case could change what they mean in a case-sensitive database. Pick UPPERCASE for the classic textbook style, lowercase for a more compact look, or Unchanged to only reindent the query and leave every word's original casing untouched.

What is Minify mode for, and is it safe to use on a real query?

Minify strips every comment and collapses the query onto a single line with only the whitespace SQL actually requires between tokens, which is handy for pasting a query into application code, a log line, or a URL parameter without wasting characters. It runs through the same string/identifier/comment-aware tokenizer as formatting, so quoted text and escaped quotes inside strings are preserved byte-for-byte — only the surrounding whitespace and comments are removed, never the query's logic or literal values.

Is my SQL query uploaded anywhere, and does this tool support every database dialect?

No — every keystroke is tokenized and reformatted entirely inside your browser with plain JavaScript; nothing is sent to a server, logged, or stored anywhere except optionally your own browser's local storage so your last query is there when you come back. This is honestly a formatter, not a full SQL parser or validator for every dialect: it recognizes common MySQL, PostgreSQL, SQL Server and SQLite syntax (including their different identifier-quoting styles) well enough to indent it cleanly, but it doesn't check whether your query is semantically valid or catch every dialect-specific keyword.