SQL Formatter Online — Format and Beautify SQL Queries
A SQL formatter adds consistent indentation and line breaks to SQL queries, making complex queries readable. Here's how SQL formatting works and the style conventions used in...
A SQL formatter takes a dense, unindented SQL query and outputs it with consistent capitalization, indentation, and line breaks. The query logic doesn’t change — only the presentation. The result is a query that’s readable, reviewable, and maintainable.
Use the SQL Formatter to format SQL queries for MySQL, PostgreSQL, SQLite, and other databases.
Before and after formatting
Before:
select u.id,u.name,u.email,count(o.id) as order_count,sum(o.total) as total_spent from users u left join orders o on u.id=o.user_id where u.created_at>'2024-01-01' and u.status='active' group by u.id,u.name,u.email having count(o.id)>0 order by total_spent desc limit 10
After:
SELECT
u.id,
u.name,
u.email,
COUNT(o.id) AS order_count,
SUM(o.total) AS total_spent
FROM users u
LEFT JOIN orders o ON u.id = o.user_id
WHERE
u.created_at > '2024-01-01'
AND u.status = 'active'
GROUP BY
u.id,
u.name,
u.email
HAVING COUNT(o.id) > 0
ORDER BY total_spent DESC
LIMIT 10;
Same query. The formatted version makes it immediately clear: what’s being selected, from where, with what conditions, and in what order.
SQL formatting conventions
Keywords in uppercase
The most universal SQL convention is uppercase keywords: SELECT, FROM, WHERE, JOIN, GROUP BY, ORDER BY, HAVING, LIMIT. This visually separates SQL syntax from identifiers and values.
Some teams use lowercase keywords for less visual weight — both are acceptable as long as the codebase is consistent.
One clause per line
Each major clause starts on a new line:
SELECT id, name
FROM users
WHERE status = 'active'
ORDER BY name;
Column list on separate lines
For SELECT with multiple columns, indent each column:
SELECT
id,
name,
email,
created_at
FROM users;
JOIN conditions aligned
SELECT *
FROM orders o
INNER JOIN users u ON o.user_id = u.id
LEFT JOIN products p ON o.product_id = p.id
WHERE o.status = 'pending';
Indented subqueries
SELECT *
FROM (
SELECT user_id, COUNT(*) AS cnt
FROM orders
GROUP BY user_id
) order_counts
WHERE cnt > 5;
SQL style guides
There’s no universal standard, but two SQL style guides are widely referenced:
Simon Holywell’s SQL Style Guide — uppercase keywords, underscored names, aligned column aliases. Conservative, widely adopted.
GitLab SQL Style Guide — similar conventions, with explicit guidance on performance and join ordering for large tables.
The SQL Style Guide blog post covers the most important conventions.
Formatting in different SQL dialects
SQL syntax varies across databases:
| Database | Specific syntax | Notes |
|---|---|---|
| MySQL | LIMIT 10 OFFSET 5 | Backtick quoting: `table_name` |
| PostgreSQL | LIMIT 10 OFFSET 5 | Double-quote identifiers: "table_name" |
| SQL Server | TOP 10 instead of LIMIT | Square bracket quoting: [table_name] |
| SQLite | LIMIT 10 OFFSET 5 | Flexible quoting |
| Oracle | ROWNUM <= 10 or FETCH FIRST 10 ROWS | Most verbose syntax |
The SQL Formatter lets you select the target dialect so formatting matches the database you’re using.
Complex SQL constructs
CTEs (Common Table Expressions)
WITH
monthly_sales AS (
SELECT
DATE_TRUNC('month', created_at) AS month,
SUM(amount) AS total
FROM orders
GROUP BY 1
),
running_total AS (
SELECT
month,
total,
SUM(total) OVER (ORDER BY month) AS cumulative
FROM monthly_sales
)
SELECT *
FROM running_total
ORDER BY month;
CTEs should be formatted with each CTE on its own indented block.
Window functions
SELECT
user_id,
order_date,
amount,
SUM(amount) OVER (
PARTITION BY user_id
ORDER BY order_date
ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW
) AS running_total
FROM orders;
CASE expressions
SELECT
name,
CASE
WHEN score >= 90 THEN 'A'
WHEN score >= 80 THEN 'B'
WHEN score >= 70 THEN 'C'
ELSE 'F'
END AS grade
FROM students;
Why SQL formatting matters for production
Code review: Unformatted SQL in code reviews is hard to review. A reviewer can’t quickly verify join conditions or spot missing WHERE clauses in a 200-character single line.
Query analysis: Database EXPLAIN plans reference the query structure. When debugging a slow query, formatted SQL makes it easier to identify which clause is causing the scan.
Maintenance: SQL in stored procedures, views, and application code is read far more than it’s written. Formatted SQL reduces the cognitive load of every future reader.
Version control diffs: Formatted SQL produces meaningful diffs — you see exactly which line changed. Unformatted SQL shows the entire one-line query as changed even for minor modifications.
Related tools
- SQL Formatter — format SQL for any database dialect
- SQL Style Guide — conventions and best practices
- JSON Formatter — format JSON API responses alongside your SQL
Related posts
- SQL Style Guide: The Things That Actually Matter — Keyword casing, indentation, naming conventions, CTEs vs subqueries — the practi…
- SQL CTEs (Common Table Expressions) — WITH Clause Guide — Common Table Expressions (CTEs) use the WITH clause to create named temporary re…
- SQL GROUP BY and HAVING — Aggregate Data with Filters — GROUP BY groups rows into summary rows; HAVING filters the groups. Learn how to …
- JSON Formatter — Why Formatting JSON Matters and How It Works — A JSON formatter takes compact, hard-to-read JSON and adds whitespace and indent…
Related tool
Format and beautify SQL queries. 12 dialect options (PostgreSQL, MySQL, SQLite, MSSQL, BigQuery, Snowflake, and more). Keyword casing control.
Written by Mian Ali Khalid. Part of the Data & Format pillar.