N058-M4 Tier 5 · Expert · medium analytics · Streamhub

Return each plan, the number of active users on that plan, the total `events` those users have generated across all time, and the average `events` per active user

Part of Multi-CTE Query Architecture in SQL

The problem

Scenario: Streamhub's analytics team wants to understand engagement levels across plan tiers — how active users on each plan compare in event volume.

Task: Write a query to return each plan, the number of active users on that plan, the total events those users have generated across all time, and the average events per active user.

Assumptions:

  • An active user has is_active equal to TRUE.
  • An active user with no recorded events contributes 0 to their event count rather than dropping out of the per-plan averages.

Output:

  • One row per plan with at least one active user.
  • Columns in this order: plan, active_users, total_events, avg_events.
  • Sorted by avg_events descending.
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
WITH
  active_users AS (
    SELECT
      u.id AS user_id,
      u.plan
    FROM
      users u
    WHERE
      u.is_active = TRUE
  ),
  user_activity AS (
    SELECT
      au.user_id,
      au.plan,
      COUNT(e.id) AS event_count
    FROM
      active_users au
      LEFT JOIN events e ON e.user_id = au.user_id
    GROUP BY
      au.user_id,
      au.plan
  ),
  plan_summary AS (
    SELECT
      plan,
      COUNT(user_id) AS active_users,
      SUM(event_count) AS total_events,
      AVG(event_count) AS avg_events
    FROM
      user_activity
    GROUP BY
      plan
  )
SELECT
  plan,
  active_users,
  total_events,
  avg_events
FROM
  plan_summary
ORDER BY
  avg_events DESC

The shape

Three CTEs that protect the per-plan average from a silent miscount. The first names the active-user set, the second left-joins those users to events so a user with zero events still produces a row, and the third aggregates per plan. The LEFT JOIN in the middle layer is the load-bearing piece: without it, zero-event users would vanish before the average gets computed.

Clause by clause

WITH active_users AS (
    SELECT u.id AS user_id, u.plan
    FROM users u
    WHERE u.is_active = TRUE
)

The active-user set is named once and reused. Only user_id and plan are needed downstream, so other columns are dropped.

user_activity AS (
    SELECT au.user_id, au.plan, COUNT(e.id) AS event_count
    FROM active_users au
    LEFT JOIN events e ON e.user_id = au.user_id
    GROUP BY au.user_id, au.plan
)

The LEFT JOIN keeps every active user even when no event row matches. COUNT(e.id) counts non-null e.id values, which means a user with no events gets a count of 0 rather than vanishing. Grouping by user_id and plan produces one row per active user with their personal event count.

plan_summary AS (
    SELECT plan, COUNT(user_id) AS active_users, SUM(event_count) AS total_events, AVG(event_count) AS avg_events
    FROM user_activity
    GROUP BY plan
)

The per-plan rollup runs across the full per-user set, zeros included. COUNT(user_id) is the active-user count per plan; SUM and AVG use the per-user event_count. Enterprise averages 8.8 events per user, free averages 0.54.

  • SELECT plan, active_users, total_events, avg_events FROM plan_summary ORDER BY avg_events DESC returns the four plans ordered by per-user engagement.

The trap

Switching the LEFT JOIN to an INNER JOIN in the middle layer would silently change the average. Users with no events stop producing rows, so the per-plan AVG divides by a smaller denominator and the number inflates. The active_users count would also drop, but the divergence between the two would not raise any error. The prompt explicitly preserves zero-event users in the average, and the LEFT JOIN plus COUNT(e.id) is what enforces that.

You practiced staging active users, per-user event counts, and per-plan summaries as three CTEs, so users with zero events still appear in the average rather than dropping out.

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.