Skip to main content
Toollyz

Search tools

Search for a command to run...

SQL Formatter

Format messy SQL into readable, indented queries. Token-based formatter for the dialect-neutral subset (SELECT / INSERT / UPDATE / DELETE / CREATE / WITH / JOIN). Handles strings, comments, sub-queries. 100% client-side.

What is the SQL Formatter?

SQL Formatter is a small tokenizer-based formatter that handles the dialect-neutral subset of SQL most users write — SELECT / INSERT / UPDATE / DELETE / CREATE / ALTER, with WITH (CTEs), JOINs, sub-queries, line (`--`) and block (`/* */`) comments, single-quoted strings (with `''` escape), and identifier quoting for PostgreSQL (`"…"`), MySQL (`` `…` ``) and SQL Server (`[…]`). The output uses standard 2-space (or configurable) indentation and uppercases the 60+ reserved words by default. Top-level clauses (`SELECT`, `FROM`, `WHERE`, `GROUP BY`, `ORDER BY`, `LIMIT`, `OFFSET`, `INTO`, `VALUES`, `SET`, `WITH`) start on their own lines. JOINs indent under FROM. Multi-line WHERE conditions break before each `AND` / `OR`. Sub-queries inherit the depth of their surrounding clause. Comments are preserved verbatim. Pure functions, no upload.

How to use it

  1. Paste an unindented or messy SQL query — single-statement or multi-statement.
  2. Toggle UPPERCASE keywords (default on) and pick the indent width (2–8 spaces).
  3. Read the per-token stats — keywords, identifiers, strings, statements.
  4. Copy the formatted output or download as .sql.

Benefits

  • Token-based formatter — handles strings, comments and quoted identifiers correctly without regex hacks.
  • Recognises 60+ reserved words across SELECT / INSERT / UPDATE / DELETE / CREATE / ALTER / WITH / JOIN.
  • Multi-word clauses (GROUP BY, LEFT OUTER JOIN, IS NOT NULL) stay together on the same line.
  • Top-level clauses break onto their own line with consistent indentation.
  • WHERE conditions break before AND / OR for clean diffs in code review.
  • Configurable indent width (2-8 spaces) and an UPPERCASE-keywords toggle.
  • Comments — both `--` line and `/* */` block — are preserved verbatim.
  • Runs 100% in your browser — Toollyz has no server.

Frequently asked questions

Which SQL dialect does it format?

The dialect-neutral subset most users write — SELECT / INSERT / UPDATE / DELETE / CREATE / ALTER, with WITH, JOINs, sub-queries and the standard data types. Identifier quoting works for PostgreSQL (`"…"`), MySQL (` `…` `) and SQL Server (`[…]`) all in the same input.

Will it format my Postgres-specific syntax?

Yes for the common ones — `ON CONFLICT DO UPDATE`, `RETURNING`, `LATERAL JOIN`, `EXCLUDED.*` are all handled. Very dialect-specific operators (`@@`, `->>`, etc.) pass through verbatim.

Does it handle multi-statement input?

Yes. Each `;` ends a statement and we keep them visually separated. The token statistics count multiple statements.

Will it break inside string literals?

No. Single-quoted strings (with `''` for embedded quote) are recognised by the tokeniser and passed through unchanged.

Why are keywords UPPERCASE by default?

It's the SQL standard convention and improves readability — identifiers stay lower-case, keywords stand out. Turn off the toggle if your house style is all-lower.

Does it preserve my comments?

Yes — both `--` line comments and `/* */` block comments. Line comments break to a new line; block comments stay where they are.

How does it handle CTEs?

`WITH foo AS (…)` formats with the CTE name on its own indented line, the body inside the parens at the next depth, and the main SELECT after — same pattern Postgres docs use.

What about window functions like ROW_NUMBER() OVER (PARTITION BY …)?

PARTITION BY is in the multi-word clause list, so it stays together. The OVER clause renders inline with the function call.

Will it work on tens of thousands of lines?

The tokenizer is O(n), so yes — large migrations or fixture files format fine. The browser's textarea is the practical limit.

Can it format MySQL CREATE PROCEDURE bodies?

Partially — the IF / WHILE / LOOP keywords aren't in the top-clause list, so they're rendered inline. For procedure bodies you may want a dialect-specific tool.

Is anything uploaded?

No. Tokenising and re-indenting are pure browser computations.