N062-H2 Tier 5 · Expert · hard analytics · Streamhub

Return each session's `id`, the `user_id` it belongs to, the count of `events` in that session, the earliest event timestamp, and the latest event timestamp

Part of Choosing Between Subqueries, CTEs, and Joins in SQL

The problem

Scenario: Streamhub's analytics team needs a per-session summary covering every session — including sessions with no recorded events.

Task: Write a query to return each session's id, the user_id it belongs to, the count of events in that session, the earliest event timestamp, and the latest event timestamp.

Assumptions:

  • A session's event_count is the number of events recorded against it; first_event_at and last_event_at are the earliest and latest event timestamps within the session.
  • The result covers every session.
  • A session with no recorded events appears with event_count of 0 and both first_event_at and last_event_at reported as missing values.

Output:

  • One row per session.
  • Columns in this order: session_id, user_id, event_count, first_event_at, last_event_at.
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
  s.id AS session_id,
  s.user_id,
  event_stats.event_count,
  event_stats.first_event_at,
  event_stats.last_event_at
FROM
  sessions s
  LEFT JOIN LATERAL (
    SELECT
      COUNT(*) AS event_count,
      MIN(occurred_at) AS first_event_at,
      MAX(occurred_at) AS last_event_at
    FROM
      events e
    WHERE
      e.session_id = s.id
  ) AS event_stats ON TRUE

The shape

LEFT JOIN LATERAL runs a three-aggregate query once per session, returning the count, earliest timestamp, and latest timestamp in a single pass. Sessions with no events still appear because the join's LEFT half preserves every outer row.

Clause by clause

  • FROM sessions s drives the query; every session is preserved.
  • LEFT JOIN LATERAL (SELECT COUNT(*) AS event_count, MIN(occurred_at) AS first_event_at, MAX(occurred_at) AS last_event_at FROM events e WHERE e.session_id = s.id) AS event_stats ON true runs once per session. The inner query filters events to that session and computes all three aggregates simultaneously. ON true is the standard LATERAL pairing — every outer row keeps its lateral subquery's row.
  • SELECT s.id AS session_id, s.user_id, event_stats.event_count, event_stats.first_event_at, event_stats.last_event_at reads the session identifiers and all three aggregates from the lateral result.

Why LATERAL and not pre-aggregate + LEFT JOIN

A pre-aggregated derived table is the natural alternative:

SELECT s.id AS session_id, s.user_id,
       COALESCE(es.event_count, 0) AS event_count,
       es.first_event_at, es.last_event_at
FROM sessions s
LEFT JOIN (
    SELECT session_id,
           COUNT(*) AS event_count,
           MIN(occurred_at) AS first_event_at,
           MAX(occurred_at) AS last_event_at
    FROM events GROUP BY session_id
) es ON es.session_id = s.id

Both shapes are correct. The pre-aggregated form aggregates events once across the whole table and then joins; LATERAL aggregates once per session. On a session table much smaller than the event table, pre-aggregation usually scales better; on a session table where most rows are then filtered down to a few, LATERAL can be faster because it only aggregates for sessions you keep. The two are equivalent in correctness; the choice is a cardinality and selectivity question.

The trap

The empty-set behavior of the three aggregates is not uniform. For a session with no matching events, the lateral subquery executes against an empty input and returns one row containing COUNT(*) = 0, MIN(occurred_at) = NULL, MAX(occurred_at) = NULL. That row matches the prompt's contract exactly: event_count of 0, both timestamps NULL. The pre-aggregated alternative behaves differently — sessions with no events match no row in the derived table, so all three columns come back NULL, including event_count, and you'd need COALESCE(event_count, 0) to recover the contract. LATERAL's "always return one row, even if empty" semantics is what makes the zero-count case fall out for free here.

You practiced returning three per-session metrics from a single LATERAL subquery — a shape that delivers multi-column output that a scalar correlated subquery cannot.

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.