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.
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.
Tables:
- `users(user_id INTEGER PRIMARY KEY, username VARCHAR(100) NOT NULL)`: one row per user; `username` is the display name.
- `viewing_history(view_id INTEGER PRIMARY KEY, user_id INTEGER, title VARCHAR(200) NOT NULL, duration INTEGER NOT NULL, view_date DATE NOT NULL)`: one row per viewing. `user_id` is the viewer and refers to `users.user_id`; `duration` is the positive number of minutes watched in that record.
Output:
- 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.
Rules:
- 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 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.
Tables
users(user_id INTEGER, username VARCHAR(100))
viewing_history(view_id INTEGER, user_id INTEGER, title VARCHAR(200), duration INTEGER, view_date DATE)
Hints
- A user's total for a title has to exist before it can be compared with that user's other totals.
- Apply the date window to individual viewings before adding them up: 2024-01-30 is inside it and 2024-01-31 is outside.