logo

Back to questions

Top 5 Artists [Spotify SQL Interview Question]

Medium

Assume there are three Spotify tables: , , and , which contain information about the artists, songs, and music charts, respectively.

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.

If two or more artists have the same number of song appearances, they should be assigned the same ranking, and the rank numbers should be continuous (i.e. 1, 2, 2, 3, 4, 5). If you've never seen a rank order like this before, do the rank window function tutorial.

Table:

Column NameType
artist_idinteger
artist_namevarchar
label_ownervarchar

Example Input:

artist_idartist_namelabel_owner
101Ed SheeranWarner Music Group
120DrakeWarner Music Group
125Bad BunnyRimas Entertainment

Table:

Column NameType
song_idinteger
artist_idinteger
namevarchar

Example Input:

song_idartist_idname
55511101Perfect
45202101Shape of You
22222120One Dance
19960120Hotline Bling

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