N031-M4 Tier 3 · Intermediate · medium analytics · Streamhub

Return each qualifying user's ID and their count of high-intensity sessions

Part of Chained CTEs in SQL

The problem

Streamhub's product team wants to know which users have had more than one high-intensity session — a session with at least 5 events.

Write a query to return each qualifying user's ID and their count of high-intensity sessions.

Assumptions:

  • A high-intensity session has an event_count of 5 or more.
  • A user's qualifying-session count is the number of high-intensity sessions linked to that user_id.
  • Only users with more than 1 qualifying session should appear.

Output:

  • One row per qualifying user, with columns user_id and qualifying_sessions.
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
  high_event_sessions AS (
    SELECT
      user_id,
      event_count
    FROM
      sessions
    WHERE
      event_count >= 5
  ),
  user_counts AS (
    SELECT
      user_id,
      COUNT(*) AS qualifying_sessions
    FROM
      high_event_sessions
    GROUP BY
      user_id
  )
SELECT
  user_id,
  qualifying_sessions
FROM
  user_counts
WHERE
  qualifying_sessions > 1

The shape

Three pieces of work spread across two CTEs and the main query. The first CTE keeps only the high-intensity sessions, the second counts those qualifying sessions per user, and the main query applies the "more than one" threshold on top of the counted rows. Two restrictions of different shapes, with a count between them.

Clause by clause

The first CTE drops every low-intensity session:

WITH high_event_sessions AS (
  SELECT user_id, event_count
  FROM sessions
  WHERE event_count >= 5
)

WHERE event_count >= 5 runs against the raw session rows, before any grouping. After this layer, every row is by definition a high-intensity session.

The second CTE counts those rows per user:

user_counts AS (
  SELECT user_id, COUNT(*) AS qualifying_sessions
  FROM high_event_sessions
  GROUP BY user_id
)

GROUP BY user_id produces one row per user, and COUNT(*) counts only the qualifying sessions because that is the only thing in the source layer. A user with zero qualifying sessions never appears here.

The main query applies the threshold on the per-user count:

SELECT user_id, qualifying_sessions
FROM user_counts
WHERE qualifying_sessions > 1

WHERE qualifying_sessions > 1 references the aggregate from the previous layer by its alias. Because the count was materialized into a named column in user_counts, the main query's WHERE can read it as if it were any other column.

Why the count threshold lives in the main query and not back in the first CTE

The two thresholds operate on different things. event_count >= 5 is a per-row condition on the raw sessions table, and it has to run before grouping because the column it tests exists only on the source rows. qualifying_sessions > 1 is a condition on an aggregated value, and the aggregate does not exist until GROUP BY runs. Splitting them across layers is what lets each one run at the level it makes sense at: the per-row filter on the per-row layer, the per-group filter on the per-group layer.

The trap

The two thresholds look similar in the prompt and easy to swap. Reading "more than one high-intensity session" as the row-level threshold would lead to WHERE event_count > 1 in the first CTE, which keeps almost every session and then counts them all. The > 1 is on the count of qualifying sessions per user, not on the events inside each session. The level at which each > applies is what the chained structure is making explicit.

You practiced layering two WITH stages with a final main-query restriction — pre-restrict in the first stage, count in the second, threshold-check in the main query.

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.