Input SQL
Formatted SQL
About SQL Formatting
Consistently formatted SQL is easier to read, review, and debug. While SQL engines ignore whitespace, well-structured queries help teams collaborate effectively and catch logic errors early.
Why Format SQL?
- Readability - quickly understand query structure and intent
- Maintainability - easier to modify and extend queries over time
- Code review - consistent formatting makes diffs cleaner and reviews faster
- Debugging - structured layout helps isolate problems in complex queries
Formatting Conventions
- Uppercase keywords -
SELECT,FROM,WHEREfor visual distinction - Lowercase identifiers - table and column names in lowercase or snake_case
- Indent subqueries - one level deeper than the containing clause
- One column per line - in SELECT lists with many columns
Common SQL Clauses
| Clause | Purpose | Example |
|---|---|---|
SELECT | Columns to return | SELECT name, email |
FROM | Source table(s) | FROM users |
WHERE | Row filter conditions | WHERE active = true |
JOIN | Combine related tables | JOIN orders ON users.id = orders.user_id |
GROUP BY | Group rows for aggregation | GROUP BY department |
HAVING | Filter groups | HAVING COUNT(*) > 5 |
ORDER BY | Sort results | ORDER BY created_at DESC |
LIMIT | Restrict row count | LIMIT 100 |