Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Top 3 Genres Per User

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

Your question is Top 3 Genres Per User. Start with the requirements and the three 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

Paramount+ wants a personalized summary of the genres each subscriber watched most during the previous 30 days. Assume the reporting timestamp is 2025-02-01 00:00:00, and viewing records at or after that timestamp are excluded.

Write a PostgreSQL query to identify the top three genres for each user, ranked by total watch time.

Requirements

  1. Join users, viewing events, and titles to associate watch time with users and genres.
  2. Include only events from the 30-day interval ending immediately before the reporting timestamp.
  3. Aggregate watch time by user and genre, ignoring events with missing titles, genres, or watch durations.
  4. Rank genres independently for each user and return only ranks 1 through 3.
  5. Return user_id, user_name, genre, total_watch_seconds, and genre_rank, ordered by user and rank.

Schema

users
ColumnTypeDescription
user_idPKINTSubscriber identifier
user_nameVARCHAR(100)Subscriber display name
viewing_events
ColumnTypeDescription
event_idPKINTViewing event identifier
user_idINTSubscriber who generated the event
title_idINTWatched title identifier
watched_atTIMESTAMPTimestamp when viewing occurred
watch_secondsINTSeconds watched during the event
titles
ColumnTypeDescription
title_idPKINTParamount+ title identifier
title_nameVARCHAR(200)Title name
genreVARCHAR(50)Title genre
Tablesusersviewing_eventstitles
Your solutionPostgreSQL
You need to log in / sign up to run or submit.
Run a query to see results