Advertisement
Guest User

Untitled

a guest
Feb 11th, 2016
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.66 KB | None | 0 0
  1.  
  2. -- Exercises
  3.  
  4. --Q1 Find the titles of all movies directed by Steven Spielberg.
  5. select title from Movie where director = "Steven Spielberg";
  6.  
  7. --Q2 Find all years that have a movie that received a rating of 4 or 5, and sort them in increasing order.
  8. select distinct year from Movie, Rating
  9. where Movie.mID = Rating.mID and
  10. stars > 3 order by year;
  11.  
  12. --Q3 Find the titles of all movies that have no ratings.
  13. select distinct title from Movie, Rating
  14. where Movie.mID not in (select mID from Rating);
  15.  
  16. --Q4 Find the names of all reviewers who have ratings with a NULL value for the date.
  17. select distinct name from Reviewer, Rating
  18. where Reviewer.rID = Rating.rID and
  19. Rating.ratingDate is NULL;
  20.  
  21. --Q5 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.
  22. select distinct name as 'reviewer name', title as 'movie title', stars, ratingDate from
  23. Movie, Rating, Reviewer
  24. where
  25. Movie.mID = Rating.mID and Rating.rID = Reviewer.rID
  26. order by name, title, stars;
  27.  
  28. --Q6 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.
  29. select name, title from Reviewer, Movie, Rating, Rating r2
  30. where Rating.mID=Movie.mID and Reviewer.rID=Rating.rID
  31. and Rating.rID = r2.rID and r2.mID = Movie.mID
  32. and Rating.stars < r2.stars and Rating.ratingDate < r2.ratingDate;
  33.  
  34. --Q7 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.
  35. select title, max(stars) from Movie, Rating
  36. where Movie.mID = Rating.mID
  37. group by Rating.mID
  38. order by title;
  39.  
  40. --Q8 For each movie, return the title and the 'rating spread', that is, the difference between highest and lowest ratings given to that movie. Sort by rating spread from highest to lowest, then by movie title.
  41. select title, max(stars) - min(stars) as ratingSpread from Movie, Rating
  42. where Movie.mID = Rating.mID
  43. group by title
  44. order by ratingspread desc, title;
  45.  
  46. --Q9 Find the difference between the average rating of movies released before 1980 and the average rating of movies released after 1980.
  47. select smaller1980.second - bigger1980.first
  48. from (select avg( averages1) as first
  49. from (select title, avg(stars) as averages1 from Movie, Rating where Movie.mID=Rating.mID and year > 1980 group by title)) as bigger1980,
  50. (select avg( averages2) as second from (select title, avg(stars) as averages2 from Movie, Rating
  51. where Movie.mID=Rating.mID and year < 1980 group by title)) as smaller1980;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement