-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy path1341. Movie Rating.sql
More file actions
73 lines (61 loc) · 1.91 KB
/
1341. Movie Rating.sql
File metadata and controls
73 lines (61 loc) · 1.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
/*
Question 1341. Movie Rating
Link: https://leetcode.com/problems/movie-rating/description/
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.
The column 'name' has unique values.
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.
*/
SELECT name AS results
FROM (
SELECT u.name
FROM MovieRating AS m
LEFT JOIN
Users AS u
ON m.user_id = u.user_id
GROUP BY u.name
ORDER BY COUNT(m.user_id) DESC, u.name ASC
LIMIT 1
)
UNION ALL
SELECT title as results --noqa: CP01
FROM (
SELECT m.title
FROM MovieRating AS mr
LEFT JOIN
Movies AS m
ON mr.movie_id = m.movie_id
WHERE mr.created_at BETWEEN '2020-02-01' AND '2020-02-29'
GROUP BY m.title
ORDER BY AVG(mr.rating) DESC, m.title ASC
LIMIT 1
)