The rider-experience team wants a quick service-level view of how long customers waited for a driver to arrive.
From ride_requests, return ride_id, rider_name, wait_minutes and a new column wait_band:
- Not picked up when
wait_minutesis NULL (the ride was cancelled before a driver arrived) - Quick for 3 minutes or less
- Normal for 4 to 8 minutes
- Slow for anything above 8 minutes
Order the result by ride_id.
Sample data
Table: ride_requests
12 row(s) · showing first 8
| ride_idINT | rider_nameTEXT | cityTEXT | wait_minutesINT | fareNUMERIC(8,2) |
|---|---|---|---|---|
| 1 | Anita | Bengaluru | 2 | 180.00 |
| 2 | Bharat | Hyderabad | 5 | 240.50 |
| 3 | Chaya | Chennai | 9 | 310.00 |
| 4 | Dev | Pune | 3 | 150.00 |
| 5 | Eshwar | Delhi | NULL | 0.00 |
| 6 | Fatima | Mumbai | 8 | 420.75 |
| 7 | Gopal | Kolkata | 12 | 275.00 |
| 8 | Hema | Bengaluru | 1 | 95.00 |
ride_requests
- ride_idINT
- rider_nameTEXT
- cityTEXT
- wait_minutesINT
- fareNUMERIC(8,2)
- Step 1: `CASE` evaluates branches top to bottom and stops at the first match, so ordering matters.
- Step 2: Test `IS NULL` first. A NULL compared with `<= 3` is unknown, so it would silently fall through to `ELSE 'Slow'`.
- Step 3: Because earlier branches already caught the smaller values, `<= 8` only needs one boundary to describe 4 to 8.
SELECT ride_id,
rider_name,
wait_minutes,
CASE
WHEN wait_minutes IS NULL THEN 'Not picked up'
WHEN wait_minutes <= 3 THEN 'Quick'
WHEN wait_minutes <= 8 THEN 'Normal'
ELSE 'Slow'
END AS wait_band
FROM ride_requests
ORDER BY ride_id;More CASE Statements questions
Practising alone only goes so far — our live cohorts pair SQL depth with dashboards, agentic analytics projects and mock interviews.
Explore the program