N045-M2 Tier 4 · Advanced · medium ecommerce · Brightlane

Return the order status, the interpolated median `total_amount`, and the actual-value median `total_amount` for each `status`. Sort the final result by `status` ascending

Part of NTILE and Percentile Functions in SQL

The problem

Brightlane's operations team is comparing two median-calculation methods within each order-status group.

Write a query to return the order status, the interpolated median total_amount, and the actual-value median total_amount for each status. Sort the final result by status ascending.

Assumptions:

  • Each unique status value should appear once.
  • The interpolated median sorts the values within a status ascending and picks the value at the midpoint position, interpolating linearly between the two middle values when the count is even. The result may not equal any actual order amount.
  • The actual-value median sorts the values within a status ascending and picks the actual value at or above the midpoint position. The result is always one of the actual order amounts (the lower of the two middle values when the count is even).
  • The final result is sorted by status ascending.

Output:

  • One row per status, with columns status, median_cont, and median_disc. Sorted by status.
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
  status,
  PERCENTILE_CONT(0.5) WITHIN GROUP (
    ORDER BY
      total_amount
  ) AS median_cont,
  PERCENTILE_DISC(0.5) WITHIN GROUP (
    ORDER BY
      total_amount
  ) AS median_disc
FROM
  orders
GROUP BY
  status
ORDER BY
  status

The shape

Two percentile functions on the same column at the same percentile, grouped by status, expose the difference between an interpolated median and an actual-value median side by side. PERCENTILE_CONT(0.5) and PERCENTILE_DISC(0.5) both ask for the 50th percentile of total_amount within each status, but they answer differently when the row count is even: CONT interpolates between the two middle values, DISC returns the lower of them verbatim.

Clause by clause

  • SELECT status, PERCENTILE_CONT(0.5) WITHIN GROUP (ORDER BY total_amount) AS median_cont, PERCENTILE_DISC(0.5) WITHIN GROUP (ORDER BY total_amount) AS median_disc returns the status and both medians in a single row per status. Each percentile function carries its own WITHIN GROUP (ORDER BY total_amount) because each is a separate ordered-set aggregate; they happen to use the same sort, but the syntax requires the clause inside each function call.
  • FROM orders reads every order. No WHERE.
  • GROUP BY status partitions the orders by status, which is what reduces the result to one row per status.
  • ORDER BY status is the outer sort. It prints the statuses alphabetically so the comparison reads in a stable order.

Why both functions on the same row

The whole point of the analysis is to compare the two methods. Computing them on the same status, in the same query, on the same row of output, makes the comparison direct: the operations team reads median_cont next to median_disc and sees exactly where they agree and where they diverge. When a status has an odd row count, the two columns will be equal; when it has an even row count, they will typically differ.

The trap

WITHIN GROUP (ORDER BY ...) is not the same syntax as OVER (ORDER BY ...). The first defines the sort for an ordered-set aggregate; the second defines the sort for a window function. These two percentile functions are aggregates here, not window functions, so they live inside GROUP BY status and collapse rows. Writing OVER (...) instead of WITHIN GROUP (...) here would turn them into window functions and the query would no longer return one row per status.

You practiced PERCENTILE_CONT vs PERCENTILE_DISC side by side — both within WITHIN GROUP (ORDER BY ...); _CONT interpolates between values, _DISC returns an actual value from the input.

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.