SQL Formatter

Beautify or minify SQL queries with proper indentation, keyword casing, and structure.

BeautifyMinifyFormatSQL
Indent
Input SQL
167 chars
Formatted SQL
173 chars

All processing happens locally in your browser — your data never leaves your device.

SQL Formatter breaks a one-line or minified query into clauses so you can read it. SELECT, FROM, WHERE, JOIN, ON, GROUP BY, ORDER BY, HAVING, LIMIT, SET, VALUES, WITH and similar keywords each start a new line, AND and OR conditions are indented under their clause, and a line break follows every comma.

Recognised keywords are converted to uppercase, while table names, column names and function names keep the case you typed. Parenthesised subqueries and CTE bodies are indented one level, and CASE expressions put each WHEN, THEN and ELSE on its own line, closed by END.

Minify goes the other way: it removes -- and /* */ comments and collapses all whitespace into single spaces, which is handy for pasting a query into a log line, a config value or a code string. Both modes run in your browser, and the result can be copied or downloaded as a .sql file.

This is a lightweight tokenizer rather than a dialect-aware parser, so treat the output as a readability aid. To see what changed between two versions of a query, format both and paste them into the diff checker; if your database returns JSON columns, the JSON formatter makes those values readable.

  1. Paste your query into the Input SQL box.
  2. Choose 2 or 4 spaces with the Indent buttons.
  3. Click Beautify to split the query into clauses, or Minify to collapse it onto one line.
  4. Check the Formatted SQL panel, particularly around function calls and string literals.
  5. Copy the result, or use the download button to save it as formatted.sql.

  • Puts major clauses (SELECT, FROM, WHERE, JOIN variants, GROUP BY, ORDER BY, LIMIT, UNION and more) on new lines
  • Indents AND / OR conditions and parenthesised subqueries
  • Formats CASE ... WHEN ... THEN ... ELSE ... END blocks
  • Uppercases recognised keywords without touching identifiers
  • Keeps single-quoted strings, quoted identifiers and comments intact when beautifying
  • Minify mode strips comments and collapses whitespace
  • Copy or download the output as a .sql file

Beautify a filtered SELECT

Input:
select id,name,email from users where active=1 and deleted_at is null order by name

Output:
SELECT id,
name,
email
FROM users
WHERE active=1
  AND deleted_at IS NULL
ORDER BY name

Clauses start new lines and AND is indented. Operators such as = keep their original spacing.

JOIN with a condition

Input:
select o.id,c.name from orders o inner join customers c on c.id=o.customer_id where o.total>100

Output:
SELECT o.id,
c.name
FROM orders o
INNER JOIN customers c
ON c.id=o.customer_id
WHERE o.total>100

Multi-word keywords such as INNER JOIN are recognised as a single unit.

Minify a commented query

Input:
-- active users
SELECT id,
       name
FROM users
WHERE active = 1;

Output:
SELECT id, name FROM users WHERE active = 1;

The comment is removed and all line breaks and indentation collapse to single spaces.

Minify removed part of a string

Minify strips everything from -- to the end of the line, even when the -- is inside a quoted value such as 'a--b'.

Do not minify queries whose string literals contain -- or /*; use Beautify, or edit those lines manually.

A doubled-quote string was split

The tokenizer ends a string at the second quote in 'it''s' and inserts a space between the pieces.

Remove the added space after formatting, or use a backslash escape if your database supports it.

Column lists are not indented under SELECT

Line breaks after commas are placed at the current nesting depth, which is zero for a top-level SELECT.

Indent the column list by hand if your style guide requires it; the query itself is unchanged.

Deeply nested subqueries look uneven

The formatter tracks parentheses but has no understanding of query structure beyond keywords.

Format the inner query on its own first, or adjust the indentation manually for complex statements.

Which SQL dialects does it support?

It works on generic SQL syntax rather than a specific dialect. Standard statements from MySQL, PostgreSQL, SQLite or SQL Server format reasonably well, but dialect-specific keywords (for example RETURNING, ILIKE or MERGE) are not recognised, so they are neither uppercased nor placed on new lines.

Why are function calls like COUNT(*) split across lines?

Every opening parenthesis increases the indent and every closing one starts a new line, whether it belongs to a subquery or a function call. That keeps subqueries readable, but it also turns count(*) into "count( *" followed by ")" on the next line. It is a cosmetic quirk; the query still runs.

Are string literals preserved?

When beautifying, text inside single quotes is kept as a single token, and backslash-escaped quotes are handled. SQL-standard doubled quotes ('it''s') are read as two separate strings and a space is inserted between them, which changes the value, so check those by hand. Minify collapses whitespace everywhere, including inside strings.

Does Beautify change keyword casing?

Keywords the formatter knows (SELECT, FROM, JOIN, AND, NULL, AS and so on) are uppercased. Everything else keeps its original case, including function names like count() and sort directions like desc.

Is my query sent anywhere?

No. Beautify, Minify and the .sql download all run locally in your browser.