Brightlane's finance team is reviewing the value distribution within the fulfilled-order pipeline.
Write a query to return each delivered order's ID and a value_tier label:
'high value'for orders above$1,000.'standard'for all other delivered orders.
Assumptions:
- The
orderstable contains every order Brightlane has processed. - Only delivered orders (
status = 'delivered') should appear in the result. - An order priced exactly at
$1,000is'standard'(threshold is strictly greater-than).
Output:
- One row per delivered order, with columns
idandvalue_tier.
Schema · ecommerce 5 tables
Run previews · Check grades
Write a query, then run it to see results here.
Worked solution Try it yourself first
SELECT
id,
CASE
WHEN total_amount > 1000 THEN 'high value'
ELSE 'standard'
END AS value_tier
FROM
orders
WHERE
status = 'delivered' The shape
WHERE status = 'delivered' narrows the row set to fulfilled orders, and CASE then labels each surviving row by value. The two clauses do separate jobs: the filter decides who's in, the CASE decides what label they get.
Clause by clause
SELECT idcarries the order ID through so each labelled row is identifiable.CASE WHEN total_amount > 1000 THEN 'high value' ELSE 'standard' END AS value_tieris the derived column. TheWHENtests each (surviving) order'stotal_amountagainst the $1,000 threshold. The threshold is strictly greater-than, so an order at exactly$1,000lands in theELSEbranch and gets'standard'— matching the prompt's assumption directly.FROM ordersis the source set: every order, before any filtering.WHERE status = 'delivered'runs before theSELECT. Cancelled, pending, shipped, and refunded orders are gone before theCASEever sees them. The labels in the result describe only delivered orders.
Why this and not put the status check inside the CASE
A CASE branch could carry the status test inside it — something like WHEN status = 'delivered' AND total_amount > 1000 THEN 'high value'. That structure changes the result: non-delivered orders would still appear in the output, just with 'standard' (or whatever the ELSE branch returns). The finance team asked for delivered orders only, so the right tool is WHERE. It removes rows; CASE only relabels them.
The division of labour is the rule: WHERE decides which rows belong in the result, CASE decides what each one's derived columns look like. Confusing the two leads to result sets with the wrong rows or the wrong labels on the right rows.
You practiced combining WHERE (to scope the row set) with CASE (to derive a per-row label). The two compose cleanly: WHERE removes the rows you don't want; CASE produces a label for the rows you keep.