Brightlane's finance team is building a tiered revenue report and needs each order labelled by value.
Write a query to return each order's ID, total_amount, and a tier label:
'premium'for orders withtotal_amount > $1,500.'high'for orders withtotal_amount > $500but not above$1,500.'standard'for all other orders.
Assumptions:
- An order at
total_amount = $1,500is'high'(it does not exceed$1,500). - An order at
total_amount = $500is'standard'(it does not exceed$500). - The three tiers are disjoint — every order falls into exactly one of the three labels.
Output:
- One row per order, with columns
id,total_amount, andtier.
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,
total_amount,
CASE
WHEN total_amount > 1500 THEN 'premium'
WHEN total_amount > 500 THEN 'high'
ELSE 'standard'
END AS tier
FROM
orders The shape
The higher threshold goes first. total_amount > 1500 is tested before total_amount > 500, because any order over $1,500 also satisfies the over-$500 test, and the first match wins. Branch order is the load-bearing decision.
Clause by clause
SELECT id, total_amountreturns the order ID and its raw total alongside the tier label, so the boundary cases are auditable.WHEN total_amount > 1500 THEN 'premium'is the most restrictive branch. Orders over $1,500 match here and stop. They never see the next branch.WHEN total_amount > 500 THEN 'high'runs only on rows that didn't match the first branch — sototal_amountis already known to be at-or-below $1,500. The condition therefore matches the band(500, 1500], even though only the lower bound is written. An order at exactly $1,500 lands here because1500 > 1500is false but1500 > 500is true — matching the prompt's assumption that $1,500 is'high'.ELSE 'standard'catches everything else: orders at or below $500. An order at exactly $500 fails500 > 500, falls past bothWHENbranches, and lands in'standard'— again matching the prompt.END AS tiercloses the expression.FROM ordersis the source set: every order Brightlane has processed.
Why this and not branches in the other order
Flipping the order to WHEN total_amount > 500 THEN 'high' first would break the result. A $2,000 order satisfies total_amount > 500, so PostgreSQL would return 'high' and never reach the premium branch. The $1,500+ orders would all be mislabelled as 'high', the report would show no premium tier at all, and the bug is silent — no error, no warning, just the wrong number on a finance dashboard.
The rule is: when conditions can overlap, the most restrictive has to come first. "Most restrictive" here means "smallest set of rows it matches."
The trap
The trap is reading the branches in business order rather than match order. The business hierarchy is standard → high → premium, smallest tier first. The CASE has to be written in the opposite direction: premium → high → standard, restrictive first. The two orderings produce different answers from the same data, and only the restrictive-first order encodes the actual band semantics.
You practiced ordering CASE branches from most-restrictive to least-restrictive. The recurring trap: PostgreSQL stops at the first WHEN that matches, so a broader condition placed before a narrower one will swallow rows that should have reached the narrower branch — the broader branch silently wins.