Skip to content

SQL Formatter

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

Paste a query and the formatter puts SELECT, FROM, WHERE, JOIN, GROUP BY and ORDER BY on their own lines, indents what follows, and breaks columns and AND / OR conditions one per line. A 41-character one-liner such as select a, b from t where x = 1 and y = 2 becomes 8 readable lines.

About this tool

Queries copied from application logs, ORM debug output or a colleague's chat message usually arrive as a single long line, and reading the join conditions in that form is slow. This formatter lays the query out clause by clause: every major keyword starts a line, selected columns and conditions are indented one per line, and a subquery inside parentheses is pushed a level deeper so you can see where it starts and ends. Text inside quotes, quoted identifiers and comments is left exactly as written, so a string like 'from where' is never mistaken for SQL. You also get a count of statements, lines and keywords. It is a layout tool, not a parser: it does not check that your SQL is valid for a particular database, though it warns about unbalanced parentheses and an unclosed string.

How to use it

  1. Paste the query

    Paste one or several statements into the box, or open a .sql or .txt file from your device.

  2. Pick the keyword case

    Choose UPPERCASE, lowercase or keep keywords as you typed them.

  3. Pick the indent

    Indent with 2 spaces, 4 spaces or a tab to match your codebase.

  4. Copy the result

    The formatted SQL updates as you type; use Copy result to take it back to your editor.

Examples

One-line SELECT

SQL query
select a, b from t where x = 1 and y = 2
Keyword case
UPPERCASE
Indent
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; strings, quoted names and comments stay intact.
  2. Placed each clause keyword on its own line and indented its contents with 2 spaces.
  3. Broke top-level commas and AND / OR conditions onto new lines; 1 statement(s), 8 lines.

Each clause gets its own line, each selected column sits on its own indented line, and the second condition starts with AND.

Two statements in one paste

SQL query
SELECT COUNT(*) FROM users; DELETE FROM logs WHERE id < 10;
Keyword case
lowercase
Indent
4 spaces

Result Formatted SQL: select
count(*)
from
users;

delete from
logs
where
id < 10;
Statements: 2
Lines: 9
Keywords: 6
Tokens: 16

  1. Split the input into 16 tokens; strings, quoted names and comments stay intact.
  2. Placed each clause keyword on its own line and indented its contents with 4 spaces.
  3. Broke top-level commas and AND / OR conditions onto new lines; 2 statement(s), 9 lines.

The semicolon ends each statement, a blank line separates them, and keywords are lowered to match a lowercase house style.

How it is calculated

tokens → clause keyword starts a line at depth d; its items go on lines at depth d + 1; a subquery raises d by 2

tokens
words, numbers, operators, quoted strings, quoted names and comments, read left to right
d
current indent depth, 0 at the start of each statement

The query is first split into tokens so that anything inside single quotes, double quotes, backticks, square brackets or comments stays as one piece. Clause keywords, including two- and three-word ones such as GROUP BY and LEFT OUTER JOIN, start a new line. Commas at the top level of a clause and AND / OR conditions start a new indented line, while commas inside a function call such as COALESCE(a, b) stay inline. The AND inside BETWEEN x AND y is not broken.

When not to use it

  • To check whether a query will run, since the formatter does not validate syntax against any database.
  • For stored procedures with dialect-specific blocks such as BEGIN … END, where the layout rules here are too simple.

Common mistakes

  • Expecting lowercase mode to change table or column names; only recognised SQL keywords change case.
  • Pasting a query with an unclosed quote, which makes everything after it part of one string.

Frequently asked questions

Will formatting change what my query does?

No. Only whitespace and the letter case of keywords change. Strings, quoted identifiers, numbers and comments are kept character for character, and SQL keywords are case-insensitive in every mainstream database.

Which SQL dialects does it handle?

The layout works on standard SQL and the common syntax shared by PostgreSQL, MySQL, SQLite and SQL Server, including backtick and square-bracket quoted names. Dialect-specific procedural code is passed through but not laid out specially.

Can it format several statements at once?

Yes. Each semicolon ends a statement, and the output places a blank line between statements. The Statements figure counts them, including a final statement without a semicolon.

Why are commas inside COUNT or COALESCE not split onto new lines?

Commas inside a function call's parentheses are arguments, not list items, so they stay inline. Only commas at the top level of a clause, or directly inside a subquery, start a new line.

Why do I see a warning about parentheses?

The number of opening and closing parentheses differs. The layout is still produced, but indentation after the missing bracket may be off, and the database would reject the query anyway.

Is my SQL sent to a server?

No. Formatting runs in your browser, so table names and literal values in the query stay on your machine.

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 Formatter. Tools Directory. Retrieved October 26, 2026, from https://tools.milunir.com/developer-tools/developer-reference/sql-formatter/

MLA

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

Chicago

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

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