N063-H3 Tier 5 · Expert · hard analytics · Streamhub

Return each user's `id` and their `total_conversion_spend` — the combined `amount` across all of their `conversions`, reported as a missing value for users with no conversions on record

Part of NULL Propagation in Complex Queries in SQL

The problem

Scenario: Streamhub's retention team is analyzing user conversion behavior and needs every user paired with their total conversion spend. Users who have never converted must appear with total_conversion_spend reported as a missing value — the distinction between 'no conversions' and 'zero spend' is analytically meaningful.

Task: Write a query to return each user's id and their total_conversion_spend — the combined amount across all of their conversions, reported as a missing value for users with no conversions on record.

Assumptions:

  • A user's total_conversion_spend is the combined amount across all of their conversions.
  • The result covers every user.
  • A user with no conversions on record appears with total_conversion_spend reported as a missing value rather than 0.

Output:

  • One row per user.
  • Columns in this order: user_id, total_conversion_spend.
  • Sorted by user_id ascending.
Schema · analytics 5 tables
users
id integer
name text
email text
country text
plan text
signed_up_at timestamptz
is_active boolean
conversions
id integer
user_id integer
converted_at timestamptz
plan text
amount numeric
sessions
id integer
user_id integer
started_at timestamptz
ended_at? timestamptz
event_count integer
events
id integer
user_id integer
session_id? integer
event_type text
occurred_at timestamptz
properties? jsonb
periods
id integer
name text
start_month integer
end_month integer

Run previews · Check grades

Write a query, then run it to see results here.

Worked solution Try it yourself first
Solution query
SELECT
  u.id AS user_id,
  SUM(c.amount) AS total_conversion_spend
FROM
  users u
  LEFT JOIN conversions c ON c.user_id = u.id
GROUP BY
  u.id
ORDER BY
  u.id

The shape

The LEFT JOIN keeps every user in the result, and SUM(c.amount) returns NULL — not zero — for users with no conversions on record. The retention team specifically wants that NULL because "no conversions" is analytically distinct from "zero spend"; no COALESCE wrapper is the right call here, and that is the load-bearing decision.

Clause by clause

  • SELECT u.id AS user_id, SUM(c.amount) AS total_conversion_spend returns one row per user with their combined conversion spend. The aggregate runs over whatever conversions rows matched the join. For a user with at least one conversion, SUM returns the populated total; for a user with no conversions, the group contains one placeholder row with c.amount set to NULL, and SUM over a single NULL input returns NULL.
  • FROM users u LEFT JOIN conversions c ON c.user_id = u.id pairs each user with their conversions. The LEFT JOIN preserves users who have none; for those users, every conversions column is NULL.
  • GROUP BY u.id collapses the per-conversion rows back to one row per user so the aggregate produces a per-user total.
  • ORDER BY u.id returns the report sorted by user id.

The trap

The reflex on a LEFT JOIN plus an aggregate is to reach for COALESCE(SUM(c.amount), 0). That reflex is correct on every other problem in this node; on this one it is the bug. Wrapping SUM with COALESCE(..., 0) would convert the NULL for non-converting users into 0, which the retention team would then average alongside the populated spends. A user who converted for $0 and a user who never converted at all would then be indistinguishable in the report, and the population mean would shift toward zero by however many non-converters exist. The fix is to read the assumptions and the output spec carefully and let NULL stand. The grader on this problem accepts NULL in total_conversion_spend for non-converters — substituting 0 would fail it. The shape of "preserve the missing-value signal" is sometimes the answer, and sometimes the rest of the report depends on you noticing that.

You practiced preserving the missing-value total that a left-join produces for empty parents — refusing to substitute 0 so the analytical distinction between 'no record' and 'zero' stays intact.

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.