Searched CASE
- SQL course
- Intermediate
- Lesson 66/115
- 4 min
Write the searched form of CASE.
Searched CASE
Searched CASE takes an independent condition in each WHEN. It is the most flexible CASE form.
SELECT name,
CASE WHEN price < 50 THEN 'ucuz'
WHEN price < 200 THEN 'orta'
ELSE 'pahalı' END AS tier
FROM products;This query produces three price tiers.
name, CASE WHEN price < 50 THEN 'ucuz' WHEN price < 200 … is selected from products.
Searched CASE — details
In searched CASE each WHEN holds a separate boolean condition. The first true condition's THEN runs.
Conditions are tried from top to bottom, so WHEN price < 50 must come before WHEN price < 100, otherwise the first one never runs.
Practice Searched CASE
Write a three-tier CASE WHEN price < 50 THEN 'ucuz' WHEN price < 200 THEN 'orta' ELSE 'pahalı' END.
Starter code
-- three tiers
SELECT name,
CASESample solution
SELECT name,
CASE WHEN price < 50 THEN 'ucuz'
WHEN price < 200 THEN 'orta'
ELSE 'pahalı' END AS tier
FROM products;Expected output
name tier Kalem ucuz Telefon orta Laptop pahalı
Questions and answers
Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.
1 Add WHEN
CASE ____ price < 50 THEN 'ucuz'
CASE WHEN price < 50 THEN 'ucuz'
Explanation: In CASE each condition starts with WHEN and gives its result with THEN.
2 What does WHEN take in searched CASE?
- A condition Correct answer
- A value
- A column
- A table
Explanation: In searched CASE WHEN takes a condition.
3 Searched CASE: match the terms
| Term | Meaning |
|---|---|
searched |
conditional |
WHEN |
condition |
THEN |
result |
ELSE |
default |