|
| 1 | +--1341. Movie Rating |
| 2 | +-- |
| 3 | +--Table: Movies |
| 4 | +-- |
| 5 | +--+---------------+---------+ |
| 6 | +--| Column Name | Type | |
| 7 | +--+---------------+---------+ |
| 8 | +--| movie_id | int | |
| 9 | +--| title | varchar | |
| 10 | +--+---------------+---------+ |
| 11 | +--movie_id is the primary key for this table. |
| 12 | +--title is the name of the movie. |
| 13 | +--Table: Users |
| 14 | +-- |
| 15 | +--+---------------+---------+ |
| 16 | +--| Column Name | Type | |
| 17 | +--+---------------+---------+ |
| 18 | +--| user_id | int | |
| 19 | +--| name | varchar | |
| 20 | +--+---------------+---------+ |
| 21 | +--user_id is the primary key for this table. |
| 22 | +--Table: Movie_Rating |
| 23 | +-- |
| 24 | +--+---------------+---------+ |
| 25 | +--| Column Name | Type | |
| 26 | +--+---------------+---------+ |
| 27 | +--| movie_id | int | |
| 28 | +--| user_id | int | |
| 29 | +--| rating | int | |
| 30 | +--| created_at | date | |
| 31 | +--+---------------+---------+ |
| 32 | +--(movie_id, user_id) is the primary key for this table. |
| 33 | +--This table contains the rating of a movie by a user in their review. |
| 34 | +--created_at is the user's review date. |
| 35 | +-- |
| 36 | +-- |
| 37 | +--Write the following SQL query: |
| 38 | +-- |
| 39 | +--Find the name of the user who has rated the greatest number of the movies. |
| 40 | +--In case of a tie, return lexicographically smaller user name. |
| 41 | +-- |
| 42 | +--Find the movie name with the highest average rating as of Feb 2020. |
| 43 | +--In case of a tie, return lexicographically smaller movie name.. |
| 44 | +-- |
| 45 | +--Query is returned in 2 rows, the query result format is in the folowing example: |
| 46 | +-- |
| 47 | +--Movie table: |
| 48 | +--+-------------+--------------+ |
| 49 | +--| movie_id | title | |
| 50 | +--+-------------+--------------+ |
| 51 | +--| 1 | Avengers | |
| 52 | +--| 2 | Frozen 2 | |
| 53 | +--| 3 | Joker | |
| 54 | +--+-------------+--------------+ |
| 55 | +-- |
| 56 | +--Users table: |
| 57 | +--+-------------+--------------+ |
| 58 | +--| user_id | name | |
| 59 | +--+-------------+--------------+ |
| 60 | +--| 1 | Daniel | |
| 61 | +--| 2 | Monica | |
| 62 | +--| 3 | Maria | |
| 63 | +--| 4 | James | |
| 64 | +--+-------------+--------------+ |
| 65 | +-- |
| 66 | +--Movie_Rating table: |
| 67 | +--+-------------+--------------+--------------+-------------+ |
| 68 | +--| movie_id | user_id | rating | created_at | |
| 69 | +--+-------------+--------------+--------------+-------------+ |
| 70 | +--| 1 | 1 | 3 | 2020-01-12 | |
| 71 | +--| 1 | 2 | 4 | 2020-02-11 | |
| 72 | +--| 1 | 3 | 2 | 2020-02-12 | |
| 73 | +--| 1 | 4 | 1 | 2020-01-01 | |
| 74 | +--| 2 | 1 | 5 | 2020-02-17 | |
| 75 | +--| 2 | 2 | 2 | 2020-02-01 | |
| 76 | +--| 2 | 3 | 2 | 2020-03-01 | |
| 77 | +--| 3 | 1 | 3 | 2020-02-22 | |
| 78 | +--| 3 | 2 | 4 | 2020-02-25 | |
| 79 | +--+-------------+--------------+--------------+-------------+ |
| 80 | +-- |
| 81 | +--Result table: |
| 82 | +--+--------------+ |
| 83 | +--| results | |
| 84 | +--+--------------+ |
| 85 | +--| Daniel | |
| 86 | +--| Frozen 2 | |
| 87 | +--+--------------+ |
| 88 | +-- |
| 89 | +--Daniel and Maria have rated 3 movies ("Avengers", "Frozen 2" and "Joker") but Daniel is smaller lexicographically. |
| 90 | +--Frozen 2 and Joker have a rating average of 3.5 in February but Frozen 2 is smaller lexicographically. |
| 91 | + |
| 92 | +--# Write your MySQL query statement below |
| 93 | +SELECT user_name AS results FROM |
| 94 | +( |
| 95 | +SELECT a.name AS user_name, COUNT(*) AS counts FROM Movie_Rating AS b |
| 96 | + JOIN Users AS a |
| 97 | + on a.user_id = b.user_id |
| 98 | + GROUP BY b.user_id |
| 99 | + ORDER BY counts DESC, user_name ASC LIMIT 1 |
| 100 | +) first_query |
| 101 | +UNION |
| 102 | +SELECT movie_name AS results FROM |
| 103 | +( |
| 104 | +SELECT c.title AS movie_name, AVG(d.rating) AS rate FROM Movie_Rating AS d |
| 105 | + JOIN Movies AS c |
| 106 | + on c.movie_id = d.movie_id |
| 107 | + WHERE substr(d.created_at, 1, 7) = '2020-02' |
| 108 | + GROUP BY d.movie_id |
| 109 | + ORDER BY rate DESC, movie_name ASC LIMIT 1 |
| 110 | +) second_query; |
0 commit comments