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.
Table: Users
+---------------+---------+
| Column Name | Type |
+---------------+---------+
| user_id | int |
| name | varchar |
+---------------+---------+
user_id is the primary key (column with unique values) 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 (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 review date.
Write a solution 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 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.
# 풀이 쿼리
(select u.name as results
from MovieRating mr
left join Users u
on mr.user_id = u.user_id
group by mr.user_id
order by count(mr.user_id) desc, name
limit 1)
union all
(select m.title as results
from MovieRating mr
left join Movies m
on mr.movie_id = m.movie_id
where created_at between '2020-02-01' and '2020-02-29'
group by m.title
order by avg(mr.rating) desc, m.title
limit 1)
- 유저 이름과 영화 이름은 각각 구했는데, 이 둘을 어떻게 한번에 출력할 것인가를 좀 고민했다.
이 방법을 써야 하나 저 방법을 써야 하나...
Union all이 없었으면 아마 한참 고생했을 것이다.
이 문제의 핵심은 유저 이름과 영화 이름을 구하는 조건을 각각 잘 구현해서 출력하고, 이 둘을 union all로 한 번에 출력하는 것이었다.
'코딩테스트 > SQL - Leetcode' 카테고리의 다른 글
1907. Count Salary Categories (0) | 2024.02.01 |
---|---|
1393. Capital Gain/Loss (0) | 2024.01.31 |
1204. Last Person to Fit in the Bus (0) | 2024.01.29 |
1193. Monthly Transactions I (0) | 2024.01.25 |
1174. Immediate Food Delivery II (0) | 2024.01.17 |