Choose the Most Prolific Reviewer and February’s Best-Rated Movie
Company: Amazon
Role: Software Engineer
Category: Data Manipulation (SQL/Python)
Difficulty: medium
Interview Round: Other
### Problem
Return the available component rows in a column named results: first the name of the user who rated the most movies overall, breaking a count tie by lexicographically smaller name; then the title of the movie with the highest average rating during February 2020, breaking an average tie by lexicographically smaller title. Preserve the user-then-movie order when both components are present, including duplicate result values. In a tie on review count or mean rating, a missing name or title sorts after all recorded names or titles.
### Missing values
The original statement does not define the result when a component has no eligible candidate. This adapted exercise omits that component's row. User activity counts matching rating records, including records whose rating value is NULL; a user is eligible when it has at least one such record. For the movie component, compute each February mean from known, non-NULL ratings only. A movie is eligible only if it has at least one known February rating; an all-NULL February group has no defined mean and contributes no candidate. This missing-rating/undefined-mean rule is an additional curator clarification, not a source NOT NULL or nonempty-population guarantee. Return the qualifying user row before the qualifying movie row when both exist, one row when only one exists, and no rows when neither exists. Do not invent a nonempty-cohort guarantee or emit a NULL placeholder for an absent component. This omission policy is our curator clarification, not an original-source fact.
### Tables
Table: `Movies`
```text
+---------------+---------+
| Column Name | Type |
+---------------+---------+
| movie_id | int |
| title | varchar |
+---------------+---------+
movie_id is the primary key (column with unique values) for this table.
title is the name of the movie.
Each movie has a unique title.
```
Table: `Users`
```text
+---------------+---------+
| Column Name | Type |
+---------------+---------+
| user_id | int |
| name | varchar |
+---------------+---------+
user_id is the primary key (column with unique values) for this table.
The column 'name' has unique values.
```
Table: `MovieRating`
```text
+---------------+---------+
| Column Name | Type |
+---------------+---------+
| movie_id | int |
| user_id | int |
| rating | int |
| created_at | date |
+---------------+---------+
(movie_id, user_id) is the primary key (column with unique values) for this table.
This table contains the rating of a movie by a user in their review.
created_at is the user's review date.
```
### Example
```text
Movies (movie_id,title): (1,Aurora), (2,Bay)
Users (user_id,name): (1,Ada), (2,Bo)
MovieRating (movie_id,user_id,rating,created_at): (1,1,4,2020-02-01), (2,1,5,2020-02-02), (1,2,2,2020-02-03), (2,2,1,2020-02-04)
Output (results): (Ada), (Aurora)
Both users rated2 movies; both movies average3 in February.
```
### Clarification example
```text
Using the same Movies and Users as above, replace MovieRating by:
(movie_id,user_id,rating,created_at): (1,1,4,2020-01-15)
Output (results): (Ada)
```
Ada is the eligible reviewer winner; no movie has a February rating, so the movie component contributes no row.
### Missing-rating clarification example
```text
Using the same Movies and Users, replace MovieRating by:
(movie_id,user_id,rating,created_at): (1,1,NULL,2020-02-10), (2,1,5,2020-02-11)
Output (results): (Ada), (Bay)
```
Ada has two rating records. Aurora has no known February rating, while Bay's known-rating mean is 5. If both rating values were NULL, Ada would still be the user result and the movie component would be omitted.
### What a Strong Answer Covers
- Determine the two winners from their respective review populations and apply the stated tie-breaks.
- Return the results in component order, preserving equal values and omitting absent components.
### Follow-up Questions
- How would you return the top three results for each component?
- How would you report the winners for every calendar month?
Overview: Choose the most prolific reviewer overall and the best-rated movie in February 2020, applying the specified tie-breaks.
Return the available component rows in a column named results: first the name of the user who rated the most movies overall, breaking a count tie by lexicographically smaller name; then the title of the movie with the highest average rating during February 2020, breaking an average tie by lexicographically smaller title. Preserve the user-then-movie order when both components are present, including duplicate result values. In a tie on review count or mean rating, a missing name or title sorts after all recorded names or titles.
Missing values
The original statement does not define the result when a component has no eligible candidate. This adapted exercise omits that component's row. User activity counts matching rating records, including records whose rating value is NULL; a user is eligible when it has at least one such record. For the movie component, compute each February mean from known, non-NULL ratings only. A movie is eligible only if it has at least one known February rating; an all-NULL February group has no defined mean and contributes no candidate. This missing-rating/undefined-mean rule is an additional curator clarification, not a source NOT NULL or nonempty-population guarantee. Return the qualifying user row before the qualifying movie row when both exist, one row when only one exists, and no rows when neither exists. Do not invent a nonempty-cohort guarantee or emit a NULL placeholder for an absent component. This omission policy is our curator clarification, not an original-source fact.
Tables
Table: Movies
+---------------+---------+
| Column Name | Type |
+---------------+---------+
| movie_id | int |
| title | varchar |
+---------------+---------+
movie_id is the primary key (column with unique values) for this table.
title is the name of the movie.
Each movie has a unique title.
Table: Users
+---------------+---------+
| Column Name | Type |
+---------------+---------+
| user_id | int |
| name | varchar |
+---------------+---------+
user_id is the primary key (column with unique values) for this table.
The column 'name' has unique values.
Table: MovieRating
+---------------+---------+
| Column Name | Type |
+---------------+---------+
| movie_id | int |
| user_id | int |
| rating | int |
| created_at | date |
+---------------+---------+
(movie_id, user_id) is the primary key (column with unique values) for this table.
This table contains the rating of a movie by a user in their review.
created_at is the user's review date.
Example
Movies (movie_id,title): (1,Aurora), (2,Bay)
Users (user_id,name): (1,Ada), (2,Bo)
MovieRating (movie_id,user_id,rating,created_at): (1,1,4,2020-02-01), (2,1,5,2020-02-02), (1,2,2,2020-02-03), (2,2,1,2020-02-04)
Output (results): (Ada), (Aurora)
Both users rated2 movies; both movies average3 in February.
Clarification example
Using the same Movies and Users as above, replace MovieRating by:
(movie_id,user_id,rating,created_at): (1,1,4,2020-01-15)
Output (results): (Ada)
Ada is the eligible reviewer winner; no movie has a February rating, so the movie component contributes no row.
Missing-rating clarification example
Using the same Movies and Users, replace MovieRating by:
(movie_id,user_id,rating,created_at): (1,1,NULL,2020-02-10), (2,1,5,2020-02-11)
Output (results): (Ada), (Bay)
Ada has two rating records. Aurora has no known February rating, while Bay's known-rating mean is 5. If both rating values were NULL, Ada would still be the user result and the movie component would be omitted.
What a Strong Answer Covers Guidance
Determine the two winners from their respective review populations and apply the stated tie-breaks.
Return the results in component order, preserving equal values and omitting absent components.
Follow-up Questions Guidance
How would you return the top three results for each component?
How would you report the winners for every calendar month?