Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Popular Songs by Region

MediumSQL · PostgreSQL00:00
Practice interviewer
In session
5 left
00:00

Your question is Popular Songs by Region. Start with the requirements and the two tables on the right.

Run and submit as often as you like. When you're ready, talk me through your approach or go straight to the code.

You need to log in / sign up to run or submit.

Problem

Context

Spotify's Analytics Engineering team uses stream events to monitor which songs are gaining traction in each market. The regional ranking should reflect recent listening activity while excluding streams outside the target market and reporting periods.

Task

Write a PostgreSQL query to identify the top three songs streamed in Spotify's GB market during the 30-day period ending on 2025-02-01. Treat the period as inclusive of 2025-01-02 and exclusive of 2025-02-01.

Requirements

  1. Join stream_events to tracks and exclude events whose track does not exist in tracks.
  2. Filter to region_code = 'GB' and the specified 30-day period.
  3. Calculate total streams and distinct listeners for each song.
  4. Rank songs by total streams descending, include ties at the third rank, and order the final results by rank and song title.

Schema

tracks
ColumnTypeDescription
track_idPKINTUnique Spotify track identifier
track_titleVARCHAR(200)Song title
artist_nameVARCHAR(200)Display artist name
stream_events
ColumnTypeDescription
event_idPKBIGINTUnique stream event identifier
track_idINTStreamed track identifier
listener_idINTListener identifier, nullable for unidentified listening
region_codeVARCHAR(10)Listener market code
streamed_atTIMESTAMPStream event timestamp
Tablestracksstream_events
Your solutionPostgreSQL
You need to log in / sign up to run or submit.
Run a query to see results