Overview
Label each order small, medium or large with CASE WHEN, and order the conditions so the first match is the right one.
- CASE WHEN ... THEN ... ELSE ... END
- The first matching WHEN wins
- Thresholds from biggest to smallest
Short Course
CASE WHEN ... THEN ... ELSE ... END picks a value row by row, like if / else.
Trap: the first matching WHEN wins, so a loose condition placed first hides
the ones below it. Test the strictest condition first.
SELECT name,
CASE WHEN stock = 0 THEN 'sold out'
WHEN stock < 5 THEN 'low' -- placed first, sold out would say 'low'
ELSE 'ok' END AS status
FROM products;
Task
Return each order's id and a label for its total: 'large' from 100,
'medium' from 30, otherwise 'small', ordered by id. In the database
below, an order of exactly 30 is medium.
Database
Sample data from the first test. The other tests use different rows, so write a query, not a list of answers.
| idINTEGER | totalREAL |
|---|---|
| 1 | 150.0 |
| 2 | 30.0 |
| 3 | 29.99 |
| 4 | 100.0 |
| 5 | 5.0 |
Test Suites
Pending
Next
Output