CASE Use Cases: Categorise, Map, Conditional Aggregation
Apply CASE in real reports: multi-condition categories, handling NULLs, custom sorting, and conditional counting/summing inside aggregates.
CASE is one of the most-used tools in real analytics work. This topic collects its classic use cases on IPL data: categorising, mapping / standardising, handling NULLs, custom sorting, and — the big one — conditional aggregation.
Definition
Conditional aggregation means putting a CASE expression inside an aggregate function (
SUM,COUNT,AVG) so that only the rows meeting a condition contribute to the result. It lets one query return several filtered totals side by side.
| Use case | Pattern |
|---|---|
| Categorise values into groups | CASE WHEN runs >= 700 THEN 'Elite' … END |
| Map / standardise values | CASE country WHEN 'India' THEN 'IN' … END |
| Handle NULLs with a custom label | CASE WHEN runs IS NULL THEN 'Did not bat' … END |
| Custom sort order | ORDER BY CASE WHEN team = 'MI' THEN 0 ELSE 1 END |
| Conditional counting | SUM(CASE WHEN cond THEN 1 ELSE 0 END) |
| Conditional summing (pivot) | SUM(CASE WHEN cond THEN runs ELSE 0 END) |
Syntax — CASE inside an aggregate
sql
SELECT
group_column,
SUM(CASE WHEN condition THEN 1 ELSE 0 END) AS count_if,
SUM(CASE WHEN condition THEN value_col ELSE 0 END) AS sum_if
FROM table_name
GROUP BY group_column;Example — conditional counting
sql
SELECT
country,
COUNT(*) AS players,
SUM(CASE WHEN runs >= 500 THEN 1 ELSE 0 END) AS big_scorers
FROM players
GROUP BY country;| country | players | big_scorers |
|---|---|---|
| Australia | 1 | 1 |
| England | 1 | 1 |
| India | 4 | 2 |
How it works: step 1 — CASE marks each row 1 (500+ runs) or 0; step 2 — SUM adds up the 1s per country. India has 4 players, but only Kohli (750) and Rohit (500) are 500+ scorers. (Aggregates and GROUP BY are covered fully in the next chapter.)
Tip:
COUNT(CASE WHEN runs >= 500 THEN 1 END)gives the same answer — with no ELSE the non-matching rows become NULL, andCOUNT(column)ignores NULLs.
Example — conditional summing (a mini pivot)
sql
SELECT
player,
SUM(CASE WHEN MONTH(match_date) = 3 THEN runs ELSE 0 END) AS march_runs,
SUM(CASE WHEN MONTH(match_date) = 4 THEN runs ELSE 0 END) AS april_runs
FROM innings
GROUP BY player
ORDER BY player;| player | march_runs | april_runs |
|---|---|---|
| Jos Buttler | 73 | 166 |
| MS Dhoni | 26 | 45 |
| Rohit Sharma | 121 | 0 |
| Virat Kohli | 141 | 73 |
Rows become columns: each CASE lets only one month's runs into its sum. Dhoni's NULL innings adds nothing — SUM ignores NULL.
Example — multi-condition categories with NULL handling
sql
SELECT
player,
match_date,
runs,
CASE
WHEN runs IS NULL THEN 'Did not bat'
WHEN runs >= 100 THEN 'Century'
WHEN runs >= 50 THEN 'Fifty'
WHEN runs = 0 THEN 'Duck'
ELSE 'Start'
END AS milestone
FROM innings
ORDER BY id;| player | match_date | runs | milestone |
|---|---|---|---|
| Virat Kohli | 2025-03-22 | 82 | Fifty |
| Virat Kohli | 2025-03-29 | 59 | Fifty |
| Virat Kohli | 2025-04-05 | 73 | Fifty |
| Rohit Sharma | 2025-03-23 | 45 | Start |
| Rohit Sharma | 2025-03-30 | 76 | Fifty |
| Rohit Sharma | 2025-04-06 | 0 | Duck |
| MS Dhoni | 2025-03-24 | 26 | Start |
| MS Dhoni | 2025-03-31 | NULL | Did not bat |
| MS Dhoni | 2025-04-07 | 45 | Start |
| Jos Buttler | 2025-03-25 | 73 | Fifty |
| Jos Buttler | 2025-04-01 | 59 | Fifty |
| Jos Buttler | 2025-04-08 | 107 | Century |
The IS NULL check comes first so NULL runs get a proper label instead of falling to ELSE.
Example — conditions on several columns
sql
SELECT
name,
country,
CASE
WHEN country = 'India' AND runs >= 500 THEN 'Indian star'
WHEN country <> 'India' THEN 'Overseas'
ELSE 'Indian squad'
END AS bucket
FROM players;| name | country | bucket |
|---|---|---|
| Virat Kohli | India | Indian star |
| Rohit Sharma | India | Indian star |
| Jos Buttler | England | Overseas |
| MS Dhoni | India | Indian squad |
| David Warner | Australia | Overseas |
| Jasprit Bumrah | India | Indian squad |
Example — custom sort order
sql
SELECT name, runs
FROM players
ORDER BY
CASE WHEN team = 'MI' THEN 0 ELSE 1 END,
runs DESC;| name | runs |
|---|---|
| Rohit Sharma | 500 |
| Jasprit Bumrah | 20 |
| Jos Buttler | 850 |
| Virat Kohli | 750 |
| David Warner | 700 |
| MS Dhoni | 350 |
MI players get sort key 0 and come first; everyone else gets 1. Inside each group, runs decide.
Common mistakes
- ❌
SUM(CASE WHEN runs >= 500 THEN 1 END)returns NULL (not 0) for a group with no matches. ✅ AddELSE 0. - ❌
COUNT(CASE WHEN runs >= 500 THEN 1 ELSE 0 END)— COUNT counts the 0s too! ✅ UseSUMwithELSE 0, orCOUNTwithout ELSE. - ❌ Repeating a long CASE in
GROUP BYwith a typo — the two must match exactly. ✅ Copy it exactly (or use a subquery/CTE, coming later). - ❌ Putting the NULL check last — NULL rows fall into ELSE first.
Interview tip
Q: Count how many players in each country scored 500+ runs, in a single query.
SELECT country, SUM(CASE WHEN runs >= 500 THEN 1 ELSE 0 END) FROM players GROUP BY country; — this conditional aggregation pattern is asked constantly, because it replaces several separate filtered queries with one pass over the table.
Key points to remember
- Conditional aggregation = CASE inside an aggregate, e.g. SUM(CASE WHEN cond THEN 1 ELSE 0 END).
- SUM(CASE … THEN 1 ELSE 0 END) counts matching rows per group.
- SUM(CASE … THEN runs ELSE 0 END) sums only matching rows — one column per condition gives a pivot.
- COUNT(CASE WHEN cond THEN 1 END) also counts matches, because COUNT ignores the NULLs from the missing ELSE.
- Put the IS NULL check first so NULL values get their own label.
- CASE in ORDER BY creates a custom sort order.
- Searched CASE can combine several columns with AND / OR.
Try it yourself
Big scorers per country (conditional counting)
Runs on a fresh IPL practice database in your browser · edit the query and press Ctrl + Enter
March vs April runs (conditional sum pivot)
Runs on a fresh IPL practice database in your browser · edit the query and press Ctrl + Enter
Milestones with NULL handling
Runs on a fresh IPL practice database in your browser · edit the query and press Ctrl + Enter
Practice: fifties per player
Runs on a fresh IPL practice database in your browser · edit the query and press Ctrl + Enter
Revision Flashcards
Tap a card to flip it and see the answer.
