Skip to main content
~/makemydev/sql-formatter

$sql-formatter

Formatters

Paste a query and get a clean, consistently-indented version. Supports SELECT, INSERT, UPDATE, DELETE, CREATE, CTEs, window functions, and multi-statement scripts. Runs entirely in your browser.

SELECT
  u.id,
  u.name,
  count(o.id) AS order_count
FROM users u
LEFT JOIN orders o
  ON o.user_id = u.id
  AND o.status = 'paid'
WHERE u.country = 'US'
  AND u.created_at > '2024-01-01'
GROUP BY
  u.id,
  u.name
HAVING count(o.id) > 0
ORDER BY
  order_count DESC
LIMIT 50;
Options

How to format and minify SQL

Paste any SQL into the input panel and pick Format for a pretty-printed version or Minify to strip whitespace and comments. The formatter understands the common statement shapes — SELECT / INSERT / UPDATE / DELETE / CREATE, CTEs, window functions, subqueries, and CASE expressions — and lays them out with the structure that makes code reviews easy to scan. Every byte stays in your browser.

  1. Paste your query. Multi-statement scripts are fine — the counter in the header tells you how many statements the formatter detected.
  2. Pick a case style. Upper-case keywords are the classic convention and make reviews easier; lower-case fits a modern prose style; preserve leaves your keywords as-typed.
  3. Pick indent & commas. 2 spaces is the default; 4 spaces matches most team style guides; tabs work in editors with configurable tab widths. Leading commas (, col) help spot missing items in long SELECT lists.
  4. Copy, save, or minify. The formatted query is available for copy, download, or you can flip to Minify when you need a one-liner for a tool that does not cope well with line breaks.

What the formatter handles

  • Major clauses on their own line — SELECT, FROM, WHERE, GROUP BY, HAVING, ORDER BY, LIMIT, and the JOIN family all start a new line at the current statement indent.
  • Column lists vertical — items after SELECT, GROUP BY, ORDER BY, SET, and RETURNING break across lines so diffs show one logical change per row.
  • JOIN ... ON nested — the ON predicate sits one level deeper than the JOIN line so it is clearly associated with that table.
  • AND / OR wrapped at clause indent — each new boolean term in a WHERE or HAVING breaks to its own line aligned with the clause body, so complex conditions are easy to follow.
  • CASE expressions expanded — CASE, WHEN, ELSE, and END line up vertically so branches are obvious at a glance.
  • CTEs and subqueries indented — WITH bodies and parenthesized SELECTs open on their own block with the inner statement indented one level deeper.
  • Postgres, MySQL, and SQL Server identifier quoting — double quotes, backticks, and square brackets are preserved as-is.

When to reach for the minifier

Minify strips comments and every non-essential character so the query fits on one line. That is the right form when you need to paste into a JSON field, an environment variable, a log message, or a URL query string. For readable source control, always prefer the formatted version.

Limitations

This is a pretty-printer, not a parser — it does not execute the query, validate columns, or reject invalid syntax. Vendor-specific DDL (stored-procedure bodies, PL/pgSQL blocks, T-SQL BEGIN…END blocks) will be preserved token-for-token but may not be re-indented beyond the surrounding statement. Always diff the formatted output against the original before committing.

// how-to

How to format and minify SQL

Paste a SQL query and get a pretty-printed version with major clauses on their own lines, or strip the query down to a single line for pasting into JSON, env vars, or log messages.

  1. Paste the SQL

    Drop any query — SELECT, INSERT, UPDATE, DELETE, CREATE, or a multi-statement script — into the input panel. The statement counter tells you how many the formatter detected.

  2. Choose Format or Minify

    Format indents for reading. Minify removes comments and collapses whitespace so the query fits on one line for transport through systems that dislike line breaks.

  3. Pick style options

    Tune keyword case (UPPER, lower, preserve), indent (2 / 4 spaces or tab), comma placement (trailing or leading), and whether data types should be upper-cased.

  4. Copy or download

    Grab the result to clipboard or save it as .sql. Everything runs locally — your query is never uploaded.

// faq

? Does my query get sent to a server?
No. Tokenization, formatting, and minification all run in your browser. There is no network call tied to the tool.
? Which SQL dialects does it handle?
The formatter targets shapes common across Postgres, MySQL, and SQL Server — CTEs, window functions, JOIN variants, and the three identifier quoting styles (double quotes, backticks, square brackets). Vendor-specific procedural code (PL/pgSQL, T-SQL triggers) is preserved token-for-token but not specially indented.
? Why is my stored procedure body not indented?
The formatter is a pretty-printer, not a parser. Bodies like PL/pgSQL DO blocks, MySQL stored procedures, and T-SQL BEGIN…END blocks are treated as opaque — the surrounding statement is formatted, but the procedural body keeps its inner layout. Use a vendor-specific tool for those.
? Leading or trailing commas — which should I pick?
Trailing commas are the default most style guides adopt. Leading commas make it easier to spot a missing comma in long SELECT lists and simpler to comment out a line without breaking the syntax. Pick whatever your team has agreed on.
? Will minifying change semantics?
Minify preserves every token and only touches whitespace and comments. Comment-based optimizer hints (MySQL /*+ … */, Oracle hints) are dropped because the minifier strips all comments — be careful with dialects that use comments meaningfully.