Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started
Top Genres by Demographic
00:00
5 left

Top Genres by Demographic

MediumSQL · PostgreSQL

Problem

Given a table of user streaming sessions, write a query to find the top three most-watched genres per user demographic.

Use the provided users and streaming sessions data. Exclude sessions without a valid user, missing genres, or NULL viewing duration. Break ties alphabetically by genre.

Output

  1. One row per included demographic and genre, with demographic, genre, total_minutes_watched, and genre_rank.
  2. Include no more than three genres per demographic.
  3. Sort by demographic, then genre_rank, then genre.

Schema

users
ColumnTypeDescription
user_idPKINTUnique streaming user identifier
demographicVARCHAR(50)User demographic category
streaming_sessions
ColumnTypeDescription
session_idPKINTUnique streaming session identifier
user_idINTUser associated with the session
genreVARCHAR(50)Genre watched during the session
minutes_watchedINTNumber of minutes watched during the session
Tablesusersstreaming_sessions
Interviewer

Your question is Top Genres by Demographic. Start with the requirements and the two tables in the Question tab.

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.
CodePostgreSQL
You need to log in / sign up to run or submit.Ln 1
Run your query to see results here.