Unit 5, lesson 2 · Several WHEN branches
Several branches: the first true WHEN wins
Price bands for the catalog
A CASE can have as many WHEN branches as you need. PostgreSQL checks them from top to bottom and stops at the first one that is true, so a row never gets two labels. That makes the order of the branches part of the logic: put the narrowest test first.
SELECT id, status,
CASE WHEN status = 'delivered' THEN 'done'
WHEN status = 'shipped' THEN 'on the way'
ELSE 'closed'
END AS stage
FROM orders
LIMIT 8;Your task
The catalog groups products into price bands: under 50 is 'budget', 50 up to 199 is 'mid', and 200 or more is 'premium'. Return each product's name, price and price_band.
Tables you will use: products
Try it in your browserFree, no sign-up. Your query is checked as you go.