Guest User

Untitled

a guest
Feb 25th, 2018
163
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.28 KB | None | 0 0
  1. -- 1. Find the titles of all movies directed by Steven Spielberg.
  2.  
  3. SELECT title
  4. FROM Movie
  5. WHERE director = 'Steven Spielberg';
  6.  
  7.  
  8. -- 2. Find all years that have a movie that received a rating of 4 or 5, and sort them in increasing order.
  9.  
  10. SELECT DISTINCT year
  11. FROM Movie, Rating
  12. WHERE Movie.mId = Rating.mId AND stars IN (4, 5)
  13. ORDER BY year;
  14.  
  15. SELECT DISTINCT year
  16. FROM Movie
  17. INNER JOIN Rating ON Movie.mId = Rating.mId
  18. WHERE stars IN (4, 5)
  19. ORDER BY year;
  20.  
  21. SELECT DISTINCT year
  22. FROM Movie
  23. INNER JOIN Rating USING(mId)
  24. WHERE stars IN (4, 5)
  25. ORDER BY year;
  26.  
  27. SELECT DISTINCT year
  28. FROM Movie NATURAL JOIN Rating
  29. WHERE stars IN (4, 5)
  30. ORDER BY year;
  31.  
  32.  
  33. -- 3. Find the titles of all movies that have no ratings.
  34.  
  35. SELECT title
  36. FROM Movie
  37. WHERE mId NOT IN (SELECT mID FROM Rating);
  38.  
  39.  
  40. -- 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.
  41.  
  42. SELECT name
  43. FROM Reviewer
  44. INNER JOIN Rating USING(rId)
  45. WHERE ratingDate IS NULL;
  46.  
  47. # BY MYSELF
  48. SELECT title FROM movie
  49. LEFT join Rating
  50. on movie.mID = Rating.mID
  51. where stars is NULL
  52.  
  53. -- 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.
  54.  
  55. SELECT name, title, stars, ratingDate
  56. FROM Movie, Rating, Reviewer
  57. WHERE Movie.mId = Rating.mId AND Reviewer.rId = Rating.rId
  58. ORDER BY name, title, stars;
  59.  
  60. SELECT name, title, stars, ratingDate
  61. FROM Movie
  62. INNER JOIN Rating ON Movie.mId = Rating.mId
  63. INNER JOIN Reviewer ON Reviewer.rId = Rating.rId
  64. ORDER BY name, title, stars;
  65.  
  66. SELECT name, title, stars, ratingDate
  67. FROM Movie
  68. INNER JOIN Rating USING(mId)
  69. INNER JOIN Reviewer USING(rId)
  70. ORDER BY name, title, stars;
  71.  
  72. SELECT name, title, stars, ratingDate
  73. FROM Movie NATURAL JOIN Rating NATURAL JOIN Reviewer
  74. ORDER BY name, title, stars;
  75.  
  76. #### BY MYSELF
  77. SELECT name, title, stars, ratingdate from (
  78. SELECT mID, stars, ratingdate,name from Rating
  79. join Reviewer
  80. on Rating.rID = reviewer.rID) as A
  81. join Movie
  82. on Movie.mID = A.mID
  83. order by name, title, stars
  84. #### BY MYSELF
  85.  
  86. -- 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.
  87. # HOW TO ENSURE THAT THE MOVIE IS REVIEWED TWICE NOT THREE TIMES OR SO?
  88. SELECT name, title
  89. FROM Movie
  90. INNER JOIN Rating R1 USING(mId)
  91. INNER JOIN Rating R2 USING(rId)
  92. INNER JOIN Reviewer USING(rId)
  93. WHERE R1.mId = R2.mId AND R1.ratingDate < R2.ratingDate AND R1.stars < R2.stars;
  94.  
  95. SELECT name, title
  96. FROM Movie
  97. INNER JOIN Rating R1 USING(mId)
  98. INNER JOIN Rating R2 USING(rId, mId)
  99. INNER JOIN Reviewer USING(rId)
  100. WHERE R1.ratingDate < R2.ratingDate AND R1.stars < R2.stars;
  101.  
  102.  
  103. -- 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.
  104.  
  105. SELECT title, MAX(stars)
  106. FROM Movie
  107. INNER JOIN Rating USING(mId)
  108. GROUP BY mId
  109. ORDER BY title;
  110.  
  111.  
  112. -- 8. 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.
  113. # My CODING
  114. SELECT title, max(Rating.stars)-min(Rating.stars) as rating_spread
  115. FROM movie join rating using (mID)
  116. group by Rating.mID
  117. order by rating_spread DESC, title
  118.  
  119. -- 9. Find the difference between the average rating of movies released before 1980 and the average rating of movies released after 1980. (Make sure to calculate the average rating for each movie, then the average of those averages for movies before 1980 and movies after. Don't just calculate the overall average rating before and after 1980.)
  120.  
  121. # MY CODING
  122.  
  123. SELECT max(average)-min(average) FROM(
  124. SELECT avg(avg_stars) as average FROM
  125. (SELECT title,
  126. CASE
  127. when year< 1980 then "before"
  128. when year>=1980 then "after"
  129. end as before_or,
  130. AVG(stars) as avg_stars
  131. FROM movie
  132. join rating
  133. using (mID)
  134. group by mID)
  135. group by before_or)
  136.  
  137.  
  138. SELECT AVG(Before1980.avg) - AVG(After1980.avg)
  139. FROM (
  140. SELECT AVG(stars) AS avg
  141. FROM Movie
  142. INNER JOIN Rating USING(mId)
  143. WHERE year < 1980
  144. GROUP BY mId
  145. ) AS Before1980, (
  146. SELECT AVG(stars) AS avg
  147. FROM Movie
  148. INNER JOIN Rating USING(mId)
  149. WHERE year > 1980
  150. GROUP BY mId
  151. ) AS After1980;
Add Comment
Please, Sign In to add comment