Cumulative Song Plays
Beginner Mode
Objective
Write a SQL query to output the cumulative play count for each user-song pair up to Feb 6th, 2026.
Table Schema:
- songs_history
| Column | Type | Description |
|---|---|---|
| history_id | INTEGER | Unique identifier for each record |
| user_id | INTEGER | ID of the user |
| song_id | INTEGER | ID of the song |
| song_name | VARCHAR | Name of the song |
| listen_count | INTEGER | Cumulative plays up to the previous week |
- songs_weekly
| Column | Type | Description |
|---|---|---|
| weekly_id | INTEGER | Unique identifier for each record |
| user_id | INTEGER | ID of the user |
| song_id | INTEGER | ID of the song |
| song_name | VARCHAR | Name of the song |
| listen_count | INTEGER | Number of plays during the current week ending Feb 6th, 2026 |
Task Requirements:
- Combine play counts from both historical and current weekly data
- Include user-song pairs that exist in only one of the two tables (new listeners or songs with no plays this week)
- Calculate the total cumulative play count per user-song pair
- Sort the results by total plays in descending order
Output columns: user_id, song_id, song_name, total_plays
Examples
Example 1:
Output:
Input:
| songs_history | ||||
|---|---|---|---|---|
| history_id | listen_count | song_id | song_name | user_id |
| 1 | 200 | 101 | Midnight Rain | 1 |
| 2 | 150 | 102 | Neon Lights | 1 |
| 3 | 300 | 101 | Midnight Rain | 2 |
| songs_weekly | ||||
|---|---|---|---|---|
| listen_count | song_id | song_name | user_id | weekly_id |
| 50 | 101 | Midnight Rain | 1 | 1 |
| 80 | 103 | Electric Pulse | 2 | 2 |
| 120 | 101 | Midnight Rain | 3 | 3 |
| song_id | song_name | total_plays | user_id |
|---|---|---|---|
| 101 | Midnight Rain | 300 | 2 |
| 101 | Midnight Rain | 250 | 1 |
| 102 | Neon Lights | 150 | 1 |
| 101 | Midnight Rain | 120 | 3 |
| 103 | Electric Pulse | 80 | 2 |
Code Environment
Sign in or try as guest to run your code.
Track
| Question | Difficulty | Company | Access |
|---|
Need more practice in this area? Explore more questions →
Spotify