Cumulative Song Plays
Spotify ☯️ Medium SQL
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:

Input:

songs_history
history_idlisten_countsong_idsong_nameuser_id
1200101Midnight Rain1
2150102Neon Lights1
3300101Midnight Rain2
songs_weekly
listen_countsong_idsong_nameuser_idweekly_id
50101Midnight Rain11
80103Electric Pulse22
120101Midnight Rain33

Output:
song_idsong_nametotal_playsuser_id
101Midnight Rain3002
101Midnight Rain2501
102Neon Lights1501
101Midnight Rain1203
103Electric Pulse802
Quick Solution

Code Environment

Sign in or try as guest to run your code.

Sign In

Track

Question Difficulty Company Access
Need more practice in this area? Explore more questions →