SQL Formatter: How to Format and Read SQL Queries
Learn how to format SQL queries for readability, common formatting rules, dialect differences, and how to use SQL formatters effectively.
SQL queries can quickly become unreadable as they grow. A single-line 200-character query is impossible to debug. SQL formatting transforms messy queries into readable, maintainable code.
Why Format SQL?
Readability
Unformatted:
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 > '2026-01-01' AND u.status = 'active' GROUP BY u.id, u.name, u.email HAVING COUNT(o.id) > 5 ORDER BY total_spent DESC LIMIT 20;
Formatted:
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 > '2026-01-01'
AND u.status = 'active'
GROUP BY u.id, u.name, u.email
HAVING COUNT(o.id) > 5
ORDER BY total_spent DESC
LIMIT 20;
The formatted version is immediately scannable — you can see the SELECT, FROM, WHERE, and other clauses at a glance.
Debugging
When a query fails, formatting helps you find syntax errors, missing commas, and incorrect joins. It's much easier to spot a missing WHERE clause in a formatted query.
Code Review
Team members can review formatted queries much faster. In pull requests, formatted diffs show exactly what changed.
Formatting Rules
Keywords on Separate Lines
Place each major SQL keyword on its own line:
SELECT
FROM
WHERE
JOIN
GROUP BY
HAVING
ORDER BY
LIMIT
Indent Subqueries
SELECT *
FROM (
SELECT user_id, COUNT(*) AS cnt
FROM orders
GROUP BY user_id
) AS user_orders
WHERE cnt > 5;
Indent JOIN Conditions
SELECT
u.name,
o.total
FROM users u
INNER JOIN orders o
ON u.id = o.user_id
AND o.status = 'completed';
Comma Placement
Two conventions — both are acceptable:
Trailing commas (more common):
SELECT
id,
name,
email
FROM users;
Leading commas:
SELECT
id
,name
,email
FROM users;
SQL Dialects
Different databases have slightly different SQL syntax:
| Feature | MySQL | PostgreSQL | SQLite |
|---------|-------|------------|--------|
| String concat | CONCAT() | || or CONCAT() | || |
| Limit | LIMIT n | LIMIT n | LIMIT n |
| Offset | LIMIT n OFFSET m | LIMIT n OFFSET m | LIMIT n OFFSET m |
| Auto-increment | AUTO_INCREMENT | SERIAL | AUTOINCREMENT |
| Boolean | TINYINT(1) | BOOLEAN | INTEGER |
| Current timestamp | NOW() | NOW() | datetime('now') |
When formatting, be aware of which dialect you're targeting.
Format in Code
JavaScript (sql-formatter)
import { format } from 'sql-formatter';
const sql = "SELECT id,name,email FROM users WHERE status='active' ORDER BY name";
// Default (standard SQL)
format(sql);
// SELECT
// id,
// name,
// email
// FROM users
// WHERE status = 'active'
// ORDER BY name
// PostgreSQL dialect
format(sql, { language: 'postgresql' });
Python (sqlparse)
import sqlparse
sql = "SELECT id,name,email FROM users WHERE status='active'"
formatted = sqlparse.format(sql, reindent=True, keyword_case='upper')
print(formatted)
Command Line
# Using sqlformat (Python)
echo "select id,name from users" | sqlformat --reindent --keywords upper
Common Patterns
SELECT with Multiple Joins
SELECT
u.name,
o.id AS order_id,
p.name AS product_name,
oi.quantity
FROM users u
INNER JOIN orders o ON u.id = o.user_id
INNER JOIN order_items oi ON o.id = oi.order_id
INNER JOIN products p ON oi.product_id = p.id
WHERE o.created_at > '2026-01-01';
Aggregate Query
SELECT
DATE_TRUNC('month', created_at) AS month,
status,
COUNT(*) AS order_count,
AVG(total) AS avg_total
FROM orders
GROUP BY 1, 2
ORDER BY 1 DESC, 2;
CTE (Common Table Expression)
WITH monthly_stats AS (
SELECT
DATE_TRUNC('month', created_at) AS month,
COUNT(*) AS orders,
SUM(total) AS revenue
FROM orders
GROUP BY 1
)
SELECT
month,
orders,
revenue,
LAG(revenue) OVER (ORDER BY month) AS prev_month
FROM monthly_stats;
Best Practices
- Format consistently — pick a style and stick with it across your codebase
- Use a formatter — don't format manually; use sql-formatter or your IDE's formatter
- Uppercase keywords —
SELECT,FROM,WHEREshould be uppercase - Align columns — makes SELECT lists scannable
- Use aliases —
FROM users uis cleaner thanFROM users users
Try It Now
Use our free SQL Formatter to format your queries instantly — with dialect selection (MySQL, PostgreSQL, SQLite) and syntax highlighting.