SQL Formatter & Beautifier
Format and beautify SQL queries with syntax highlighting. Support for MySQL, PostgreSQL, SQL Server. Free online SQL formatter and pretty printer
SQL formatting is the difference between a 400-character line you cannot parse and a structured query where you can see the JOIN graph at a glance. This formatter handles dialect-specific syntax (Postgres CTEs, MySQL backticks, BigQuery STRUCT literals, T-SQL square brackets) and keeps your query semantically identical — same parameter placeholders, same identifier casing, same string escaping. It runs locally, so production credentials embedded in queries do not leak.
Why "just indent it" is harder than it sounds
- Identifier quoting differs per dialect — Postgres and ANSI use "double quotes", MySQL uses `backticks`, SQL Server uses [square brackets]. A formatter that normalizes quotes will break case-sensitive identifiers in another dialect.
- String literals can contain SQL — 'SELECT 1' inside a string is a literal, not nested SQL. A naive tokenizer that splits on keywords will mangle dynamic SQL.
- CTEs and window functions need different indentation rules than plain SELECTs. WITH cte AS (SELECT ...) wants the inner SELECT indented one level under the CTE name; without that, deeply nested CTEs become unreadable.
- Parameter placeholders are not all the same — ? (positional), $1 (Postgres), :name (named), @name (SQL Server), %s (Python DB-API). Some formatters treat unknown placeholders as identifiers and "fix" the casing.
Working example: a CTE-heavy query
Input
with recent_orders as (select user_id,count(*) as c from orders where created_at > now() - interval '30 days' group by user_id),top_users as (select user_id from recent_orders where c > 10) select u.id,u.email,r.c from users u join recent_orders r on r.user_id=u.id where u.id in (select user_id from top_users) order by r.c desc
Output
WITH recent_orders AS (
SELECT
user_id,
COUNT(*) AS c
FROM
orders
WHERE
created_at > NOW() - INTERVAL '30 days'
GROUP BY
user_id
),
top_users AS (
SELECT
user_id
FROM
recent_orders
WHERE
c > 10
)
SELECT
u.id,
u.email,
r.c
FROM
users u
JOIN recent_orders r ON r.user_id = u.id
WHERE
u.id IN (
SELECT
user_id
FROM
top_users
)
ORDER BY
r.c DESCEach CTE indents its body one level. Subqueries in IN clauses get the same treatment. Keyword case is uppercase by convention but configurable. The interval string keeps its single quotes regardless of how the surrounding identifiers are quoted.
Dialects worth distinguishing
- PostgreSQL — double-quote identifiers, $$dollar quoting$$ for strings with embedded quotes, ::cast operator, RETURNING clause.
- MySQL — backtick identifiers, LIMIT n,m (count first) vs LIMIT m OFFSET n, REPLACE INTO, ON DUPLICATE KEY UPDATE.
- SQLite — flexible typing, no separate DATE type, AUTOINCREMENT on INTEGER PRIMARY KEY.
- SQL Server / T-SQL — [bracket-quoted] identifiers, TOP n instead of LIMIT, [N'' for Unicode strings, OUTPUT clause vs RETURNING.
- BigQuery — backtick or no quoting for project.dataset.table, STRUCT and ARRAY literals, qualified function namespaces.
- Snowflake — case-folding to uppercase by default for unquoted identifiers, FLATTEN for JSON, ::variant cast.
When to reach for this tool
- You pasted a logged production query (one massive line) and need to see the JOIN order before reasoning about the EXPLAIN plan.
- You are reviewing a PR with a 200-line stored procedure and the diff is unreadable because indentation varies between hunks.
- You inherited a SQL file generated by an ORM where every keyword is mixed case (Select, From, Where) and you want to normalize before reading.
- You are debugging a slow query and want to align two candidate rewrites side-by-side with identical formatting so the structural difference is obvious.
What this tool will not do
- It will not validate SQL semantics. Syntactically formattable does not mean executable — invalid column names, missing joins, type mismatches all format fine and fail at the database.
- It will not optimize the query. Reformatting changes whitespace; the query plan is identical before and after. For optimization, run EXPLAIN ANALYZE against a real instance.
- It will not handle every vendor extension. Oracle PL/SQL, T-SQL procedural blocks (BEGIN...END with DECLARE and CURSOR), and PostgreSQL DO $$...$$ blocks are formatted as best-effort; complex procedural code may need vendor-specific tools.
Queries are formatted entirely in your browser. Paste production SQL — including queries with embedded credentials or PII — without sending it to a server.
Frequently asked questions
Will the formatter change my query result?
No. SQL is whitespace-insensitive between tokens. Reformatting changes layout only — the optimizer sees the same query, returns the same rows.
Why does the formatter uppercase my keywords?
Convention. SQL keywords are case-insensitive, so SELECT and select are identical. Uppercase keywords distinguish them visually from identifiers and string literals. Change the option to "lower" if your team prefers all-lowercase.
My formatted query lost the JOIN type — INNER JOIN became JOIN. Is that OK?
Yes. INNER JOIN and JOIN are synonyms in every major SQL dialect. The formatter normalizes for brevity. The behavior is identical; only the source text changes.
How do I handle dynamic SQL with question-mark parameters?
The formatter preserves placeholders as-is. ? stays ?, $1 stays $1, :name stays :name. Dialect choice does not rewrite placeholder style — that would change parameter binding and is never safe.
Can it un-format minified SQL with no newlines?
Yes — any whitespace pattern works as input. The tokenizer rebuilds structure from the keywords and operators regardless of original line breaks.
Is there a way to format SQL inside Python or Java string literals?
Not directly — you have to extract the string content first. For ORM-style multi-line strings, copy the string body (without the surrounding quotes) into the formatter. Tools like sqlfluff can lint and format SQL inside Python files; this web tool does not parse host-language wrappers.
Related tools
Format and beautify JSON, XML, CSS, HTML, JavaScript, SQL code. Auto-indent and syntax highlight. Free online code beautifier and formatter
Compare two JSON documents with visual tree diff. Highlight added, removed, modified nodes. Expandable tree view. Free online JSON comparison tool
Run SQL queries in your browser with SQLite. Import CSV files, create tables, export results. Free online SQL editor and database sandbox
Visual ERD designer for database schemas. Create tables, define relationships, foreign keys. Export to SQL for MySQL, PostgreSQL, SQLite. Free online ERD tool
Build docker-compose.yml files visually. Add MySQL, PostgreSQL, Redis, MongoDB services. Free online Docker Compose YAML generator
Last updated · E-Utils editorial team