SQL Formatting Best Practices for Readable Queries

Well-formatted SQL is easier to read, debug, and maintain. Whether you’re writing complex joins or simple SELECT statements, consistent formatting makes your queries professional and understandable.

Why Format SQL?

1. Readability

Formatted SQL is easier for you and your team to understand.

2. Debugging

Proper indentation makes errors more visible.

3. Code Reviews

Reviewers can focus on logic, not formatting issues.

4. Maintenance

Future developers (including you) will thank you.

Basic Formatting Rules

Keywords on New Lines

-- Good
SELECT
    id,
    name,
    email
FROM users
WHERE active = true
ORDER BY created_at DESC;

-- Avoid
select id, name, email from users where active = true order by created_at desc;

Consistent Indentation

SELECT
    u.id,
    u.name,
    o.order_id
FROM users u
INNER JOIN orders o
    ON u.id = o.user_id
WHERE u.active = true
    AND o.status = 'completed';

Align Columns

SELECT
    id,
    first_name,
    last_name,
    email_address,
    created_at
FROM users;

Using Our SQL Formatter

Our SQL Formatter makes formatting easy:

  1. Paste your SQL query into the input
  2. Click “Format” to get readable output
  3. Click “Minify” for production-ready one-liners
  4. Copy the result to your code

Common SQL Patterns

SELECT with JOINs

SELECT
    u.id,
    u.name,
    COUNT(o.id) AS order_count
FROM users u
LEFT JOIN orders o
    ON u.id = o.user_id
WHERE u.created_at >= '2024-01-01'
GROUP BY
    u.id,
    u.name
HAVING COUNT(o.id) > 0
ORDER BY order_count DESC;

INSERT Statement

INSERT INTO users (
    first_name,
    last_name,
    email,
    created_at
) VALUES (
    'John',
    'Doe',
    '[email protected]',
    NOW()
);

UPDATE Statement

UPDATE users
SET
    first_name = 'Jane',
    updated_at = NOW()
WHERE id = 123;

Subqueries

SELECT
    id,
    name
FROM users
WHERE id IN (
    SELECT user_id
    FROM orders
    WHERE total_amount > 1000
);

CASE Statements

SELECT
    id,
    name,
    CASE
        WHEN score >= 90 THEN 'A'
        WHEN score >= 80 THEN 'B'
        WHEN score >= 70 THEN 'C'
        ELSE 'F'
    END AS grade
FROM students;

Formatting Conventions

Keyword Case

Choose a style and stick with it:

-- UPPERCASE (most common)
SELECT * FROM users WHERE active = true;

-- lowercase
select * from users where active = true;

-- PascalCase
Select * From Users Where Active = true;

Comma Placement

-- Leading commas (some prefer this)
SELECT
    id
    , name
    , email
FROM users;

-- Trailing commas (traditional)
SELECT
    id,
    name,
    email
FROM users;

String Quotes

-- Single quotes (standard SQL)
WHERE name = 'John'

-- Double quotes (some databases)
WHERE name = "John"

Code Examples

JavaScript

// Simple SQL formatter
function formatSQL(sql) {
    const keywords = ['SELECT', 'FROM', 'WHERE', 'JOIN', 'ON', 'AND', 'OR', 'ORDER BY', 'GROUP BY', 'HAVING', 'LIMIT'];

    let formatted = sql.toUpperCase();
    keywords.forEach(kw => {
        const regex = new RegExp(`\\b${kw}\\b`, 'gi');
        formatted = formatted.replace(regex, '\n' + kw);
    });

    return formatted.trim();
}

Python

import sqlparse

# Format SQL
formatted = sqlparse.format(
    'SELECT * FROM users WHERE active=true',
    reindent=True,
    keyword_case='upper'
)
print(formatted)

Common Mistakes

1. SELECT *

Avoid in production - specify columns:

-- Avoid
SELECT * FROM users;

-- Better
SELECT id, name, email FROM users;

2. No Aliases for Joins

-- Unclear
SELECT users.id, orders.id
FROM users
JOIN orders ON users.id = orders.user_id;

-- Clear
SELECT
    u.id AS user_id,
    o.id AS order_id
FROM users u
JOIN orders o ON u.id = o.user_id;

3. Complex WHERE Without Parentheses

-- Confusing
WHERE a = 1 AND b = 2 OR c = 3

-- Clear
WHERE (a = 1 AND b = 2) OR c = 3

Performance Tips

Readability vs Performance

Well-formatted SQL doesn’t affect performance - the database parses it the same way.

Use EXPLAIN

Format your query, then use EXPLAIN to understand performance:

EXPLAIN SELECT
    id, name
FROM users
WHERE email LIKE '%@gmail.com';

Conclusion

Good SQL formatting is a sign of professional development. It makes your queries easier to read, debug, and maintain.

Use our SQL Formatter to quickly format and beautify your SQL queries. Your team (and future you) will appreciate the clean, readable code!