Back to questions

Spotify Streaming History Spotify SQL Interview Question

Spotify Streaming History

Spotify SQL Interview Question

You're given two tables containing data on Spotify users' streaming activity: which has historical streaming data, and which has data from the current week.

Write a query that outputs the user ID, song ID, and cumulative count of song plays up to August 4th, 2022, sorted in descending order.

Assume that there may be new users or songs in the table that are not present in the table.

Definitions:

  • table only contains data for the week of August 1st to August 7th, 2022.
  • table contains data up to July 31st, 2022. The query should include historical data from this table.

Table:

Column NameType
history_idinteger
user_idinteger
song_idinteger
song_playsinteger

Example Input:

history_iduser_idsong_idsong_plays
10011777123811
1245269545201

field contains the historical data of the number of times a user has played a particular song.

Table:

Column NameType
user_idinteger
song_idinteger
listen_timedatetime

Example Input:

user_idsong_idlisten_time
777123808/01/2022 12:00:00
695452008/04/2022 08:00:00
125963008/04/2022 16:00:00
695985208/07/2022 12:00:00

Example Output:

user_idsong_idsong_plays
777123812
69545202
12596301

On 4 August 2022, the data shows that User 777 listened to the song with song ID 1238 for a total of 12 times, with 11 of those times occurring before the current week and 1 time occurring within the current week.

However, the streaming data for User 695 with the song ID 9852 are not included in the output because the streaming date for that record falls outside the date range specified in the question.

The dataset you are querying against may have different input & output - this is just an example!

Input

Output