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

Return each customer's `id`, name, and `pending_order_id` — the `id` of any pending order they have on record, reported as a missing value for customers with no pending order

Part of NULL Propagation in Complex Queries in SQL

The problem

Scenario: Brightlane's marketing team needs every customer paired with any pending order they have on record — customers with no pending order should still appear in the report.

Task: Write a query to return each customer's id, name, and pending_order_id — the id of any pending order they have on record, reported as a missing value for customers with no pending order.

Assumptions:

  • A pending order has status equal to 'pending'.
  • Customers with no pending order still appear in the result, with pending_order_id reported as a missing value. In the current data, no customer has more than one pending order, so the result has exactly one entry per customer.

Output:

  • One row per customer (one per customer-pending-order pairing, plus one for each customer with no pending order).
  • Columns in this order: customer_id, customer_name, pending_order_id.
  • Sorted by customer_name ascending, then pending_order_id ascending.
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
  c.id AS customer_id,
  c.name AS customer_name,
  o.id AS pending_order_id
FROM
  customers c
  LEFT JOIN orders o ON o.customer_id = c.id
  AND o.status = 'pending'
ORDER BY
  c.name,
  o.id

The shape

The status = 'pending' filter sits inside the ON clause, not in a WHERE, because the marketing report has to keep customers who have no pending order. With the filter in the ON clause, a customer with no pending order fails the join condition; the LEFT JOIN preserves the customer row with o.id as NULL. With the filter in the WHERE clause, that same NULL fails the equality test and the customer disappears.

Clause by clause

  • SELECT c.id AS customer_id, c.name AS customer_name, o.id AS pending_order_id returns the three columns the report needs. o.id is the pending order's id when one exists and NULL when the join found no match.
  • FROM customers c LEFT JOIN orders o ON o.customer_id = c.id AND o.status = 'pending' pairs each customer with their pending order. The join condition has two parts joined by AND: the standard customer-to-order link, plus the status restriction. Both must be true for an order row to attach; a non-pending order fails the second part and is treated as no match.
  • ORDER BY c.name, o.id sorts the result by customer name, with pending order id as the tiebreaker.

The trap

The trap is in where the status = 'pending' test lives. Two queries that look almost identical produce different result sets:

-- Right: customers with no pending order are kept
FROM customers c
LEFT JOIN orders o ON o.customer_id = c.id AND o.status = 'pending'

-- Wrong: customers with no pending order are silently dropped
FROM customers c
LEFT JOIN orders o ON o.customer_id = c.id
WHERE o.status = 'pending'

The wrong version performs the LEFT JOIN first, preserves the orderless customer with o.status set to NULL, then runs the WHERE clause and discards that row because NULL = 'pending' evaluates to NULL, which WHERE treats as false. The LEFT JOIN is silently converted to an inner join. Any restriction on the right-hand table of a LEFT JOIN that must not eliminate unmatched left-side rows belongs inside the ON clause. The two clauses look interchangeable, and they are not.

You practiced placing a status restriction inside the left-join condition rather than in the outer restriction — so customers without a matching pending order still appear with a missing-value id.

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.