Top track per listener

ranking

Top track per listener

Spotify SQL Interview Question

Spotify builds a personal year-in-review around the one track a listener returned to most. Getting it wrong is worse than showing nothing, because the whole feature trades on the listener recognizing themselves in it.

For each listener, return the listener_id, the track_id they played most often, and the number of times they played it as plays.

Asked of

  • Data Analyst
  • Business Analyst
  • BI Analyst
  • Product Analyst
  • Analytics Engineer
  • Data Engineer
  • Data Scientist

spotify_playsTable30 rows

Column NameType
play_idBIGINT
listener_idVARCHAR
track_idVARCHAR
played_atTIMESTAMP
ms_playedBIGINT

spotify_playsExample Input

play_idlistener_idtrack_idplayed_atms_played
5001L01T1002024-03-04 08:02:00210000
5002L01T1042024-03-04 08:06:00185000
5003L01T1002024-03-04 08:10:00198000
5004L01T1122024-03-04 09:15:00240000
5005L01T1042024-03-04 09:21:00172000
5006L01T1002024-03-04 18:40:00205000
5007L01T1182024-03-04 18:44:00160000
5008L01T1122024-03-04 18:49:00233000
5026L04T1002024-03-04 11:00:00204000
5027L04T1002024-03-04 11:05:00201000
5028L04T1042024-03-04 15:45:00178000
5029L04T1182024-03-04 15:52:00162000
5030L04T1212024-03-04 15:58:00249000

Example Output

listener_idtrack_idplays
L01T1003
L04T1002

Explanation

L01 played T100 three times, more than any other track, so that is their top one. L04 played T100 twice and everything else once.

The example above is a small slice of the data. Your query runs against the full tables.