Skip to content

SQL Beautifier

Keyword case
Indent with
Formatted SQL —
Statements
—
Lines
—
Keywords
—
Tokens
—

Paste a query and the beautifier puts each clause (SELECT, FROM, WHERE, GROUP BY, ORDER BY) on its own line, lists columns one per line and starts every JOIN, AND or OR on a new indented line. For example, select a,b from t where x=1 and y=2 becomes an 8-line block with four uppercase keywords.

About this tool

Queries copied from application logs, ORM debug output or a colleague's chat message usually arrive as one long line, which makes a missing join condition or a misplaced OR hard to spot. This beautifier re-prints the query with a consistent layout: main clauses at the left margin, their contents indented beneath, columns split at commas, and subqueries pushed in a further step. Keywords can be shown in uppercase, lowercase or left as typed, and indentation can be two spaces, four spaces or a tab. Quoted strings, quoted identifiers and comments are copied through character for character. It is a layout tool, not a parser: it does not check that the query is valid for MySQL, PostgreSQL or SQL Server, and procedural code such as stored procedure bodies keeps only basic formatting.

How to use it

  1. Add the query

    Paste SQL into the box or open a .sql or .txt file from your device. Several statements separated by semicolons are fine.

  2. Choose keyword case

    Pick UPPERCASE, lowercase or Keep as typed. Only recognised SQL keywords change; table and column names are never touched.

  3. Choose the indent

    Select 2 spaces, 4 spaces or Tab to match your team's style guide or editor settings.

  4. Copy the result

    The formatted query updates as you type; copy it back into your editor, migration file or pull request.

Examples

A one-line SELECT

SQL query
select a,b from t where x=1 and y=2
Keyword case
UPPERCASE
Indent with
2 spaces

Result Formatted SQL: SELECT
a,
b
FROM
t
WHERE
x = 1
AND y = 2
Statements: 1
Lines: 8
Keywords: 4
Tokens: 14

  1. Split the input into 14 tokens: words, numbers, quoted strings, comments and symbols.
  2. Started a new line at each main clause (SELECT, FROM, WHERE, GROUP BY, ORDER BY and so on) and each JOIN, AND or OR.
  3. Broke column lists after each comma outside function brackets and indented subqueries by two extra levels.
  4. Applied uppercase to 4 keywords; the result has 8 lines.

Each clause gets its own line, the two columns are listed one per line, and the AND condition starts a fresh indented line.

A join with GROUP BY, 4-space indent

SQL query
select count(*) as n, u.name from users u left join orders o on o.uid=u.id group by u.name order by n desc
Keyword case
UPPERCASE
Indent with
4 spaces

Result Formatted SQL: SELECT
COUNT(*) AS n,
u.name
FROM
users u
LEFT JOIN orders o ON o.uid = u.id
GROUP BY
u.name
ORDER BY
n DESC
Statements: 1
Lines: 10
Keywords: 12
Tokens: 35

  1. Split the input into 35 tokens: words, numbers, quoted strings, comments and symbols.
  2. Started a new line at each main clause (SELECT, FROM, WHERE, GROUP BY, ORDER BY and so on) and each JOIN, AND or OR.
  3. Broke column lists after each comma outside function brackets and indented subqueries by two extra levels.
  4. Applied uppercase to 12 keywords; the result has 10 lines.

The LEFT JOIN sits under FROM with its ON condition on the same line, and COUNT(*) stays together because commas and brackets inside functions are left inline.

A subquery in lowercase

SQL query
SELECT * FROM t WHERE id IN (SELECT id FROM x)
Keyword case
lowercase
Indent with
2 spaces

Result Formatted SQL: select
*
from
t
where
id in (
select
id
from
x
)
Statements: 1
Lines: 11
Keywords: 6
Tokens: 13

  1. Split the input into 13 tokens: words, numbers, quoted strings, comments and symbols.
  2. Started a new line at each main clause (SELECT, FROM, WHERE, GROUP BY, ORDER BY and so on) and each JOIN, AND or OR.
  3. Broke column lists after each comma outside function brackets and indented subqueries by two extra levels.
  4. Applied lowercase to 6 keywords; the result has 11 lines.

The inner SELECT is indented one step past the outer clauses and its closing bracket lines up under the condition that opened it.

How it is calculated

tokens = words, numbers, quoted text, comments, symbols; new line before each clause, JOIN, AND, OR and top-level comma

clause
SELECT, FROM, WHERE, GROUP BY, ORDER BY, HAVING, LIMIT, VALUES, SET, UPDATE, INSERT, DELETE and set operators such as UNION
level
the indent depth: clause contents sit one level in, subqueries two levels further

The input is split into tokens first, so anything inside single quotes, double quotes, backticks, square brackets or comments is treated as one unit and never reflowed. The printer then walks the tokens: a clause keyword starts a line at the current depth, its contents go one level deeper, and a comma outside function brackets ends the line. An opening bracket followed by SELECT is treated as a subquery and indented; other brackets, such as COUNT(*) or IN (1, 2, 3), stay on one line. Operators get a single space on each side.

When not to use it

  • To check whether a query will run, because the beautifier does not validate syntax against any database dialect.
  • For long stored procedures with IF, LOOP or DECLARE blocks, where a dialect-aware formatter in your IDE handles control flow better.

Common mistakes

  • Assuming a neat layout means the query is correct; a missing ON condition still formats cleanly.
  • Switching keyword case on a query that uses an unquoted reserved word as a column name, then being surprised it changes case too.
  • Pasting only part of a statement, which leaves brackets unbalanced and the indentation shifted.

Frequently asked questions

Will it change my string values or column names?

No. Text inside single quotes, double quotes, backticks or square brackets is copied exactly, and identifiers keep their original case. Only recognised SQL keywords such as select, from or left join are changed when you pick UPPERCASE or lowercase.

Which SQL dialects does it handle?

The layout rules are dialect-neutral, so standard SELECT, INSERT, UPDATE and DELETE queries from MySQL, PostgreSQL, SQLite, SQL Server and Oracle all format the same way. Quoting styles from each, including MySQL backticks and SQL Server square brackets, are recognised as single tokens.

Why are keywords usually written in uppercase?

SQL keywords are case-insensitive, so the database does not care. Uppercase is a long-standing convention that makes the structure of a query stand out from table and column names, and many style guides, including Simon Holywell's SQL Style Guide, recommend it. Lowercase is equally valid if your team prefers it.

What happens to comments?

Line comments starting with — are kept and followed by a line break, so they never swallow the code after them. Block comments in /* */ are kept inline where they appeared.

Can I format several statements at once?

Yes. Statements separated by semicolons are formatted one after another with a blank line between them, and the Statements figure tells you how many were found.

Why is there a warning about an unclosed string?

A single quote with no partner makes everything after it part of one string, so the rest of the query cannot be laid out. Check for an apostrophe inside a value, which SQL escapes by doubling it, as in 'O''Brien'.

Is my query sent to a server?

No. Tokenising and formatting run in your browser, so table names, credentials pasted by mistake and customer data in literals stay on your device.

Cite this tool

Using this tool in an assignment, article or report? Copy a citation, or a link for your website.

APA

Tools Directory Editorial Team. (2026, October 23). SQL Beautifier. Tools Directory. Retrieved October 26, 2026, from https://tools.milunir.com/developer-tools/developer-reference/sql-beautifier/

MLA

Tools Directory Editorial Team. “SQL Beautifier.” Tools Directory, 23 Oct. 2026, https://tools.milunir.com/developer-tools/developer-reference/sql-beautifier/. Accessed 26 Oct. 2026.

Chicago

Tools Directory Editorial Team. “SQL Beautifier.” Tools Directory. Last modified October 23, 2026. Accessed October 26, 2026. https://tools.milunir.com/developer-tools/developer-reference/sql-beautifier/.

Link<a href="https://tools.milunir.com/developer-tools/developer-reference/sql-beautifier/">SQL Beautifier – Tools Directory</a>