CASE Statements
Lesson opener
In one line: Conditional logic in SQL for categories and flags.
Why it matters: Raw revenue numbers are hard to scan in a deck - bands turn continuous spend into tiers everyone understands.
Before this lesson: Joins
Before you start
| Resource | Link |
|---|---|
| Previous | Joins |
| Next | Date functions |
What you will learn
Conditional logic in SQL for categories and flags.
Simple CASE
[Revenue bands A/B/C]
WHY: Raw revenue numbers are hard to scan in a deck — bands turn continuous spend into tiers everyone understands.
WHEN: You're labeling customers A/B/C for a campaign based on revenue thresholds.
SELECT customer_id, total_revenue,
CASE
WHEN total_revenue >= 2000 THEN 'A'
WHEN total_revenue >= 1000 THEN 'B'
ELSE 'C'
END AS revenue_band
FROM customers;Breaking it down:
CASE WHEN ... THEN ...→ if revenue ≥ 2000, label 'A'; else check next ruleWHEN total_revenue >= 1000 THEN 'B'→ mid tierELSE 'C'→ everyone below 1000END AS revenue_band→ name the new calculated column
Use this when: You need if/else buckets in the SELECT list without changing the table.
CASE in aggregation
[Count churned vs active in one query]
WHY: Running two separate COUNT queries for churned and active doubles your work and can drift if filters differ.
WHEN: Leadership wants one row: how many churned vs how many still active.
SELECT
SUM(CASE WHEN churned = 1 THEN 1 ELSE 0 END) AS churned_n,
SUM(CASE WHEN churned = 0 THEN 1 ELSE 0 END) AS active_n
FROM customers;Breaking it down:
CASE WHEN churned = 1 THEN 1 ELSE 0 END→ 1 for churned rows, 0 otherwiseSUM(...)→ add those 1s — conditional count without GROUP BY- Second SUM does the same for active (churned = 0)
Use this when: You want a pivot-style headcount split in a single aggregate query.
Searched CASE on joined data
[Mobile revenue by segment]
WHY: Filtering to mobile only loses other channels — a conditional SUM keeps one row per segment with just mobile dollars in a column.
WHEN: Product wants mobile revenue by customer segment alongside total segment reporting.
SELECT c.segment,
SUM(CASE WHEN s.channel = 'mobile' THEN s.revenue ELSE 0 END) AS mobile_rev
FROM customers c
JOIN sales s ON c.customer_id = s.customer_id
GROUP BY c.segment;Breaking it down:
JOIN→ attach orders to customer segmentCASE WHEN s.channel = 'mobile' THEN s.revenue ELSE 0→ count revenue only for mobile orders; 0 for web/email/etc.SUM(...)→ total mobile revenue per segmentGROUP BY c.segment→ one row per segment
Use this when: You're building a channel breakdown column in a segment summary — classic conditional aggregation.
Essential commands
| Command / action | What it does |
|---|---|
CASE WHEN ... THEN ... ELSE ... END | Conditional expression in SELECT. |
SUM(CASE WHEN ... THEN 1 ELSE 0 END) | Conditional count inside aggregation. |
Practice
Before each query, name the three SQL beats — Source → Pattern → Result (translated from Python's Input → Logic → Output — see sql2).
Do this now: Add a revenue_band column via CASE; build a pivot-style SUM(CASE …) by channel; save 05_case.sql.
Task 1 — revenue bands
-- Source: customers (customer_id, total_revenue)
-- Pattern: CASE in SELECT (threshold bands)
-- Result: one row per customer with a band label (A/B/C)
SELECT customer_id, total_revenue,
CASE
WHEN total_revenue >= 3000 THEN 'A'
WHEN total_revenue >= 1000 THEN 'B'
ELSE 'C'
END AS revenue_band
FROM customers;Task 2 — pivot-style SUM CASE
Fill the blanks before you write SQL:
Source: ___
Pattern: ___
Result: ___
-- Source: ___
-- Pattern: ___
-- Result: ___
-- Write your SUM(CASE …) query here.Checklist
- [ ] CASE in SELECT works
- [ ] CASE inside SUM works
- [ ] Bands make business sense