SQL is one of the most widely used programming languages in the world — yet it's also one of the most commonly written in an unreadable mess. Whether it's a one-liner that wraps three times, or a multi-join query with no indentation, poorly formatted SQL wastes time and introduces bugs.
This guide covers SQL formatting best practices, naming conventions, and style rules that make your queries readable and maintainable. When you need instant formatting, our SQL Formatter beautifies any query right in your browser — no data ever leaves your device.
Why SQL Formatting Matters
Consider this actual query found in a production codebase:
SELECT u.id,u.name,u.email,o.id as order_id,o.total,o.created_at,p.name as product_name 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 u.active=1 AND o.created_at>='2026-01-01' AND o.total>100 ORDER BY o.created_at DESC LIMIT 50;Now formatted properly:
SELECT
u.id,
u.name,
u.email,
o.id AS order_id,
o.total,
o.created_at,
p.name AS product_name
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 u.active = 1
AND o.created_at >= '2026-01-01'
AND o.total > 100
ORDER BY o.created_at DESC
LIMIT 50;The formatted version is immediately scannable. You can see the tables being joined, the filter conditions, and the output columns at a glance.
SQL Formatting Rules
1. One Clause Per Line
Each major SQL clause should start on its own line: SELECT, FROM, WHERE, JOIN, GROUP BY, HAVING, ORDER BY, LIMIT.
-- ❌ Bad
SELECT name, email FROM users WHERE active = 1 ORDER BY name;
-- ✅ Good
SELECT name, email
FROM users
WHERE active = 1
ORDER BY name;2. Indent Column Lists and Conditions
SELECT
u.id,
u.name,
u.email,
u.created_at
FROM users u
WHERE u.active = 1
AND u.role = 'admin'
AND u.last_login >= '2026-01-01';3. Uppercase SQL Keywords
While SQL is case-insensitive, uppercasing keywords (SELECT, FROM, WHERE) helps distinguish them from table and column names. This is the most widely adopted convention.
4. Use Meaningful Table Aliases
-- ❌ Bad — what is a, b, c?
SELECT a.name, b.total
FROM users a
JOIN orders b ON a.id = b.user_id
JOIN products c ON b.product_id = c.id;
-- ✅ Good — clear abbreviations
SELECT u.name, o.total
FROM users u
JOIN orders o ON u.id = o.user_id
JOIN products p ON o.product_id = p.id;5. Align JOIN Conditions
FROM orders o
INNER JOIN users u
ON o.user_id = u.id
LEFT JOIN shipping s
ON o.id = s.order_id
AND s.status = 'delivered';6. Use Common Table Expressions (CTEs) for Complex Queries
-- Instead of nested subqueries, use CTEs
WITH active_users AS (
SELECT id, name, email
FROM users
WHERE active = 1
AND last_login >= '2026-01-01'
),
recent_orders AS (
SELECT user_id, COUNT(*) AS order_count, SUM(total) AS total_spent
FROM orders
WHERE created_at >= '2026-01-01'
GROUP BY user_id
)
SELECT
au.name,
au.email,
ro.order_count,
ro.total_spent
FROM active_users au
INNER JOIN recent_orders ro
ON au.id = ro.user_id
ORDER BY ro.total_spent DESC;CTEs make complex queries readable by breaking them into named, logical steps. They also help with debugging — you can run each CTE independently.
SQL Naming Conventions
Tables and Columns
- Use
snake_casefor table and column names:user_profiles,created_at - Pluralize table names:
users,orders,products - Use descriptive names:
is_activenotflag,total_amountnotamt - Prefix boolean columns with
is_orhas_:is_active,has_verified_email - Timestamp columns:
created_at,updated_at,deleted_at
Primary and Foreign Keys
-- Primary key: id
CREATE TABLE users (
id BIGINT PRIMARY KEY GENERATED ALWAYS AS IDENTITY,
name TEXT NOT NULL
);
-- Foreign key: referenced_table_id
CREATE TABLE orders (
id BIGINT PRIMARY KEY GENERATED ALWAYS AS IDENTITY,
user_id BIGINT NOT NULL REFERENCES users(id),
total DECIMAL(10, 2) NOT NULL
);Common SQL Anti-Patterns
1. SELECT *
-- ❌ Bad — fetches all columns, breaks if schema changes
SELECT * FROM users;
-- ✅ Good — explicit column list
SELECT id, name, email, created_at
FROM users;2. Missing Indexes on WHERE/JOIN Columns
-- If you filter or join on a column frequently, index it
CREATE INDEX idx_orders_user_id ON orders(user_id);
CREATE INDEX idx_orders_created_at ON orders(created_at);3. N+1 Queries
-- ❌ Bad — one query per user (N+1 problem)
-- Application code: for each user, SELECT orders WHERE user_id = ?
-- ✅ Good — single JOIN query
SELECT u.name, o.id, o.total
FROM users u
INNER JOIN orders o ON u.id = o.user_id
WHERE u.active = 1;4. Implicit Type Conversions
-- ❌ Bad — string comparison on numeric column (prevents index use)
WHERE user_id = '123'
-- ✅ Good — correct type
WHERE user_id = 1235. Not Using Parameterized Queries
-- ❌ SQL Injection vulnerability!
query = f"SELECT * FROM users WHERE name = '{user_input}'"
-- ✅ Parameterized query
query = "SELECT * FROM users WHERE name = $1"
params = [user_input]SQL Style Guides in the Wild
Several well-known SQL style guides exist:
- Simon Holywell's SQL Style Guide: The most widely referenced. Uses uppercase keywords, 4-space indentation, and river formatting.
- GitLab SQL Style Guide: Uses CTEs extensively, lowercase keywords, and trailing commas.
- Mozilla SQL Style Guide: Similar to Holywell's with organization-specific additions.
The specific style matters less than consistency. Pick a style, enforce it with a formatter, and never argue about it in code reviews again.
Format SQL Instantly
Our SQL Formatter takes any SQL query — messy, minified, or partially formatted — and outputs clean, consistently indented SQL. It supports all major SQL dialects (PostgreSQL, MySQL, SQLite, T-SQL, PL/SQL) and lets you configure indentation style.
Everything runs 100% in your browser. Your queries never leave your device, making it safe to format production SQL containing real table names, conditions, and business logic.
Need a quick SQL reference? Check our SQL Cheat Sheet. Working with JSON data from your database? Try the JSON Formatter.
Summary
Clean SQL isn't just aesthetics — it prevents bugs, speeds up code reviews, and makes debugging faster. Follow the rules: one clause per line, indent sub-items, uppercase keywords, use meaningful aliases, and prefer CTEs over nested subqueries.
For instant formatting, DevToolbox's SQL Formatter is always available — free, private, and ready to beautify any query.