
Listening time so far
mediumListening time so far
Spotify SQL Interview Question
Spotify counts a listener as engaged once they pass a threshold of minutes in a day, and the product team needs the moment each listener crosses it rather than the daily total. That means the minutes have to add up play by play through the day.
Return the listener_id, the played_at time of each play, the minutes that play lasted as minutes, and the listener's minutes so far including that play as running_minutes. Round both to 2 decimal places. Sort by listener_id and then by played_at.
Asked of
- Data Analyst
- Business Analyst
- BI Analyst
- Product Analyst
- Analytics Engineer
- Data Engineer
- Data Scientist
spotify_playsTable30 rows
| Column Name | Type |
|---|---|
| play_id | BIGINT |
| listener_id | VARCHAR |
| track_id | VARCHAR |
| played_at | TIMESTAMP |
| ms_played | BIGINT |
spotify_playsExample Input
| play_id | listener_id | track_id | played_at | ms_played |
|---|---|---|---|---|
| 5001 | L01 | T100 | 2024-03-04 08:02:00 | 210000 |
| 5002 | L01 | T104 | 2024-03-04 08:06:00 | 185000 |
| 5003 | L01 | T100 | 2024-03-04 08:10:00 | 198000 |
| 5004 | L01 | T112 | 2024-03-04 09:15:00 | 240000 |
| 5005 | L01 | T104 | 2024-03-04 09:21:00 | 172000 |
| 5006 | L01 | T100 | 2024-03-04 18:40:00 | 205000 |
| 5007 | L01 | T118 | 2024-03-04 18:44:00 | 160000 |
| 5008 | L01 | T112 | 2024-03-04 18:49:00 | 233000 |
| 5026 | L04 | T100 | 2024-03-04 11:00:00 | 204000 |
| 5027 | L04 | T100 | 2024-03-04 11:05:00 | 201000 |
| 5028 | L04 | T104 | 2024-03-04 15:45:00 | 178000 |
| 5029 | L04 | T118 | 2024-03-04 15:52:00 | 162000 |
| 5030 | L04 | T121 | 2024-03-04 15:58:00 | 249000 |
Example Output
| listener_id | played_at | minutes | running_minutes |
|---|---|---|---|
| L01 | 2024-03-04 08:02:00 | 3.5 | 3.5 |
| L01 | 2024-03-04 08:06:00 | 3.08 | 6.58 |
| L01 | 2024-03-04 08:10:00 | 3.3 | 9.88 |
| L01 | 2024-03-04 09:15:00 | 4 | 13.88 |
| L01 | 2024-03-04 09:21:00 | 2.87 | 16.75 |
| L01 | 2024-03-04 18:40:00 | 3.42 | 20.17 |
| L01 | 2024-03-04 18:44:00 | 2.67 | 22.83 |
| L01 | 2024-03-04 18:49:00 | 3.88 | 26.72 |
| L04 | 2024-03-04 11:00:00 | 3.4 | 3.4 |
| L04 | 2024-03-04 11:05:00 | 3.35 | 6.75 |
| L04 | 2024-03-04 15:45:00 | 2.97 | 9.72 |
| L04 | 2024-03-04 15:52:00 | 2.7 | 12.42 |
| L04 | 2024-03-04 15:58:00 | 4.15 | 16.57 |
Explanation
L01's first play lasts 3.5 minutes, so the running figure starts there. The next play of 3.08 minutes takes it to 6.58, and the one after takes it to 9.88. L04 starts again from its own first play rather than carrying on from L01.
The example above is a small slice of the data. Your query runs against the full tables.
Company
Spotify
Difficulty
medium
Topic
running totals
Language
SQL
Your query
Starting the SQL engine...