Career Hub/Interview questions/7-Day Rolling Listen Average
Spotify logo

7-Day Rolling Listen Average

Spotify·medium·SQL

You are given daily listen counts per artist. Calculate the 7-day rolling average of daily listens for each artist. The window covers each day plus the 6 days before it. Return artist_id, name, listen_date, daily_listens, and the rolling average rounded to 2 decimal places, ordered by artist_id then listen_date.

Schema

listen_logs
log_idINTEGER
artist_idINTEGER
listen_dateDATE
daily_listensINTEGER
artists
artist_idINTEGER
nameVARCHAR
genreVARCHAR

Target output

Target output
artist_idnamelisten_datedaily_listensrolling_7_day_avg
1Olivia Dean2024-01-011,2001200.00
1Olivia Dean2024-01-021,3501275.00
1Olivia Dean2024-01-039801176.67
1Olivia Dean2024-01-041,1001157.50
1Olivia Dean2024-01-051,4201210.00
1Olivia Dean2024-01-061,6001275.00
1Olivia Dean2024-01-071,2501271.43
1Olivia Dean2024-01-081,3801297.14
2The Midnight2024-01-01400400.00
2The Midnight2024-01-02600500.00
2The Midnight2024-01-03500500.00

What this question tests

Tests the 7-day rolling average with an explicit ROWS BETWEEN frame. It is the cleanest probe of whether you actually control window frames: the default frame quietly produces a running average from the start of time instead of a 7-day roll.

More SQL interview questions

  • Premium Artist Diversity · Spotify · medium
  • Highly Rated Listings · Airbnb · easy
  • Vacant Days in 2021 · Airbnb · hard
Querylisten_logs, artists
Cmd+Enterto runLoading...
Output

Loading sample data...