Query Top Played Tracks Globally And By Country
SpotifyMachine Learning Engineer · Technical ScreenLast updated
Tests writing clean SQL queries with aggregations, window functions, and deterministic tie-breaking to extract top playback metrics globally and per country.
Music Streaming Analytics: Top Tracks
As a Machine Learning Engineer on a music recommendation team, you need to extract popular tracks from streaming event logs to feed popularity-based candidate generators and editorial dashboards.
You are given a table playback_events that logs user listening activity:
playback_events (
event_id BIGINT PRIMARY KEY,
user_id BIGINT,
track_id BIGINT,
country_code VARCHAR(2),
played_at TIMESTAMP,
duration_secs INT
)
A track play is valid if duration_secs >= 30.
Task
Write two separate SQL queries for playback events recorded between 2024-01-01 00:00:00 and 2024-01-31 23:59:59 (inclusive):
- Global Top 5: Return the top 5 tracks globally by total valid play count. The output must include
track_idandplay_count, ordered byplay_countdescending, thentrack_idascending. - Country Top 3: Return the top 3 tracks for every country by total valid play count. Each country must have at most 3 rows. The output must include
country_code,track_id,play_count, andrank(from 1 to 3), ordered bycountry_codeascending, thenrankascending.
Requirements & Edge Cases
- Filter out any play where
duration_secs < 30. - Break ties deterministically: if two tracks have the same play count, the track with the lower
track_idtakes precedence. - If a country has fewer than 3 unique played tracks, return all available tracks for that country.
Example Output
Global Top 5:
+----------+------------+
| track_id | play_count |
+----------+------------+
| 1042 | 54200 |
| 2011 | 48100 |
| 105 | 48100 |
| 9901 | 39050 |
| 451 | 31200 |
+----------+------------+
(Note: Track 105 and 2011 are tied at 48100; track 105 ranks ahead due to lower track_id.)
Country Top 3:
+--------------+----------+------------+------+
| country_code | track_id | play_count | rank |
+--------------+----------+------------+------+
| BR | 884 | 12000 | 1 |
| BR | 1042 | 11500 | 2 |
| BR | 503 | 9200 | 3 |
| SE | 1042 | 8100 | 1 |
| SE | 772 | 4300 | 2 |
+--------------+----------+------------+------+
Hints
Hint 1
Pre-filter the raw logs on `duration_secs` and the date range before performing any aggregation.
Hint 2
For the country query, aggregate play counts by `(country_code, track_id)` first in a CTE before applying a window function.
Hint 3
Use `ROW_NUMBER() OVER (PARTITION BY country_code ORDER BY play_count DESC, track_id ASC)` to guarantee an exact, deterministic top 3 per country.
Follow-up questions
- How does using ROW_NUMBER() differ from RANK() or DENSE_RANK() when handling ties, and which would you choose if product specifications required showing all tied tracks?
- If playback_events contains 10 billion rows, how would you optimize data layout (partitioning, clustering, pre-aggregation) to make daily top-track queries fast?
- How would you adapt this logic to compute a rolling 7-day top tracks window refreshed on an hourly basis?
Solution walkthrough is for members
Get the full walkthrough with reference code and complexity analysis, plus Kitty Coach at 300 messages a day. Hints, the code runner and community answers stay free.