ToolsHub4u

Format SQL with clauses on their own lines

Paste a one-line query and get the major clauses on separate lines, with indented joins, CTEs and subqueries and a choice of keyword case.

  • Major clauses separated
  • Strings preserved
  • Runs locally
Ready Developer & Utility
1Enter
2Adjust
3Copy result

SQL Formatter

1

SQL input

Keyword case
No data yet Live
2

Formatted SQL

Formatted 95 tokens into 11 lines.

Formatted SQL

  • Strings and quoted identifiers preservedText inside single quotes, double quotes, backticks or square brackets is copied without keyword casing.
  • Layout onlyNo query is executed, validated against a schema or rewritten. The tool does not know your database dialect.
  • Single statementThe query ends with a semicolon in the output.
Tokens
95
Lines
11
Characters
330
Keywords cased
UPPER

Formatting is cosmetic. Keyword lists and clause boundaries are language conventions, not a SQL parser. Dialect-specific syntax such as PostgreSQL dollar-quoted strings or MySQL delimiter blocks is not handled specially.

This page puts SQL clauses on separate lines and indents joins, conditions and nested queries. Paste a one-line query, choose upper or lower case for keywords, and the formatted query appears beside the form. It works on the text rather than on a database connection, so no server, credentials or schema are involved.

It is a cosmetic formatter built around a list of keywords and clause boundaries. That is enough to make a generated query readable, and it also means the tool can be fooled by input that is not SQL. The output is valid as long as the input was valid; the formatter does not add missing clauses or check table names.

What it formats

The major clauses start on a new line: SELECT, FROM, WHERE, GROUP BY, HAVING, ORDER BY, LIMIT, OFFSET, JOIN, UNION and their relatives. A join's ON condition is indented under the join, and a second condition joined with AND is indented to the same level.

Commas inside a select list stay on the same line, so a query with ten columns produces one long line rather than one column per line. SET assignments in an UPDATE also stay together. That keeps the output compact, though it is less aggressive than a formatter that breaks every list item.

String literals, quoted identifiers and comments are tokenised so their contents are not treated as keywords. "select", `from` and [where] stay lower case and unchanged inside their quotes, and a string containing 'select from where' is left alone. Line comments and block comments are kept in place.

Keyword case and the indent option

Keyword case has two settings. UPPERCASE is the common convention for SQL keywords, and lowercase is preferred by some teams and by tools that generate queries. The choice applies to recognised keywords only; identifiers and string contents keep their original case.

Indent AND and OR conditions now works under WHERE, HAVING and ON. With it on, each top-level condition goes on its own line one level in. The sample's WHERE o.status = 'paid' AND o.created_at >= '2026-01-01' becomes two lines, and the query grows from 11 lines to 12 without changing any value. With it off, the conditions stay on the clause line.

A condition that is already inside its own parentheses stays together, so WHERE (a = 1 AND b = 2) OR c = 3 keeps the grouped pair on one line and breaks only at the outer OR. That preserves the meaning of the grouping instead of exploding every boolean operator.

A worked example

A 95-token join query with six clauses was formatted with keywords in upper case and the indent option on. The result was 12 lines and 332 characters; with the condition option off it was 11 lines and 330 characters.

Formatting results for four queries
QueryResult
95-token join with six clauses, conditions on12 lines, 332 characters
Same query, conditions off11 lines, 330 characters
CTE with a nested aggregate, 59 tokens12 lines, 266 characters; inner query indented
Subquery in WHERE, 28 tokens9 lines, 135 characters

The sample output put each join on its own line, indented the two ON conditions, kept the select list, the GROUP BY list and the ORDER BY list on single lines, and placed the second WHERE condition on its own indented line.

Lowercasing the keywords changed only the keywords: select, from, join, on, where, group by, having, order by and limit. The table and column names were untouched, so the two outputs are the same query.

Text with no clause is left aloneA note such as this is not a query at all has no clause word, so it now comes back unchanged: no uppercased words and no added semicolon. A sentence that does contain a word such as SELECT can still be treated as a query.

Subqueries, CTEs and comments

A parenthesised query is now indented as a block. WITH recent AS ( opens one level in, the inner SELECT, FROM and WHERE sit at that level, and the closing parenthesis returns to the outer level on its own line. The test CTE formatted to 12 lines and 266 characters with the inner aggregate on its own clause lines.

The same applies to a subquery after IN or EXISTS. In the test, WHERE id IN ( was followed by the inner SELECT at one level in, its own WHERE and indented AND, then a closing parenthesis at the outer level. A condition that follows the subquery, such as AND active = 1, returns to the outer condition indent rather than staying on the closing line.

A second CTE after a comma starts on a new line, so a chain of WITH blocks reads as a list rather than one long line. A nested subquery goes one level deeper again. The formatter tracks parentheses for this purpose but still does not parse the query, so unbalanced parentheses in the input will produce unbalanced indentation.

Comments are kept and placed where they appeared. A line comment stays on its own line, and a block comment inside a statement stays on the line before the clause it preceded. They are not removed or reordered, which matters when a comment explains why a condition exists.

Limits and dialect notes

The formatter does not parse a dialect. PostgreSQL dollar-quoted strings, MySQL DELIMITER blocks, SQL Server GO batches and procedural bodies are not handled specially. A dollar-quoted body containing semicolons can be split at the wrong place, and a stored procedure may not survive the round trip intact.

It also does not validate. A misspelled keyword is treated as an identifier, a missing FROM is not reported, and a query that would fail on the server formats without complaint. Use an EXPLAIN or a database client when correctness matters.

For a dialect-aware formatter with more layout options, use a dedicated library or a database tool. This page is for making a generated or copied query readable quickly. The code diff checker is useful for comparing the formatted query with the original, and the regex tester helps when you need to extract part of a query from a log.

Questions

Common questions

Short answers about this tool and its limits.

Do WHERE conditions go on separate lines?

Yes, when Indent AND and OR conditions is on. Each top-level condition goes on its own indented line under WHERE, HAVING or ON. With the option off they stay on the clause line.

Does it validate the query?

No. It recognises keywords and clause boundaries, not grammar or schema. A query that would fail on the server still formats. Use a database client to check correctness.

Are subqueries and CTEs indented?

Yes. A parenthesised SELECT or WITH block starts one level in, and its closing parenthesis returns to the outer level on its own line. A second CTE after a comma starts on a new line.

Does it change quoted identifiers or strings?

No. Text inside double quotes, backticks, square brackets and single quotes is kept as written. A keyword inside a string or a quoted name is not uppercased.

Why was my note returned unchanged?

If the text contains no clause word such as SELECT, FROM or WITH, the page leaves it exactly as written and says so. That stops ordinary prose from having words uppercased and a semicolon added.

Does it support PostgreSQL or MySQL syntax?

Ordinary queries format, but dialect-specific constructs such as dollar-quoted strings, DELIMITER blocks and procedural bodies are not handled specially. Check the output before running it.

Can I put each column on its own line?

Not from the form. Commas inside a select list stay on one line. Add the line breaks after formatting, or use a formatter with a one-column-per-line option.

Is my query uploaded?

No. Formatting runs in the browser with no account or server copy. Uploaded files are read locally and capped at 5 MB. Copy or download the result before leaving the page.