N016-H2 Tier 2 · Core SQL · hard ecommerce · Brightlane

- `'premium'` for orders with `total_amount > $1,500`. - `'high'` for orders with `total_amount > $500` but **not above** `$1,500`. - `'standard'` for all other orders

Part of CASE WHEN Expressions in SQL

The problem

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 with total_amount > $1,500.
  • 'high' for orders with total_amount > $500 but not above $1,500.
  • 'standard' for all other orders.

Assumptions:

  • An order at total_amount = $1,500 is 'high' (it does not exceed $1,500).
  • An order at total_amount = $500 is '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, and tier.
Schema · ecommerce 5 tables
categories
id integer
name text
parent_id? integer
products
id integer
name text
category_id integer
price numeric
stock_qty integer
attributes? jsonb
order_items
id integer
order_id integer
product_id integer
quantity integer
unit_price numeric
customers
id integer
name text
email text
city? text
country text
created_at timestamptz
is_active boolean
orders
id integer
customer_id integer
ordered_at timestamptz
status text
total_amount numeric

Run previews · Check grades

Write a query, then run it to see results here.

Worked solution Try it yourself first
Solution query
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_amount returns 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 — so total_amount is 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 because 1500 > 1500 is false but 1500 > 500 is 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 fails 500 > 500, falls past both WHEN branches, and lands in 'standard' — again matching the prompt.
  • END AS tier closes the expression.
  • FROM orders is 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.

How you actually get good at SQL

Reading explains SQL. Writing it, over and over with instant feedback, is what makes you fluent.

That's the whole SQLMaxx loop: 600+ real problems, instant AI feedback, mastery you can actually see, and spaced review that won't let you forget.

A stack of SQL practice problem cards, the top card showing an employees table.
615 problems · 66 concepts

Real problems. Not toy examples.

615 hand-built problems spanning all 66 concepts, from basic SELECTs to window functions, built on real schemas and real business questions, the kind you'll actually get asked on the job. Enough reps to make SQL automatic.

A retro computer showing a SQL query marked correct with a green checkmark.
Instant AI feedback

Write a query. Know if it's right in one second.

No copying an answer and hoping it clicked. The AI grader checks your real query against real data, catches exactly what's wrong, and explains the fix in plain English, like a senior analyst reading over your shoulder on every problem.

A circular mastery progress dial filling from blue to green, the SQLMaxx diamond at its center.
Mastery tracking

Stop guessing whether you actually know it.

SQLMaxx tracks every concept and shows you what you've mastered and what's still shaky. Your skills fill in one concept at a time, so 'I think I get joins' becomes something you can prove.

A SQL query editor circled by a blue return arrow with a clock, scheduled to come back for review.
Spaced review

Learn it once. Keep it for good.

Most of what you learn this week fades by next week. So when a concept comes due for review, SQLMaxx hands you a fresh problem to solve from a blank editor, not a flashcard to re-read. A research-backed spaced-repetition algorithm (FSRS) times each return for right before you'd forget, so your SQL is still there months later, when the interview or the job actually needs it.

Practice, feedback, mastery, review. That's the loop that turns reading into real skill.

Start free

No account, no credit card. Start solving in under a minute.