CASE Statement: Searched & Simple
Add IF / ELSE logic to queries with CASE: syntax, the searched and simple forms, and the three rules — order matters, ELSE is optional, same data type.
SQL has no IF inside a SELECT — but it has something better: the CASE expression. With it you can label players "Elite", "Solid" or "Developing", convert "India" to "IN", or turn a condition into a 1/0 flag — all row by row.
Definition
A CASE expression evaluates a list of conditions in order and returns the result of the first condition that is TRUE. If no condition is TRUE, it returns the
ELSEvalue — or NULL if there is noELSE.
- Expression — CASE produces one value per row, just like a single-row function, so it can be used in
SELECT,WHERE,ORDER BY,GROUP BYand inside aggregates. - In order — conditions are checked from top to bottom.
- First TRUE wins — once a condition matches, the rest are skipped for that row.
Syntax
sql
CASE
WHEN condition1 THEN result1
WHEN condition2 THEN result2
WHEN condition3 THEN result3
ELSE default_result
END AS new_column| Part | Meaning |
|---|---|
CASE | start of the logic |
WHEN condition THEN result | if the condition is TRUE, return this result |
more WHENs | more conditions, checked in order |
ELSE default_result | result if nothing matched (optional — default is NULL) |
END | end of the logic (required) |
AS new_column | name for the new column |
Example — derive a new category column
sql
SELECT
name,
runs,
CASE
WHEN runs >= 700 THEN 'Elite'
WHEN runs >= 400 THEN 'Solid'
ELSE 'Developing'
END AS category
FROM players;| name | runs | category |
|---|---|---|
| Virat Kohli | 750 | Elite |
| Rohit Sharma | 500 | Solid |
| Jos Buttler | 850 | Elite |
| MS Dhoni | 350 | Developing |
| David Warner | 700 | Elite |
| Jasprit Bumrah | 20 | Developing |
How it works: step 1 — check conditions top to bottom; step 2 — the first TRUE one wins; step 3 — no match → ELSE. Kohli's 750 matches >= 700 and stops there, even though >= 400 is also true. CASE categorises values into groups as a brand-new column.
The two forms of CASE
| Searched CASE | Simple CASE (quick form) |
|---|---|
CASE WHEN country = 'India' THEN 'IN' WHEN country = 'England' THEN 'ENG' ELSE 'N/A' END | CASE country WHEN 'India' THEN 'IN' WHEN 'England' THEN 'ENG' ELSE 'N/A' END |
Any condition: >, <, AND, OR, IN, LIKE, IS NULL … | Only compares one expression with = |
| Most flexible — use for ranges | Shorter — use for one-to-one mapping |
Both give the same result for equality checks — pick the one that is easier to read.
Example — simple CASE to standardise values
sql
SELECT
name,
CASE country
WHEN 'India' THEN 'IN'
WHEN 'England' THEN 'ENG'
WHEN 'Australia' THEN 'AUS'
ELSE 'N/A'
END AS country_code
FROM players;| name | country_code |
|---|---|
| Virat Kohli | IN |
| Rohit Sharma | IN |
| Jos Buttler | ENG |
| MS Dhoni | IN |
| David Warner | AUS |
| Jasprit Bumrah | IN |
Read the country, map it to a short code — messy values become one clean format.
CASE — 3 rules to remember
1. Order matters. SQL checks WHEN from top to bottom and the first TRUE one wins. Put the strictest condition (700) before the looser one (400). If you swap them:
sql
SELECT
name,
runs,
CASE
WHEN runs >= 400 THEN 'Solid'
WHEN runs >= 700 THEN 'Elite'
ELSE 'Developing'
END AS category
FROM players;| name | runs | category |
|---|---|---|
| Virat Kohli | 750 | Solid |
| Rohit Sharma | 500 | Solid |
| Jos Buttler | 850 | Solid |
| MS Dhoni | 350 | Developing |
| David Warner | 700 | Solid |
| Jasprit Bumrah | 20 | Developing |
Nobody is 'Elite' any more — 750 already matched >= 400, so the >= 700 line is never reached.
2. ELSE is optional. No match and no ELSE? You get NULL:
sql
SELECT
name,
runs,
CASE WHEN runs >= 700 THEN 'Elite' END AS tag
FROM players;| name | runs | tag |
|---|---|---|
| Virat Kohli | 750 | Elite |
| Rohit Sharma | 500 | NULL |
| Jos Buttler | 850 | Elite |
| MS Dhoni | 350 | NULL |
| David Warner | 700 | Elite |
| Jasprit Bumrah | 20 | NULL |
Add ELSE 'N/A' (or another default) when NULL isn't what you want.
3. Same data type. All THEN/ELSE results must be compatible — don't mix text with numbers. CASE WHEN runs >= 700 THEN 'Elite' ELSE 0 END fails in SQL Server ("Conversion failed when converting the varchar value 'Elite' to data type int"), because SQL Server tries to make everything an INT.
Common mistakes
- ❌ Forgetting
END→ syntax error. - ❌ Loose condition first (
>= 400before>= 700) → wrong categories. - ❌
CASE team WHEN NULL THEN 'Unsold' END— the simple form uses=, andteam = NULLis never TRUE. ✅CASE WHEN team IS NULL THEN 'Unsold' ELSE team END. - ❌ Mixing types in THEN/ELSE → conversion errors.
- ❌ Overlapping ranges with gaps:
WHEN runs > 700 … WHEN runs < 700— exactly 700 falls through to ELSE.
Interview tip
Q: What is the difference between a simple and a searched CASE?
A simple CASE compares one expression against a list of values using equality (CASE country WHEN 'India' THEN …). A searched CASE evaluates independent Boolean conditions (CASE WHEN runs >= 700 AND country = 'India' THEN …), so it supports ranges, IS NULL, LIKE and multiple columns. Every simple CASE can be rewritten as a searched CASE, but not the other way round.
Key points to remember
- CASE returns the result of the first WHEN condition that is TRUE; otherwise the ELSE value.
- Conditions are checked top to bottom — put the strictest condition first.
- ELSE is optional; with no match and no ELSE, CASE returns NULL.
- All THEN/ELSE results must be the same (compatible) data type.
- Searched CASE (CASE WHEN cond …) supports any condition; simple CASE (CASE col WHEN value …) only compares with =.
- Simple CASE cannot match NULL — use CASE WHEN col IS NULL instead.
- Every CASE must finish with END.
Try it yourself
Categorise players by runs
Runs on a fresh IPL practice database in your browser · edit the query and press Ctrl + Enter
Simple CASE: country codes
Runs on a fresh IPL practice database in your browser · edit the query and press Ctrl + Enter
Order matters & missing ELSE
Runs on a fresh IPL practice database in your browser · edit the query and press Ctrl + Enter
Practice: add a 'Legend' level
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.
