An interviewer asks: "How many of our orders are small, medium and large?" This is a histogram. Put each order's total into a bucket with CASE, then GROUP BY the bucket and count. CASE gives the label of the first WHEN that is true, so each WHEN only needs the top edge of its bucket.
Here is the same idea on products, sorted into two price bands:
SELECT CASE
WHEN price < 5 THEN 'cheap'
ELSE 'not cheap'
END AS band, COUNT(*) AS products
FROM products
GROUP BY band
The starting code already has totals, a named query with one row per order and its total (the sum of quantity * unit_price over the order's items). Your query reads from it and must print the buckets from small to big:
size | orders under 20 | 1 20 to 49 | 4 50 and up | 2 (3 rows)
Sorting by the label won't do it: text sorts character by character, so '20 to 49' comes before 'under 20'. Sort the buckets by their smallest total instead.
Your task: between the closing bracket and FROM totals, select a CASE named size with three buckets, 'under 20', '20 to 49' and '50 and up', and COUNT(*) named orders. Then group by size and order the buckets by their smallest total.
Assignment
Follow the Coding Tutorial to build the CASE one WHEN at a time, then group and sort the buckets.
Hint
Write the WHENs from small to big. An order under 20 stops at the first WHEN, so total < 50 only catches 20 to 49, and ELSE catches the rest.
Free plan: 30 tutor messages. PRO is unlimited.
