SQL query from www.db-class.com

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
+3
2

-, gretest-n-per-group. tag:)

, () , , , (). , , , .

, , (, ... ). .

:

, , , , .

select distinct m1.director, m1.title, r1.stars from movie m1
join rating r1 on m1.mID = r1.mID
left join (
    select m2.director, r2.stars from movie m2
    join rating r2 on m2.mID = r2.mID
) s on m1.director = s.director and r1.stars < s.stars
where s.stars is null and m1.director is not null
+2

:

  • , ()

  • "", - -

  • "" 2 " " "max ()"

0

All Articles