Dataford
Interview QuestionsInterview GuidesExperiencesCoaching
New
Pricing
Get started
Dataford
Popular roles
Software EngineerData AnalystData ScientistData EngineerBusiness AnalystAI EngineerMachine Learning EngineerProduct Manager
Browse
Browse All RolesEvery role hub, from analyst to MLBrowse All CompaniesCompany-specific interview loopsAll Interview GuidesThe full guide library
Top questions by role
Software EngineerData AnalystData ScientistData EngineerBusiness AnalystAI EngineerMachine Learning EngineerProduct Manager
Top questions by skill
SQLPythonStatisticsMachine LearningA/B TestingSystem DesignGenerative AIProduct SenseMetricsBehavioral
Browse all questions →Try a mock interview
Experiences
Practice
Mock InterviewsTimed interview simulations with feedbackCoaching1:1 coaching with people at your target companiesSuccess PathYour 6-week structured planModulesCurated lessons by topicWebinarsTalks from ex-Big Tech data leadsPlaygroundA free-form scratch editor
Learn
BlogInterview strategy and career adviceTech Job Market ReportHiring trends across data and AI rolesFor UniversitiesDataford for career centersAbout DatafordWho we are and how we build
Pricing
Build my plan
Top 3 Genres Per User
00:00
5 left

Top 3 Genres Per User

MediumSQL · PostgreSQL

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
Interviewer

Your question is Top 3 Genres Per User. Start with the requirements and the three 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
Sign up free to run your codeLog inLn 1
Run your query to see results here.