N041-H2 Tier 3 · Intermediate · hard ecommerce · Brightlane

Return each qualifying customer's ID and total spend

Part of Temp Tables and CREATE TABLE AS SELECT in SQL

The problem

Brightlane's high-value customer pipeline materializes customers whose total spend exceeds the platform-wide average individual order amount into a temp table. The temp table feeds downstream segmentation analyses.

Write a query to return each qualifying customer's ID and total spend.

Assumptions:

  • A customer's total spend is the combined total_amount across every order linked to that customer_id.
  • The platform-wide average individual order amount is the average of total_amount across every order in the table.
  • Only customers whose total spend is greater than the platform-wide average individual order amount should appear.

Output:

  • One row per qualifying customer, with columns customer_id and total_spent.
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
  customer_id,
  total_spent
FROM
  (
    SELECT
      customer_id,
      SUM(total_amount) AS total_spent
    FROM
      orders
    GROUP BY
      customer_id
  ) customer_totals
WHERE
  total_spent > (
    SELECT
      AVG(total_amount)
    FROM
      orders
  )

The shape

A derived table computes each customer's total spend, and the outer WHERE compares those totals against a scalar subquery that returns the platform-wide average individual order amount. Two aggregations at different grain in one statement: the inner one is per-customer, the comparison value is across all orders. Both have to be computed and then compared at row level for each customer.

Clause by clause

  • (SELECT customer_id, SUM(total_amount) AS total_spent FROM orders GROUP BY customer_id) customer_totals is the inner derived table. It groups orders by customer_id, sums the order amounts in each group, and exposes a two-column row source with the per-customer totals.
  • SELECT customer_id, total_spent FROM customer_totals reads the derived table and returns both columns.
  • WHERE total_spent > (SELECT AVG(total_amount) FROM orders) is the threshold. The inner SELECT AVG(total_amount) FROM orders is an uncorrelated scalar subquery: it runs once, scans every order, and returns the single number 633.62865. The outer WHERE then compares each customer's total_spent against that constant.

Why two different aggregations and not one

The two numbers being compared live at different grains. A customer's total spend is the sum across all their orders, computed inside the derived table. The platform-wide average is the average over individual orders across the whole company, computed by the scalar subquery against the raw orders table. Folding them into one GROUP BY would mean computing both at the same grain, which loses the meaning of either. Separating them — per-customer in one place, all-orders in the other — preserves what each number represents.

The trap

It is tempting to compare per-customer totals to the per-customer average, which would be a different filter entirely. AVG(total_amount) over the orders table is not the average customer's total spend. It is the average individual order amount, a smaller number. A customer with a single 700-dollar order would clear that bar; the per-customer average total would be much higher and might not. The threshold the pipeline materializes is the one against the individual order average, so the scalar subquery has to be SELECT AVG(total_amount) FROM orders directly, with no GROUP BY and no derived table in front. Reading "average" in the spec without checking what is being averaged is the silent mistake here.

You practiced comparing per-customer aggregates against a scalar subquery threshold — total-spend per customer above the average individual order amount; the kind of restricted result worth materializing once and querying repeatedly.

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.