Listening sessions split by a 30-minute gap
The task
The product team defines a listening session from play timestamps alone. Use plays that started in the first week of June 2025 (from 2025-06-01 up to, but not including, 2025-06-08).
For each listener, order their plays in that week by played_at, then play_id. A play starts a new session when it is the listener's first play of the week, or when more than 30 minutes have passed since the played_at of the listener's previous play in that week. A gap of exactly 30 minutes does not start a new session. Plays that are not separated by a bigger gap belong to the same session. Do not look at plays before June 1.
Return one row per listener who has at least one play in the week:
listener_idsessions: the number of sessionslongest_session_plays: the number of plays in the listener's biggest session
Sort by listener_id.
Expected output
listener_idsessionslongest_session_plays- Sorted by listener_id ascending.
- Checked on the sample and on 2 hidden datasets. How grading works
Tables Music streaming service · click a name to insert it
listeners180 rows in the sample
One row per listener account. Key: listener_id.
| int | Unique id. | |
| date | Day they joined. | |
| text | Two-letter country code. | |
| text | free, premium or family. |
artists30 rows in the sample
One row per artist. Key: artist_id.
| int | Unique id. | |
| text | Name. | |
| text | pop, rock, hip-hop, electronic, jazz or folk. |
tracks116 rows in the sample
One row per track. Key: track_id.
| int | Unique id. | |
| text | Track title. | |
| int | Main artist. | |
| int | Length. | |
| date | Release day. |
plays6,555 rows in the sample
One row per play. Key: play_id.
| bigint | Unique id. | |
| int | Who listened. | |
| int | What. | |
| timestamp | Start time (UTC). | |
| int | How long they listened. | |
| text | phone, desktop, speaker or car. |
Related questions
- Longest run of consecutive driver cancellationshard
- Longest run of delivery days per campaignmedium
- Merge subscription rows into continuous customer stretcheshard
- Missing inventory snapshot ranges per SKUmedium
- Streaks of four or more days of opening the appmedium
- Artist catalog size and 2025 releasesmedium
Loading the editor and PostgreSQL
Loading the editor and PostgreSQL