N063-E2 Tier 5 · Expert · easy analytics · Streamhub

Return each user's `id` and the total number of `events` they have generated across all of their `sessions`, with `total_event_count` reported as `0` for users with no events on record

Part of NULL Propagation in Complex Queries in SQL

The problem

Scenario: Streamhub's product team is auditing user engagement depth and needs every user paired with their total event count, including users with no sessions and users whose sessions have no recorded events.

Task: Write a query to return each user's id and the total number of events they have generated across all of their sessions, with total_event_count reported as 0 for users with no events on record.

Assumptions:

  • A user's total_event_count is the count of events recorded across all of their sessions.
  • The result covers every user.
  • A user with no recorded events appears with total_event_count of 0.

Output:

  • One row per user.
  • Columns in this order: user_id, total_event_count.
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,
  COUNT(e.id) AS total_event_count
FROM
  users u
  LEFT JOIN sessions s ON s.user_id = u.id
  LEFT JOIN events e ON e.session_id = s.id
GROUP BY
  u.id

The shape

Two chained LEFT JOINs walk from users down through sessions to events, and COUNT(e.id) returns 0 for any user whose branch ran out before reaching an event. Counting the child id, rather than COUNT(*), is what makes the empty case land on zero instead of one.

Clause by clause

  • SELECT u.id AS user_id, COUNT(e.id) AS total_event_count returns one row per user with the count of events attached to them.
  • FROM users u LEFT JOIN sessions s ON s.user_id = u.id keeps every user in the result, even those with no sessions. For a sessionless user, every sessions column is NULL.
  • LEFT JOIN events e ON e.session_id = s.id extends the chain to events while preserving users whose sessions had no events. For those users, e.id is also NULL.
  • GROUP BY u.id collapses back to one row per user so COUNT runs per user.

The trap

COUNT(*) would return 1 for a user with no sessions, not 0. The LEFT JOIN left one row per user with all the right-side columns set to NULL, and COUNT(*) counts that row because it counts rows regardless of whether any column is NULL. COUNT(e.id) ignores NULL values, so the placeholder row contributes nothing and the user reports 0 events.

You practiced chaining two left-joins so users without sessions and sessions without events both reach the count step, where counting the child id naturally produces 0 for empty branches.

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.