正在加载,请稍候…

How to Format SQL Queries for Better Readability

Learn SQL formatting best practices, indentation rules, and keyword casing. Use our online formatter to clean up messy queries instantly — supports MySQL, PostgreSQL, SQLite, and more.

What Is SQL Formatting?

SQL (Structured Query Language) formatting refers to consistently styling SQL code for readability and maintainability. Well-formatted SQL is easier to read, debug, and review in version control. Since SQL is whitespace-insensitive, the same query can be written in many ways - formatting is about human readability.

SQL Formatting Conventions

Keyword Case

SQL keywords are traditionally written in UPPERCASE, while identifiers (table names, column names) use lowercase or snake_case:

-- Recommended
SELECT user_id, first_name, last_name
FROM users
WHERE created_at > '2025-01-01'
ORDER BY last_name ASC;

-- Poor (harder to distinguish keywords from identifiers)
select user_id, first_name, last_name from users where created_at > '2025-01-01' order by last_name asc;

Indentation

Each clause starts on a new line, with continuation lines indented:

SELECT
    u.user_id,
    u.first_name,
    u.last_name,
    o.order_count
FROM users u
LEFT JOIN (
    SELECT user_id, COUNT(*) AS order_count
    FROM orders
    WHERE status = 'completed'
    GROUP BY user_id
) o ON u.user_id = o.user_id
WHERE u.created_at > '2025-01-01'
  AND u.active = TRUE
ORDER BY u.last_name ASC, u.first_name ASC
LIMIT 100;

Column Alignment

Align columns after SELECT for readability in complex queries:

SELECT
    p.product_id,
    p.product_name,
    p.price,
    c.category_name,
    COUNT(oi.order_item_id) AS order_count,
    SUM(oi.quantity)        AS total_quantity
FROM products p
JOIN categories c ON p.category_id = c.category_id
LEFT JOIN order_items oi ON p.product_id = oi.product_id
GROUP BY p.product_id, p.product_name, p.price, c.category_name
HAVING COUNT(oi.order_item_id) > 10
ORDER BY order_count DESC;

Writing Readable JOINs

Express JOIN conditions clearly, showing the relationship between tables:

-- Bad: implicit join (avoid)
SELECT * FROM orders, users
WHERE orders.user_id = users.user_id;

-- Good: explicit JOIN
SELECT
    o.order_id,
    u.email,
    o.total_amount
FROM orders o
INNER JOIN users u ON o.user_id = u.user_id
WHERE o.created_at >= CURRENT_DATE - INTERVAL '30 days';

SQL Query Optimization Tips

Use Indexes Effectively

-- Check if column is indexed before filtering:
EXPLAIN ANALYZE SELECT * FROM users WHERE email = 'test@example.com';

-- Create an index for frequently filtered columns:
CREATE INDEX idx_users_email ON users(email);

Avoid SELECT *

Always specify the columns you need. SELECT * transfers unnecessary data and breaks code when table schemas change:

-- Bad
SELECT * FROM products WHERE category_id = 5;

-- Good
SELECT product_id, product_name, price FROM products WHERE category_id = 5;

Use EXISTS Instead of COUNT for Existence Checks

-- Bad (counts all matching rows)
SELECT * FROM users WHERE (SELECT COUNT(*) FROM orders WHERE user_id = users.id) > 0;

-- Good (stops at first match)
SELECT * FROM users WHERE EXISTS (SELECT 1 FROM orders WHERE user_id = users.id);

SQL Dialects

SQL comes in several dialects with syntax variations:

Feature Standard SQL PostgreSQL MySQL SQL Server SQLite
String concat ` ` `
Current time CURRENT_TIMESTAMP NOW() NOW() GETDATE() CURRENT_TIMESTAMP
Limit rows FETCH FIRST n ROWS LIMIT n LIMIT n TOP n LIMIT n
Auto-increment GENERATED ALWAYS AS IDENTITY SERIAL or BIGSERIAL AUTO_INCREMENT IDENTITY AUTOINCREMENT

This tool formats SQL for standard SQL, PostgreSQL, MySQL, and SQL Server dialects.

Using This Tool

Paste any SQL query to instantly format it with proper indentation, keyword casing, and spacing. Supports all major SQL dialects and handles complex queries including subqueries, CTEs, window functions, and stored procedures.

-> Try the SQL Prettify Tool