본문 바로가기
Issue Note

Movie rating - SQL

by jhleeatl 2024. 6. 12.

 

Question Movie Rating.

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's 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.

 

 

I created a query to find the required values and match the expected output to solve this problem.

 

with user_rank as (select name
                    from MovieRating m
                    left join users u
                    on m.user_id = u.user_id
                    group by name
                    order by count(*) desc, name asc
                    limit 1),
movie_title as (select title
                from MovieRating mr
                left join Movies mv 
                on mr.movie_id = mv.movie_id
                where month(created_at) = '02' and year(created_at) = 2020
                group by title
                order by avg(rating) desc, title asc
                limit 1)

 

 

However, the issue was how to combine these values into a single column. After some research, I found that it is possible using UNION ALL, but I couldn't recall how to use it in this case as the column names were different.

 

After considering various approaches and conducting some research, I found it to be simpler than I initially thought. It was just a matter of making the column names the same. By renaming the columns as shown below and using UNION ALL, I was able to solve the problem.

 

select user_rank.name as results
from user_rank
union all
select movie_title.title as results
from movie_title

 

'Issue Note' 카테고리의 다른 글

Group sold products by the date - SQL  (0) 2024.06.27
Delete Duplicate Emails [SQL]  (0) 2024.06.25
Join And function  (0) 2024.04.19
1934. Confirmation Rate  (0) 2024.04.12
The count of divisors and addition  (0) 2024.04.09