I solve the exercises at www.db-class.com. Although it ends, the questions are still interesting. I came across the last task in extras and I can’t understand the solution.
The SQL schema is as follows:
create table Movie(mID int, title text, year int, director text);
create table Reviewer(rID int, name text);
create table Rating(rID int, mID int, stars int, ratingDate date);
The entire database can be obtained here.
The question arises:
Q12 For each director, return the name of the director along with the name of the film (s) they directed, which received the highest rating among all their films and the value of this rating. Ignore movies with NULL mode.
To talk more about the problems here:
One request
select m.mid, m.title, max(r.stars) as stars
from rating r natural join movie m
where m.director is NOT NULL group by m.mid
returns identifiers of the top rated films:
101 Gone with the Wind 4
103 The Sound of Music 3
104 E.T. 3
107 Avatar 5
108 Raiders of the Lost Ark 4
Another request
select m.director, max(r.stars) as stars
from rating r natural join movie m
where m.director is NOT NULL group by m.director
returns the names of directors with the highest rated films for them:
James Cameron 5
Robert Wise 3
Steven Spielberg 4
Victor Fleming 4
:
James Cameron Avatar 5
Robert Wise The Sound of Music 3
Steven Spielberg Raiders of the Lost Ark 4
Victor Fleming Gone with the Wind 4