Writing Clean, Readable & Maintainable SQL Style Guides
Adopt naming, formatting, and CTE habits that keep analytical SQL reviewable.
In plain words
Everyone writes SQL the same tidy way, so it's easy for anyone to read.
Think of it like…
A newspaper's style guide: every journalist writes dates, numbers and names the same way, so readers never stumble over the format and can focus on the story.
Where you’ve seen it
A data team adopts SQLFluff in CI with rules for lowercase keywords, leading commas, explicit aliases and no implicit joins. Pull requests that break the rules fail with a message pointing at the line. After a month, reviewers say they spend their time on logic instead of commenting on formatting.
Words you’ll hear
- Formatter
- A tool that rewrites SQL into a standard layout automatically.
- Linter
- A tool that flags rule violations such as SELECT * or implicit joins (SQLFluff).
- Naming convention
- Agreed patterns: customer_id for keys, created_at for timestamps, is_active for booleans.
- Explicit join
- JOIN … ON with the condition next to the table, rather than FROM a, b WHERE ….
Visual diagram
Theory & example queries
Layout and naming
Put each selected column on its own line, indent clauses consistently, and align JOIN … ON blocks so each table sits next to its join condition. Choose upper- or lowercase keywords and leading or trailing commas, and stick with the choice everywhere; the exact choice matters far less than consistency. Qualify every column with its table alias in queries with joins, and use aliases that mean something (orders o, customers c) rather than a, b, c.
Names should say what they hold: snake_case everywhere, tables named consistently (all plural or all singular), keys as <table>_id, timestamps with _at (created_at), dates with _date, booleans with is_ or has_, and amounts with their unit or currency where it's ambiguous (amount_inr, duration_ms). Avoid reserved words and abbreviations only the author understands.
| id | name | city | |
|---|---|---|---|
| 1 | Maya | maya@example.com | Pune |
| 2 | Ravi | ravi@example.com | Delhi |
| 3 | Noor | noor@example.com | Mumbai |
| id | customer_id | status | amount | coupon_code |
|---|---|---|---|---|
| 101 | 1 | paid | 249 | NULL |
| 102 | 2 | paid | 120 | WELCOME10 |
| 103 | 1 | shipped | 89.5 | NULL |
SELECT o.customer_id, count(*) AS order_count, sum(o.amount_inr) AS revenue_inrFROM orders AS o INNER JOIN customers AS c ON c.customer_id = o.customer_idWHERE o.created_at >= '2026-01-01' AND c.is_test_account = FALSEGROUP BY o.customer_idPostgreSQL is the lesson baseline unless noted otherwise.
Calculate the selected expressions and return the requested output columns.
Formatted query result2 sample rows
| customer_id | order_count | revenue_inr |
|---|---|---|
| 17 | 6 | 48250 |
| 29 | 4 | 31700 |
Note: Formatting does not change results; values assume two qualifying non-test customers in the sample.
(written one clause and one column per line in the editor).
Key takeaway: Consistent layout, qualified columns, names that describe their contents.
Structure, comments and enforcement
Prefer CTEs over nested subqueries for anything beyond one level, list columns explicitly instead of SELECT * so schema changes don't silently change results, and write explicit JOIN syntax, which keeps join conditions out of WHERE and makes a missing condition obvious. Group by column names rather than ordinal positions (GROUP BY 1, 2) in production code, since adding a column shifts the numbers; many teams allow ordinals in quick analysis only.
Comments should explain decisions the SQL can't: why test accounts are excluded, why the date boundary is half-open, which ticket defined the metric. Write the rules down on one page with before and after examples, then enforce them with a formatter and linter in the editor and in CI. SQLFluff supports most dialects and dbt templating; tools enforce style so reviewers don't have to.
A comment above the filter saying "exclude internal test accounts (see ANALYTICS-412); they inflate November signups by ~3%" tells the next reader why c.is_test_account = false is there and that it's deliberate.
How it works, step by step
Follow one practical SQL workflow from start to finish. Select any step or play through the flow.
Agree the rules
Choose keyword case, comma style, indentation, alias and naming rules, starting from an existing guide.
lowercase keywords · leading commas · snake_case
When to use it & trade-offs
What it is
A SQL style guide is a team's agreed rules for formatting and naming, so every query looks like it was written by the same person. Typical rules: one selected column per line, consistent keyword case, explicit JOIN … ON instead of comma joins, meaningful table aliases, CTEs instead of deep nesting, snake_case names with suffixes like _id, _at and is_, and no SELECT * in production code. A formatter and linter such as SQLFluff enforce the rules automatically.
Why it matters: Analytical SQL is read far more often than it's written: in code review, when a metric looks wrong, and when someone inherits a report. Consistent style makes those reads fast and makes mistakes such as a wrong join condition or a missing filter easier to see. Take-home assignments are judged on readability too.
When to reach for it
- Shared analytics repositories and dbt projects
- Take-home assignments and interview whiteboards
- Any SQL that other people will maintain
When to avoid or be careful
- Comma joins with conditions in WHERE
- SELECT * in production queries
- Aliases like a, b, t1 in long queries
Ways to keep SQL consistent
Strengths: Quick to agree.
Watch out: Depends on people remembering; drifts over time.
Strengths: Fixes formatting automatically; no arguments.
Watch out: Can't judge naming or structure.
Strengths: Catches SELECT *, implicit joins, inconsistent aliases.
Watch out: Needs configuring per dialect; some rules need tuning.
Interview signals & numbers to know
When this topic comes up
- “Make this query readable”
- Take-home graded on code quality
- “How does your team review SQL?”
- dbt projects
- Naming conventions
Benchmarks & rules of thumb
- 1
- column per line in SELECT lists
- _id · _at · is_
- common name suffixes and prefixes
- 1 level
- of nesting before switching to CTEs
Common mistakes & follow-ups
Common interview mistakes
- Mixing comma joins and explicit joins in one query, so a join condition hides in a long WHERE clause.
- Using GROUP BY 1, 2, 3 in a production model, then adding a column and grouping by the wrong one.
- Naming a column date or status_flag2, which tells the reader nothing about its contents.
Likely interviewer follow-ups
- What rules would you put in a SQL style guide?
- Why prefer explicit JOIN syntax to comma joins?
- How would you enforce a style guide across a team?
Knowledge check
Put it into practice
Practice related interview Q&A
Apply Writing Clean, Readable & Maintainable SQL Style Guides with hands-on SQL practice and interview Q&A.