SQL Formatter

Free SQL formatter: indent and beautify queries for MySQL, PostgreSQL, SQL Server, SQLite and more, with keywords in capitals and comments kept.

Runs in your browser. Files never leave your device.
Formatted SQL

About the SQL Formatter

Paste a long one-line query and this SQL formatter lays it out the way a person would write it: each clause (SELECT, FROM, WHERE, GROUP BY, ORDER BY and the rest) on its own line in capitals, with the columns, tables and conditions under it indented by two spaces. Reach for it when a query copied from a log, an ORM or a colleague's message has to be read, reviewed or debugged.

Pick the dialect the query was written for, from Standard SQL, MySQL, MariaDB, PostgreSQL, SQLite, SQL Server, Oracle or BigQuery. The result updates as you type, and Copy SQL puts the tidy version on your clipboard. The query is never run and never leaves your browser, which matters when it contains table names or customer data.

How the SQL is laid out

The formatter reads the query the way a database would, token by token, so it knows the difference between a keyword, a column name, a quoted string and a comment. Clause keywords start a new line and the items that belong to them are indented beneath, one per line for column lists and one condition per line for AND and OR. Joins stay whole, so LEFT JOIN orders o ON ... sits on one line under FROM.

Subqueries, CASE expressions and bracketed groups of conditions are indented one more level, so you can see where each one starts and ends. Short lists in brackets, such as IN (1, 2) or a function's arguments, stay on one line.

Only whitespace and the capitalisation of keywords change. Text inside quotes, comments of both kinds, and the names of your tables, columns and functions are kept exactly as you wrote them, so the formatted query does the same thing as the original.

Assumptions and limits

  • Keywords such as SELECT, AND, AS and DESC are written in capitals. Function names, data types and your own names keep the case you typed, so count(o.id) and decimal(10, 2) stay in lower case if that is how they came in.
  • Each dialect knows its own keywords, functions, quoting and parameter styles, such as [brackets] and @id in SQL Server, `backticks` in MySQL or $1 in PostgreSQL. Placeholders written as ? or :name, as PDO and most ORMs write them, are accepted in Standard SQL, MySQL and MariaDB. If a query won't format or comes out oddly, choose the dialect it was written for.
  • The query is read but never run, so it isn't checked against your database. An unclosed bracket or quote gets a message with the line and column. A query that stops halfway, such as one ending in WHERE, is still laid out as far as it goes, and a misspelt column or table name isn't noticed at all.
  • A -- comment stays at the end of the line it was on, and a /* block comment */ stays where it was, so formatting never hides code inside a comment.
  • The layout is fixed: two-space indents and one column per line. If your team uses a different style, such as leading commas, adjust it after copying.

Worked examples

Format a SELECT query from a log file

A filtered, sorted query copied as one long line becomes a block with each clause on its own line and its contents indented under it. The string values are left exactly as they were.

Input:
select name, email from customers where country = 'PK' and signup_date >= '2026-01-01' order by signup_date desc limit 20

Output:
SELECT
  name,
  email
FROM
  customers
WHERE
  country = 'PK'
  AND signup_date >= '2026-01-01'
ORDER BY
  signup_date DESC
LIMIT
  20

Beautify an UPDATE statement

Each column being changed gets its own line under SET, so every change is easy to check before you run it against a live database. The now() call keeps its lower-case name and its bracket.

Input:
update products set price = price * 1.1, updated_at = now() where category_id = 4 or category_id = 7;

Output:
UPDATE products
SET
  price = price * 1.1,
  updated_at = now()
WHERE
  category_id = 4
  OR category_id = 7;

A UNION with pagination

select a from x union select a from y order by a limit 10 offset 20 comes out with UNION on its own line between the two SELECT blocks, then ORDER BY, LIMIT and OFFSET at the same level, which makes it clear the sort and paging apply to the combined result.

How to use the SQL Formatter

  1. Paste your SQL. Put the raw query in the left box and choose its dialect; Standard SQL works for most everyday queries.
  2. Read the formatted SQL. The right box updates as you type, one clause per line with keywords in capitals and the details indented.
  3. Copy SQL. Press Copy SQL and paste the result into your editor, pull request or database client.

Frequently asked questions

Which SQL dialects does the formatter support?

Standard SQL, MySQL, MariaDB, PostgreSQL, SQLite, SQL Server (T-SQL), Oracle (PL/SQL) and BigQuery. Choose one from the dialect list so the formatter knows its keywords, quoting and parameter styles, such as TOP and @id in SQL Server or RETURNING and $1 in PostgreSQL.

Does formatting change what my query does?

No. Only whitespace and the capitalisation of keywords change, and SQL ignores both. Quoted strings, comments and your table, column and function names are kept exactly as written, so a keyword inside a string such as 'from here or there' is left alone.

Can it check my SQL for syntax errors?

Only in a small way. It reads the structure of the query, so an unclosed bracket or quote gets a message with the line and column. It doesn't run the query, so a query cut off after WHERE, a stray comma, a missing column or a wrong table name won't be reported; run the formatted query in your database client, or use its EXPLAIN command, to check it.

Why are some words still in lower case?

Because only SQL keywords are capitalised. Function names such as count or now and data types such as decimal keep the case you typed, along with your own table and column names, so mixed input may need a quick tidy if your style guide wants those in capitals too.

How do I format SQL that has comments in it?

Paste it as it is. Both -- line comments and /* block comments */ are kept, and a -- comment stays at the end of its own line, so the code after it is never swallowed into the comment.

Is my SQL query sent to a server?

No. The formatting runs in your browser as you type, so queries containing table names, customer emails or other private values stay on your own device.

This tool runs entirely in your browser. What you enter or upload stays on your device and is never sent to our servers.