Find each user's most-watched title by total minutes from January 1 to 30, 2024

Quick Overview

Write a SQL query that finds, for each user, the title with the most total minutes watched between January 1 and January 30, 2024, returning every title that ties. It tests grouped aggregation, inclusive and exclusive date boundaries, and tie-preserving ranking with window functions.

Find each user's most-watched title by total minutes from January 1 to 30, 2024

Company: Netflix

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: medium

Interview Round: Other

Each row of `viewing_history` records one viewing: a user watched a title for `duration` minutes on `view_date`. For each user, find the title with the highest total `duration` viewed from 2024-01-01 to 2024-01-30 inclusive. If several titles tie for a user's highest total, return all of them. ```hint Two levels of aggregation A user's total for a title has to exist before it can be compared with that user's other totals. ``` ### Input Table(s) `users` | Column | Type | Meaning | |---|---|---| | user_id | INT | User identifier. Primary key. | | username | VARCHAR(100) | Display name. NOT NULL. | `viewing_history` | Column | Type | Meaning | |---|---|---| | view_id | INT | Viewing record identifier. Primary key. | | user_id | INT | The viewer. Foreign key to `users.user_id`. | | title | VARCHAR(200) | Title watched. NOT NULL. | | duration | INT | Minutes watched in this record. NOT NULL, positive. | | view_date | DATE | Date of the viewing. NOT NULL. | ### Output Contract - Columns, in this order: `user_id`, `username`, `title`, `total_duration`. - `total_duration` is the sum of `duration` over that user's rows for that title inside the window. It is an integer, so no rounding applies. - Return one row for each title that ties for the user's highest total in the window. - Order by `user_id` ascending, then `title` ascending. Each `(user_id, title)` pair appears once, so the order is fully determined. ### Example `users` | user_id | username | |---|---| | 1 | alice | | 2 | bob | | 3 | cara | `viewing_history` | view_id | user_id | title | duration | view_date | |---|---|---|---|---| | 1 | 1 | Night Shift | 50 | 2024-01-03 | | 2 | 1 | Night Shift | 40 | 2024-01-20 | | 3 | 1 | Harbor Lights | 70 | 2024-01-10 | | 4 | 1 | Harbor Lights | 60 | 2024-01-31 | | 5 | 2 | Red Desert | 45 | 2024-01-05 | | 6 | 2 | Cold Trail | 30 | 2024-01-06 | | 7 | 2 | Cold Trail | 15 | 2024-01-30 | | 8 | 3 | Harbor Lights | 100 | 2023-12-31 | Expected result: | user_id | username | title | total_duration | |---|---|---|---| | 1 | alice | Night Shift | 90 | | 2 | bob | Cold Trail | 45 | | 2 | bob | Red Desert | 45 | Alice's Night Shift totals 50 + 40 = 90. Her Harbor Lights total is 70, because the viewing on January 31 falls outside the window. Bob's Cold Trail totals 30 + 15 = 45 (January 30 is inside the window), which ties Red Desert, so both rows are returned. Cara's only viewing is on December 31, so she has no row. ### Constraints and Clarifications - The window is `view_date >= '2024-01-01'` and `view_date < '2024-01-31'`: January 30 counts and January 31 does not. - Repeated viewings of the same title by the same user add up into one row. Two records that match in everything except `view_id` are separate viewings and both count. - Users with no viewing records in the window do not appear in the result. - Every column except `viewing_history.user_id` is declared NOT NULL. A viewing row with a NULL `user_id` belongs to no user and is not reported. - Titles are compared exactly as stored. - `users` has at most 10,000 rows and `viewing_history` at most 100,000 rows. - `duration` is a positive integer.

Overview: Write a SQL query that finds, for each user, the title with the most total minutes watched between January 1 and January 30, 2024, returning every title that ties. It tests grouped aggregation, inclusive and exclusive date boundaries, and tie-preserving ranking with window functions.

Netflix logo
Netflix
Nov 24, 2026
mediumData ScientistOtherData Manipulation (SQL/Python)
0
0

Each row of viewing_history records one viewing: a user watched a title for duration minutes on view_date. For each user, find the title with the highest total duration viewed from 2024-01-01 to 2024-01-30 inclusive. If several titles tie for a user's highest total, return all of them.

Input Table(s)

users

ColumnTypeMeaning
user_idINTUser identifier. Primary key.
usernameVARCHAR(100)Display name. NOT NULL.

viewing_history

ColumnTypeMeaning
view_idINTViewing record identifier. Primary key.
user_idINTThe viewer. Foreign key to users.user_id.
titleVARCHAR(200)Title watched. NOT NULL.
durationINTMinutes watched in this record. NOT NULL, positive.
view_dateDATEDate of the viewing. NOT NULL.

Output Contract

  • Columns, in this order: user_id , username , title , total_duration .
  • total_duration is the sum of duration over that user's rows for that title inside the window. It is an integer, so no rounding applies.
  • Return one row for each title that ties for the user's highest total in the window.
  • Order by user_id ascending, then title ascending. Each (user_id, title) pair appears once, so the order is fully determined.

Example

users

user_idusername
1alice
2bob
3cara

viewing_history

view_iduser_idtitledurationview_date
11Night Shift502024-01-03
21Night Shift402024-01-20
31Harbor Lights702024-01-10
41Harbor Lights602024-01-31
52Red Desert452024-01-05
62Cold Trail302024-01-06
72Cold Trail152024-01-30
83Harbor Lights1002023-12-31

Expected result:

user_idusernametitletotal_duration
1aliceNight Shift90
2bobCold Trail45
2bobRed Desert45

Alice's Night Shift totals 50 + 40 = 90. Her Harbor Lights total is 70, because the viewing on January 31 falls outside the window. Bob's Cold Trail totals 30 + 15 = 45 (January 30 is inside the window), which ties Red Desert, so both rows are returned. Cara's only viewing is on December 31, so she has no row.

Constraints and Clarifications

  • The window is view_date >= '2024-01-01' and view_date < '2024-01-31' : January 30 counts and January 31 does not.
  • Repeated viewings of the same title by the same user add up into one row. Two records that match in everything except view_id are separate viewings and both count.
  • Users with no viewing records in the window do not appear in the result.
  • Every column except viewing_history.user_id is declared NOT NULL. A viewing row with a NULL user_id belongs to no user and is not reported.
  • Titles are compared exactly as stored.
  • users has at most 10,000 rows and viewing_history at most 100,000 rows.
  • duration is a positive integer.