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

Return the total number of customer-order pairings and the number of actual order records as a single row

Part of NULL Handling in Joins and Aggregates in SQL

The problem

Brightlane's customer success team wants a global coverage summary describing how customer and order records pair together.

Write a query to return the total number of customer-order pairings and the number of actual order records as a single row.

Assumptions:

  • Every customer must contribute to the pairing count, including customers with no orders. Customers with no orders contribute a single pairing each with a missing order ID.
  • The pairing count includes every pairing record; the order count includes only pairings where an actual order is present.

Output:

  • A single row with columns customer_order_rows and order_count.
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
  COUNT(*) AS customer_order_rows,
  COUNT(o.id) AS order_count
FROM
  customers c
  LEFT JOIN orders o ON c.id = o.customer_id

The shape

COUNT(*) counts every row the LEFT JOIN produces, including the placeholder rows for customers with no orders. COUNT(o.id) counts only the rows where an actual order was attached, because COUNT(column) skips missing values by design. The gap between the two figures is exactly the number of customers the LEFT JOIN is preserving.

Clause by clause

  • SELECT COUNT(*) AS customer_order_rows, COUNT(o.id) AS order_count returns the two figures as a single row. COUNT(*) returns 208 — the full set of join output rows. COUNT(o.id) returns 200 — only the rows where the join actually attached an order. The difference, 8, equals the number of customers with no orders at all (the same eight surfaced by an anti-join).
  • FROM customers c LEFT JOIN orders o ON c.id = o.customer_id pairs each customer with their orders, preserving customers who have none by emitting a single row with every o.* column missing.

Why COUNT(o.id) and not COUNT(o.status) or COUNT(o.total_amount)

Any non-missing column on a real order would give the same result here, because every real order has every column recorded. But the safe column to count is the primary key, o.id. A primary key is guaranteed non-missing on real rows and guaranteed missing on the LEFT JOIN-introduced placeholders, so the count cleanly separates the two populations. Counting a column that can legitimately be missing on a real order would undercount.

The trap

COUNT(o.id) and COUNT(*) look interchangeable until the LEFT JOIN introduces missing values. After that, they answer different questions: COUNT(*) asks how many rows came out of the join, and COUNT(o.id) asks how many of those rows have an actual order attached. The two numbers only match when there are no unmatched rows.

You practiced COUNT(*) vs COUNT(right.id) after a LEFT JOIN — the first includes the unmatched-row placeholders, the second skips them.

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.