SQL/LEETCODE
[1341#_MEDIUM] Movie Rating
소금깨
2022. 8. 5. 01:28
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: MovieRating
+---------------+---------+
| 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 an SQL query to:
- Find the name of the user who has rated the greatest number of movies. In case of a tie, return the lexicographically smaller user name.
- Find the movie name with the highest average rating in February 2020. In case of a tie, return the lexicographically smaller movie name.
The query result format is in the following example.
Example 1:
Input:
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 |
+-------------+--------------+
MovieRating 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 |
+-------------+--------------+--------------+-------------+
Output:
+--------------+
| results |
+--------------+
| Daniel |
| Frozen 2 |
+--------------+
Explanation:
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.
문제 조건
가장 많은 영화를 평가한 사용자의 이름을 찾으시오.
평가된 영화의 수가 동일할 경우 사전순으로 더 작은 사용자의 이름을 반환합니다. 2020년 2월 평균 평점이 가장 높은 영화의 이름을 찾으시오.평점이 동일할 경우, 사전순으로 더 작은 영화의 이름을 반환합니다.
문제 풀이 -- 문제 조건을 똑바로 보자 ^^;
with temp_1 as (
select name as results,
max(movie_id),
count(movie_id) as cnt
from MovieRating m
left join Users u on m.user_id = u.user_id
group by 1
order by cnt desc, results
limit 1),
temp_2 as (
select title as results,
max(m.movie_id),
avg(rating) as avg_rate
from MovieRating r
left join Movies m on r.movie_id = m.movie_id
where created_at >= '2020-02-01' and created_at < '2020-03-01'
group by 1
order by avg_rate desc, results
limit 1)
select results
from temp_1
union
select results
from temp_2