Stanford--Introduce to Databases(1)

For each movie that has at least one rating, find the highest number of stars that movie received. Return the movie title and number of stars. Sort by movie title.
SQL Movie-Rating Query Exercises (core set)

 

You've started a new movie-rating website, and you've been collecting data on reviewers' ratings of various movies. There's not much data yet, but you can still try out some interesting queries. Here's the schema:

  Movie ( mID, title, year, director )
  English: There is a movie with ID number mID, atitle, a releaseyear, and adirector.

  Reviewer ( rID, name )
  English: The reviewer with ID number rID  has a certainname.

  Rating ( rID, mID, stars, ratingDate )
  English: The reviewer rID  gave the moviemID a number ofstars rating (1-5) on a certainratingDate.

  Learn more.Discuss

Movie
mID title year director
101 Gone with the Wind 1939 Victor Fleming
102 Star Wars 1977 George Lucas
103 The Sound of Music 1965 Robert Wise
104 E.T. 1982 Steven Spielberg
105 Titanic 1997 James Cameron
106 Snow White 1937 <null>
107 Avatar 2009 James Cameron
108 Raiders of the Lost Ark 1981 Steven Spielberg

Reviewer
rID name
201 Sarah Martinez
202 Daniel Lewis
203 Brittany Harris
204 Mike Anderson
205 Chris Jackson
206 Elizabeth Thomas
207 James Cameron
208 Ashley White

Rating
rID mID stars ratingDate
201 101 2 2011-01-22
201 101 4 2011-01-27
202 106 4 <null>
203 103 2 2011-01-20
203 108 4 2011-01-12
203 108 2 2011-01-30
204 101 3 2011-01-09
205 103 3 2011-01-27
205 104 2 2011-01-22
205 108 4 <null>
206 107 3 2011-01-15
206 106 5 2011-01-19
207 107 5 2011-01-20
208 104 3 2011-01-02

 

Question 1

Find the titles of all movies directed by Steven Spielberg.

select title from Movie where director='Steven Spielberg'

Question 2

Find all years that have a movie that received a rating of 4 or 5, and sort them in increasing order.

select year from Movie where mID in (select mID from Rating where stars in (4, 5)) order by year;

 

Question 3

Find the titles of all movies that have no ratings.

select title from Movie where mID not in (select mID from Rating)

 

Question 4

Some reviewers didn't provide a date with their rating. Find the names of all reviewers who have ratings
 
with a NULL value for the date.

 select distinct name from Reviewer natural join Rating where ratingDate is null

 

Question 5

Write a query to return the ratings data in a more readable format: reviewer name, movie title, stars, and
 
ratingDate. Also, sort the data, first by reviewer name, then by movie title, and lastly by number of stars.
select name, title, stars, ratingDate from (Movie natural join Reviewer) natural join  Rating order by name, title, stars
 

Question 6

For all cases where the same reviewer rated the same movie twice and gave it a higher rating the second
 
time, return the reviewer's name and the title of the movie.
select name, title from Reviewer natural join Movie where rID in (select rID from ((select * from Rating R1 where rID in (select rID from Rating R2 group by rID, mID having count(stars)=2) and exists (select * from Rating R3 where R3.rID=R1.rID and R3.mID=R1.mID and R3.ratingDate>R1.ratingDate and R3.stars>R1.stars)) as Rs)) and mID in (select mID from ((select * from Rating R1 where rID in (select rID from Rating R2 group by rID, mID having count(stars)=2) and exists (select * from Rating R3 where R3.rID=R1.rID and R3.mID=R1.mID and R3.ratingDate>R1.ratingDate and R3.stars>R1.stars)) as Rs))
 

Question 7

For each movie that has at least one rating, find the highest number of stars that movie received. Return
 
the movie title and number of stars. Sort by movie title.

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章