logo

Back to questions

Top 5 Artists [Spotify SQL Interview Question]

Medium

Assume there are three Spotify tables containing information about the artists, songs, and music charts. Write a query to find the top 5 artists whose songs appear most frequently in the Top 10 of the table.

Display the top 5 artist names in ascending order, along with their song appearance ranking. Note that if two artists have the same number of song appearances, they should have the same ranking, and the rank numbers should be continuous (i.e. 1, 2, 2, 3, 4, 5).

For instance, if Ed Sheeran appears in the Top 10 five times and Bad Bunning four times, Ed Sheeran should be ranked 1st, and Bad Bunny should be ranked 2nd.

Table:

Column NameType
artist_idinteger
artist_namevarchar

Example Input:

artist_idartist_name
101Ed Sheeran
120Drake

Table:

Column NameType
song_idinteger
artist_idinteger

Example Input:

song_idartist_id
45202101
19960120

Table:

Column NameType
dayinteger (1-52)
song_idinteger
rankinteger (1-1,000,000)

Example Input:

daysong_idrank
1452025
3452022
1199603
91996015

Example Output:

artist_nameartist_rank
Ed Sheeran1
Drake2

Explanation

Ed Sheeran's song appeared twice in the Top 10 list of global song rank while Drake's song is only listed once. Therefore, Ed is ranked #1 and Drake is ranked #2.

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

PostgreSQL 14