N013-M4 Tier 2 · Core SQL · medium ecommerce · Brightlane

Return the total unit count in a single column named `total_units`

Part of Aggregate Functions (COUNT, SUM, AVG, MIN, MAX) in SQL

The problem

Brightlane's wholesale team wants the total number of units sold on high-value line items — specifically, order lines where the per-unit price exceeded $500.

Write a query to return the total unit count in a single column named total_units.

Assumptions:

  • The order_items table contains one row per product per order.
  • quantity is the number of units on the line; unit_price is the per-unit price at time of purchase.
  • Only line items with unit_price > 500 should contribute to the total.

Output:

  • A single row with one column, 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
SELECT
  SUM(quantity) AS total_units
FROM
  order_items
WHERE
  unit_price > 500

The shape

The filter narrows order_items to only the line items whose unit_price exceeds $500, and SUM then totals the quantity column across those filtered rows — the count of units sold at the premium tier. The answer is 39 units, across however many line items met the threshold.

Clause by clause

  • FROM order_items is the source: every line item across every order, one row per (order, product) combination.
  • WHERE unit_price > 500 filters first, keeping only line items where the unit price is strictly greater than $500. The comparison is exclusive: unit_price = 500 does not pass. Premium-tier items, by Brightlane's working definition, sit above the threshold.
  • SUM(quantity) AS total_units then totals the quantity column across the filtered rows. The filter is on unit_price, but the sum is on quantity. WHERE and SUM can target different columns: the filter decides which rows count, and SUM decides which column from those rows gets added.
  • The result is the number of premium-tier units sold. A line where 12 premium units were sold contributes 12 to the total; a line where 1 was sold contributes 1.

Why this and not SUM(unit_price) over the same filter

SUM(unit_price) FROM order_items WHERE unit_price > 500 would total the unit prices of premium line items — the dollar value of one of each, ignoring how many were sold. That tells you something about the price spread at the premium tier, not about units moved. Multiplying unit_price * quantity would be needed to get premium revenue, which is a different question again.

The team asked for unit volume at the premium tier, so the sum has to be on the volume column rather than the price column. The filter and the aggregate target different columns on purpose.

You practiced summing a column over a WHERE-filtered subset. The recurring shape: any time the question is "the total of X for the rows that meet condition Y," the answer is SUM(X) ... WHERE Y.

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.