N061-H1 Tier 5 · Expert · hard ecommerce · Brightlane

Return each qualifying `product_id` and its total units sold

Part of Query Structure Patterns for Performance in SQL

The problem

Scenario: Brightlane's inventory analytics team is identifying products whose total units sold exceed the average total units sold across the catalog.

Task: Write a query to return each qualifying product_id and its total units sold.

Assumptions:

  • The order_items table holds one row per line item on an order, with product_id linking back to products.id and quantity recording the unit count.
  • A product's total_units is the combined quantity across all of its line items.
  • The result covers only products whose total_units is strictly greater than the average total_units across every product that has at least one line item on record.

Output:

  • One row per qualifying product.
  • Columns in this order: product_id, total_units.
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
WITH
  product_sales AS (
    SELECT
      product_id,
      SUM(quantity) AS total_units
    FROM
      order_items
    GROUP BY
      product_id
  )
SELECT
  ps.product_id,
  ps.total_units
FROM
  product_sales ps
WHERE
  ps.total_units > (
    SELECT
      AVG(total_units)
    FROM
      product_sales
  )

The shape

The same per-product total has to appear twice — once as the value on each output row, and once aggregated again into the catalog-wide average that the threshold compares against. A CTE that computes SUM(quantity) per product once and gets referenced twice in the main statement is the shape that expresses this cleanly without recomputing the totals.

Clause by clause

  • The CTE product_sales reads order_items, groups by product_id, and computes SUM(quantity) AS total_units. One row per product that has at least one line item, with the per-product total already in place.
  • SELECT ps.product_id, ps.total_units FROM product_sales ps reads the CTE as the outer driving set. Every row already carries its per-product total.
  • WHERE ps.total_units > (SELECT AVG(total_units) FROM product_sales) is a scalar subquery that reads the same CTE a second time, averages the per-product totals across every product, and returns a single number. The outer WHERE compares each row's total_units against that single average and keeps only the rows that exceed it.

Why a CTE and not two derived tables

Without the CTE, the per-product aggregation has to be written twice — once for the outer row source and once inside the scalar subquery that computes the average. Two copies of the same aggregation drift over time and read as noise. A CTE names the aggregation once, and both references read from the same named set, which is the structural value the problem is practicing.

The trap

The threshold here is the average across products, not the average across line items. Writing WHERE total_units > (SELECT AVG(quantity) FROM order_items) is a different number entirely — it averages every line item's quantity, ignoring how line items are distributed across products. A product with one line item of quantity 10 and a product with ten line items of quantity 1 contribute equally to the per-product average (each contributes one row of value 10 and one row of value 10 respectively), but they contribute eleven rows of very different shapes to the per-line-item average. The prompt asks for the per-product average; the CTE makes that choice explicit by aggregating to one row per product first, and the scalar subquery averages over that already-collapsed set.

You practiced computing per-product totals once in a CTE, then referencing that same set twice — for the per-product value and for the all-product average — a shape only a CTE expresses cleanly.

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.