Movie Rating Problem
Description
LeetCode Problem 1341.
Table: Movies
+---------------+---------+
| Column Name | Type |
+---------------+---------+
| movie_id | int |
| title | varchar |
+---------------+---------+
movie_id is the primary key for this table.
title is the name of the movie.
Table: Users
+---------------+---------+
| Column Name | Type |
+---------------+---------+
| user_id | int |
| name | varchar |
+---------------+---------+
user_id is the primary key for this table.
Table: Movie_Rating
+---------------+---------+
| Column Name | Type |
+---------------+---------+
| movie_id | int |
| user_id | int |
| rating | int |
| created_at | date |
+---------------+---------+
(movie_id, user_id) is the primary key 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.
Write the following SQL query:
Find the name of the user who has rated the greatest number of movies. In case of a tie, return lexicographically smaller user name.
Find the movie name with the highest average rating in February 2020. In case of a tie, return lexicographically smaller movie name.
The query is returned in 2 rows, the query result format is in the following example:
Movies table:
+-------------+--------------+
| movie_id | title |
+-------------+--------------+
| 1 | Avengers |
| 2 | Frozen 2 |
| 3 | Joker |
+-------------+--------------+
Users table:
+-------------+--------------+
| user_id | name |
+-------------+--------------+
| 1 | Daniel |
| 2 | Monica |
| 3 | Maria |
| 4 | James |
+-------------+--------------+
Movie_Rating table:
+-------------+--------------+--------------+-------------+
| movie_id | user_id | rating | created_at |
+-------------+--------------+--------------+-------------+
| 1 | 1 | 3 | 2020-01-12 |
| 1 | 2 | 4 | 2020-02-11 |
| 1 | 3 | 2 | 2020-02-12 |
| 1 | 4 | 1 | 2020-01-01 |
| 2 | 1 | 5 | 2020-02-17 |
| 2 | 2 | 2 | 2020-02-01 |
| 2 | 3 | 2 | 2020-03-01 |
| 3 | 1 | 3 | 2020-02-22 |
| 3 | 2 | 4 | 2020-02-25 |
+-------------+--------------+--------------+-------------+
Result table:
+--------------+
| results |
+--------------+
| Daniel |
| Frozen 2 |
+--------------+
Daniel and Monica have rated 3 movies ("Avengers", "Frozen 2" and "Joker") but Daniel is smaller lexicographically.
Frozen 2 and Joker have a rating average of 3.5 in February but Frozen 2 is smaller lexicographically.
Solution
Approach: Two Top-1 Queries with UNION ALL
The result is two independent answers stacked into one column, so write two queries that each return one row and combine them.
User with the most ratings. Join Movie_Rating to Users, group by user, and sort by COUNT(*) descending, then by name ascending to break ties. LIMIT 1 takes the winner.
Movie with the highest average rating in February 2020. Keep only ratings with created_at >= '2020-02-01' and < '2020-03-01', join to Movies, group by movie, and sort by AVG(rating) descending, then by title. LIMIT 1 again.
Each part needs its own parentheses so that its ORDER BY ... LIMIT 1 applies to that part only.
Combine them with UNION ALL, not UNION. If the top user's name is the same string as the top movie's title, UNION would remove one of them and return a single row.
The Pandas version builds each answer separately: count ratings per user, average February ratings per movie, sort each by its value and then by name, and put the two top names in one results column.
(
SELECT u.name AS results
FROM Movie_Rating AS mr
JOIN Users AS u
ON u.user_id = mr.user_id
GROUP BY u.user_id, u.name
ORDER BY COUNT(*) DESC, u.name
LIMIT 1
)
-- UNION ALL keeps both rows even if the user name equals the movie title.
UNION ALL
(
SELECT m.title AS results
FROM Movie_Rating AS mr
JOIN Movies AS m
ON m.movie_id = mr.movie_id
WHERE mr.created_at >= '2020-02-01'
AND mr.created_at < '2020-03-01'
GROUP BY m.movie_id, m.title
ORDER BY AVG(mr.rating) DESC, m.title
LIMIT 1
);