|
11 | 11 |
|
12 | 12 | ## [1341. Movie Rating (Medium)](https://leetcode.com/problems/movie-rating "电影评分")
|
13 | 13 |
|
| 14 | +<p>Table: <code>Movies</code></p> |
| 15 | +<pre> |
| 16 | ++---------------+---------+ |
| 17 | +| Column Name | Type | |
| 18 | ++---------------+---------+ |
| 19 | +| movie_id | int | |
| 20 | +| title | varchar | |
| 21 | ++---------------+---------+ |
| 22 | +movie_id is the primary key for this table. |
| 23 | +title is the name of the movie. |
| 24 | +</pre> |
14 | 25 |
|
| 26 | +<p>Table: <code>Users</code></p> |
| 27 | +<pre> |
| 28 | ++---------------+---------+ |
| 29 | +| Column Name | Type | |
| 30 | ++---------------+---------+ |
| 31 | +| user_id | int | |
| 32 | +| name | varchar | |
| 33 | ++---------------+---------+ |
| 34 | +user_id is the primary key for this table. |
| 35 | +</pre> |
| 36 | + |
| 37 | +<p>Table: <code>Movie_Rating</code></p> |
| 38 | +<pre> |
| 39 | ++---------------+---------+ |
| 40 | +| Column Name | Type | |
| 41 | ++---------------+---------+ |
| 42 | +| movie_id | int | |
| 43 | +| user_id | int | |
| 44 | +| rating | int | |
| 45 | +| created_at | date | |
| 46 | ++---------------+---------+ |
| 47 | +(movie_id, user_id) is the primary key for this table. |
| 48 | +This table contains the rating of a movie by a user in their review. |
| 49 | +created_at is the user's review date. |
| 50 | +</pre> |
| 51 | + |
| 52 | +Write the following SQL query: |
| 53 | + |
| 54 | +- Find the name of the user who has rated the greatest number of the movies. |
| 55 | + In case of a tie, return lexicographically smaller user name. |
| 56 | + |
| 57 | +- Find the movie name with the highest average rating as of Feb 2020. |
| 58 | + In case of a tie, return lexicographically smaller movie name.. |
| 59 | + |
| 60 | +Query is returned in 2 rows, the query result format is in the folowing example: |
| 61 | +<pre> |
| 62 | +Movie table: |
| 63 | ++-------------+--------------+ |
| 64 | +| movie_id | title | |
| 65 | ++-------------+--------------+ |
| 66 | +| 1 | Avengers | |
| 67 | +| 2 | Frozen 2 | |
| 68 | +| 3 | Joker | |
| 69 | ++-------------+--------------+ |
| 70 | + |
| 71 | +Users table: |
| 72 | ++-------------+--------------+ |
| 73 | +| user_id | name | |
| 74 | ++-------------+--------------+ |
| 75 | +| 1 | Daniel | |
| 76 | +| 2 | Monica | |
| 77 | +| 3 | Maria | |
| 78 | +| 4 | James | |
| 79 | ++-------------+--------------+ |
| 80 | + |
| 81 | +Movie_Rating table: |
| 82 | ++-------------+--------------+--------------+-------------+ |
| 83 | +| movie_id | user_id | rating | created_at | |
| 84 | ++-------------+--------------+--------------+-------------+ |
| 85 | +| 1 | 1 | 3 | 2020-01-12 | |
| 86 | +| 1 | 2 | 4 | 2020-02-11 | |
| 87 | +| 1 | 3 | 2 | 2020-02-12 | |
| 88 | +| 1 | 4 | 1 | 2020-01-01 | |
| 89 | +| 2 | 1 | 5 | 2020-02-17 | |
| 90 | +| 2 | 2 | 2 | 2020-02-01 | |
| 91 | +| 2 | 3 | 2 | 2020-03-01 | |
| 92 | +| 3 | 1 | 3 | 2020-02-22 | |
| 93 | +| 3 | 2 | 4 | 2020-02-25 | |
| 94 | ++-------------+--------------+--------------+-------------+ |
| 95 | + |
| 96 | +Result table: |
| 97 | ++--------------+ |
| 98 | +| results | |
| 99 | ++--------------+ |
| 100 | +| Daniel | |
| 101 | +| Frozen 2 | |
| 102 | ++--------------+ |
| 103 | + |
| 104 | +Daniel and Maria have rated 3 movies ("Avengers", "Frozen 2" and "Joker") but Daniel is smaller lexicographically. |
| 105 | +Frozen 2 and Joker have a rating average of 3.5 in February but Frozen 2 is smaller lexicographically. |
| 106 | +</pre> |
0 commit comments