N029-M3 Tier 3 · Intermediate · medium ecommerce · Brightlane

Return the total order value from these customers as a single figure

Part of NULL Handling in Joins and Aggregates in SQL

The problem

Brightlane's finance team is auditing revenue from recently acquired customers — those who registered on or after 2025-01-01 00:00:00+00.

Write a query to return the total order value from these customers as a single figure.

Assumptions:

  • Recently acquired customers are those whose created_at is on or after 2025-01-01 00:00:00+00. Every such customer must contribute to the figure, including those who have placed no orders.
  • If no recently acquired customer has placed any order, the figure should be 0 rather than missing.

Output:

  • A single row with one column, total_revenue, containing the total order value.
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
  COALESCE(SUM(o.total_amount), 0) AS total_revenue
FROM
  customers c
  LEFT JOIN orders o ON c.id = o.customer_id
WHERE
  c.created_at >= '2025-01-01 00:00:00+00'::TIMESTAMPTZ

The shape

The LEFT JOIN plus the WHERE on a left-side column keeps every recently acquired customer in the calculation, even those with no orders. Their orders contribute missing values to the SUM, SUM skips missing values, and COALESCE rewrites the all-missing result to the presentational 0 the finance report needs.

Clause by clause

  • SELECT COALESCE(SUM(o.total_amount), 0) AS total_revenue returns the audit figure. SUM(o.total_amount) adds up every non-missing order amount across the qualifying customers. If no qualifying customer placed any order, every attached o.total_amount is missing, SUM returns missing, and COALESCE(..., 0) converts that missing result to 0.
  • FROM customers c LEFT JOIN orders o ON c.id = o.customer_id pairs each customer with their orders. A customer with no orders still appears, with every o.* column missing.
  • WHERE c.created_at >= '2025-01-01 00:00:00+00'::timestamptz restricts to recently acquired customers. The condition is on c.created_at — a left-side column — so it filters customers rather than dropping unmatched rows. The cast pins the comparison to a timestamptz value so the literal is interpreted in the same time zone as the stored column.

Why this and not WHERE o.total_amount IS NOT NULL

That filter would defeat the LEFT JOIN for any customer with no orders and produce a result equal to the inner-join sum. On this data the answer would happen to look the same, but the meaning is different. The version here computes "total revenue from the recently acquired population, including those with zero revenue." The WHERE o.total_amount IS NOT NULL version computes "total revenue across recently acquired customers who placed at least one order." The first matches the prompt; the second silently changes the population the figure is about.

The trap

A bare SUM on an all-missing set returns missing, not zero, and a missing total displayed in a finance report reads as a data gap rather than as the genuine zero it is. COALESCE(SUM(...), 0) is the deliberate presentational rewrite. The choice matters: a region with no qualifying customers and a region with qualifying customers who spent nothing are analytically different, and only the second is a true zero. On this prompt the population is "any qualifying customer including zero-spenders," so the rewrite is correct; on a different population, leaving the missing value alone would be the right call.

You practiced COALESCE(SUM(...), 0) — when SUM operates over only missing values it returns missing, so wrapping it in COALESCE produces the presentational zero a report needs.

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.