-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathextras
114 lines (91 loc) · 3.43 KB
/
extras
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
-- 1. Find the names of all reviewers who rated Gone with the Wind.
select distinct name
from Movie
inner join Rating using(mId)
inner join Reviewer using(rId)
where title = "Gone with the Wind";
-- 2. For any rating where the reviewer is the same as the director of the movie, return the reviewer name, movie title, and number of stars.
select name, title, stars
from Movie
inner join Rating using(mId)
inner join Reviewer using(rId)
where director = name;
-- 3. Return all reviewer names and movie names together in a single list, alphabetized. (Sorting by the first name of the reviewer and first word in the title is fine; no need for special processing on last names or removing "The")
select title
from Movie
union
select name
from Reviewer
order by name, title;
-- 4. Find the titles of all movies not reviewed by Chris Jackson.
select title
from Movie
where mId not in (
select mId
from Rating
inner join Reviewer using(rId)
where name = "Chris Jackson"
);
-- 5. For all pairs of reviewers such that both reviewers gave a rating to the same movie, return the names of both reviewers.
select distinct Re1.name, Re2.name
from Rating R1, Rating R2, Reviewer Re1, Reviewer Re2
where R1.mID = R2.mID
and R1.rID = Re1.rID
and R2.rID = Re2.rID
and Re1.name < Re2.name
order by Re1.name, Re2.name;
-- 6. For each rating that is the lowest (fewest stars) currently in the database, return the reviewer name, movie title, and number of stars.
select name, title, stars
from Movie
inner join Rating using(mId)
inner join Reviewer using(rId)
where stars = (select MIN(stars) from Rating);
-- 7. List movie titles and average ratings, from highest-rated to lowest-rated. If two or more movies have the same average rating, list them in alphabetical order.
select title, AVG(stars) as average
from Movie
inner join Rating using(mId)
group by mId
order by average desc, title;
-- 8. Find the names of all reviewers who have contributed three or more ratings.
select name
from Reviewer
where (select count(*) from Rating where Rating.rId = Reviewer.rId) >= 3;
-- 9. Some directors directed more than one movie. For all such directors, return the titles of all movies directed by them, along with the director name. Sort by director name, then movie title.
select title, director
from Movie M1
where (select count(*) from Movie M2 where M1.director = M2.director) > 1
order by director, title;
-- 10. Find the movie(s) with the highest average rating. Return the movie title(s) and average rating.
select title, AVG(stars) AS average
from Movie
inner join Rating using(mId)
group by mId
having average = (
select MAX(average_stars)
from (
select title, AVG(stars) as average_stars
from Movie
inner join Rating using(mId)
group by mId
)
);
-- 11. Find the movie(s) with the lowest average rating. Return the movie title(s) and average rating.
select title, AVG(stars) AS average
from Movie
inner join Rating using(mId)
group by mId
having average = (
select MIN(average_stars)
from (
select title, AVG(stars) as average_stars
from Movie
inner join Rating using(mId)
group by mId
)
);
-- 12. For each director, return the director's name together with the title(s) of the movie(s) they directed that received the highest rating among all of their movies, and the value of that rating. Ignore movies whose director is NULL.
select director, title, MAX(stars)
from Movie
inner join Rating using(mId)
where director is not NULL
group by director;