Build viewing sessions from raw play events. Events of the same user belong to the same session while the gap to the previous event is 30 minutes or less; a gap of more than 30 minutes starts a new session. Identical (user_id, event_ts) rows are duplicates and count once. Session length = minutes between its first and last event (a single-event session lasts 0). Return `user_id`, `sessions`, `avg_session_min` (ROUND 1). Order by `user_id`.
| Column | Type |
|---|---|
| user_id | INTEGER |
| event_ts | TIMESTAMP |
| title_id | INTEGER |
| user_id | event_ts | title_id |
|---|---|---|
| 1 | 2026-05-10 20:00:00 | 501 |
| 1 | 2026-05-10 20:20:00 | 501 |
| 1 | 2026-05-10 20:50:00 | 502 |
| 1 | 2026-05-10 22:00:00 | 503 |
| 1 | 2026-05-10 22:10:00 | 503 |
| 2 | 2026-05-10 09:00:00 | 501 |
| 3 | 2026-05-10 23:50:00 | 510 |
| 3 | 2026-05-11 00:15:00 | 510 |
| user_id | sessions | avg_session_min |
|---|---|---|
| 1 | 2 | 30 |
| 2 | 1 | 0 |
| 3 | 1 | 25 |
Solve it in the live editor on this page — run on sample data, then submit against hidden tests.
‹ Driver Idle Time Between Rides · Binge Watchers Within 24 Hours ›
{{ totalCount }} questions in the style asked at Amazon, Microsoft, Google, Meta, Flipkart and more — {{ hardPct }}% Hard. Write SQL in a real database engine in your browser, run it on sample data, then evaluate against hidden tests.
Company tags show where this type of question is commonly reported in interviews. Questions, data and solutions are written by TechTown.
{{ cur.statement }}
No submissions yet. Press Evaluate to submit your query.
{{ solutionSql }}
{{ out.error }}
You have solved your 3 free questions — nice work! Log in (free) to unlock all 150 questions, keep your streak and appear on the leaderboard.