Search ⌘K
Spotify
Spotify

Interview Core Track · Medium · 25 min

8. Calculate the average listening duration for each music genre on Spotify

Suppose that Spotify would like to understand better the average listening duration for each genre of music on their platform. **Write a SQL query that calcu...

Interview Core Track
Medium
25 min
joins
aggregation
date functions

Company labels are directional practice context, not official interview guidance.

Timer 00:00
Back to practice

Objective

Practice joins through a Spotify-tagged business scenario.

Approach

Use this track to improve speed, edge-case handling, and accuracy under timed conditions.

Company context

Company labels are directional practice context, not official interview guidance.

Prereq: query basics
Prereq: filtering

Suppose that Spotify would like to understand better the average listening duration for each genre of music on their platform. **Write a SQL query that calculates the average listening duration per genre for every month.** Assume you have access to a user_activity table and a songs table with the following schema: user_activity **Example Input:** activity_id user_id song_id timestamp listening_duration_sec ------------------------------------------------------------------- 1 101 5001 2022-03-01 09:00:00 210 2 102 6985 2022-03-01 11:30:00 120 3 103 5001 2022-03-01 15:45:00 300 4 101 6985 2022-04-01 08:45:00 180 5 102 5001 2022-04-01 10:00:00 240 songs **Example Input:** song_id genre ---------------- 5001 Rock 6985 Pop Your aim is to produce a table like: **Example Output:** mth genre avg_listening_duration_sec ----------------------------------------- 3 Rock 255 3 Pop 120 4 Rock 240 4 Pop 180 **Answer:** SELECT EXTRACT(MONTH FROM timestamp) AS month, genre, AVG(listening_duration_sec) AS avg_listening_duration_sec FROM user_activity ua JOIN songs s USING(song_id) GROUP BY 1, 2 ---

Pro question
This is a Pro question

Upgrade to Pro to unlock this prompt, the SQL workspace, and all 254 problems.

Upgrade to Pro — unlock all 254 problems
Editor
SQL workspace

Run queries against the protected question data, then submit once the result shape looks right.

Sign in to run SQL

Create a free account or sign in before running queries against protected question data.