Advertisement
Astekk

sqlzoo [MoreJoins]

Jun 18th, 2018
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
SQL 3.71 KB | None | 0 0
  1. Problem 1 : List the films WHERE the yr IS 1962 [SHOW id, title]
  2.  
  3. SELECT id, title FROM movie WHERE yr = 1962
  4.  
  5.  
  6.  
  7. Problem 2 : Give YEAR OF ‘Citizen Kane’.
  8.  
  9.  
  10. SELECT yr
  11. FROM movie
  12. WHERE title='Citizen Kane'
  13.  
  14.  
  15. Problem 3 : List ALL OF the Star Trek movies, include the id title AND yr. (ALL OF these movies include the words Star Trek IN the title.)
  16.  
  17. SELECT id,title, yr
  18. FROM movie
  19. WHERE title LIKE '%Star%Trek%'
  20. ORDER BY yr ASC
  21.  
  22.  
  23. Problem 4 : What are the titles OF the films WITH id 11768, 11955, 21191
  24.  
  25.  
  26. SELECT title
  27. FROM movie
  28. WHERE id IN (11768, 11955, 21191)
  29.  
  30.  
  31. Problem 5 : What id NUMBER does the actor ‘Glenn Close’ have?
  32.  
  33. SELECT id
  34. FROM actor
  35. WHERE name='Glenn Close'
  36.  
  37.  
  38.  
  39. Problem 6 : What IS the id OF the film ‘Casablanca’
  40.  
  41. SELECT id
  42. FROM movie
  43. WHERE title = 'Casablanca'
  44.  
  45.  
  46. REAL JOIN questions BEGIN :
  47. Problem 7 : Obtain the CAST list FOR ‘Casablanca’. USE the id VALUE that you obtained IN the previous question.
  48.  
  49.  
  50. SELECT a.Name
  51. FROM
  52. casting c
  53. JOIN actor a ON
  54. c.actorid = a.id
  55. WHERE movieid = 27
  56.  
  57.  
  58.  
  59. Problem 8 : Obtain the CAST list FOR the film ‘Alien’
  60.  
  61. SELECT a.Name
  62. FROM
  63.  casting c
  64. JOIN actor a ON
  65. c.actorid = a.id
  66. WHERE movieid IN
  67. (SELECT id FROM movie WHERE title = 'Alien');
  68.  
  69.  
  70.  
  71. Problem 9 : List the films IN which ‘Harrison Ford’ has appeared
  72.  
  73. SELECT m.title
  74. FROM
  75.  movie m
  76. JOIN casting c ON
  77.  m.id = c.movieid
  78. WHERE c.actorid IN
  79. (SELECT id FROM actor WHERE name='Harrison Ford');
  80.  
  81.  
  82.  
  83.  
  84. Problem 10 : List the films WHERE ‘Harrison Ford’ has appeared - but NOT IN the star ROLE.
  85.  
  86. SELECT m.title
  87. FROM
  88.  movie m
  89. JOIN casting c ON
  90.  m.id = c.movieid AND
  91.  c.ord <> 1
  92. WHERE c.actorid IN
  93. (SELECT id FROM actor WHERE name='Harrison Ford')
  94.  
  95.  
  96.  
  97.  
  98. Problem 11: List the films together WITH the LEADING star FOR ALL 1962 films.
  99.  
  100. SELECT m.title,a.name
  101.  FROM casting c JOIN movie m ON
  102.    m.id = c.movieid
  103.  JOIN actor a ON
  104.    a.id = c.actorid  AND
  105.    c.ord = 1
  106. WHERE m.yr = 1962
  107.  
  108.  
  109.  
  110.  
  111. Problem 12 : Which were the busiest years FOR ‘John Travolta’. SHOW the NUMBER OF movies he made FOR each YEAR.
  112.  
  113. SELECT yr,COUNT(title) FROM
  114.   movie JOIN casting ON movie.id=movieid
  115.          JOIN actor   ON actorid=actor.id
  116. WHERE name='John Travolta'
  117. GROUP BY yr  
  118. HAVING COUNT(title)=(SELECT MAX(c) FROM
  119. (SELECT yr,COUNT(title) AS c FROM
  120.    movie JOIN casting ON movie.id=movieid
  121.          JOIN actor   ON actorid=actor.id
  122.  WHERE name='John Travolta'
  123.  GROUP BY yr)  AS t
  124. )
  125.  
  126.  
  127.  
  128. Problem 13 : List the film title AND the LEADING actor FOR ALL OF ‘Julie Andrews’ films.
  129.  
  130. SELECT m.title, a.name
  131.   FROM casting c JOIN movie m ON
  132.    m.id = c.movieid AND
  133.    c.ord = 1
  134.  JOIN actor a ON
  135.    a.id = c.actorid
  136. WHERE m.id IN (
  137.  SELECT m.id
  138.   FROM casting c JOIN movie m ON
  139.     m.id = c.movieid
  140.   JOIN actor a ON
  141.     a.id = c.actorid
  142.  WHERE a.name = 'Julie Andrews'
  143. )
  144.  
  145.  
  146.  
  147. Problem 14 : Obtain a list OF actors IN who have had at least 30 starring roles.
  148.  
  149. SELECT a.name
  150.   FROM casting c JOIN movie m ON
  151.     m.id = c.movieid
  152.   JOIN actor a ON
  153.     a.id = c.actorid AND
  154.     c.ord = 1
  155. GROUP BY a.name
  156. HAVING COUNT(m.id) >= 30
  157.  
  158.  
  159.  
  160.  
  161. Problem 15 : List the 1978 films BY ORDER OF CAST list SIZE.
  162.  
  163. SELECT m.title, COUNT(c.actorid)
  164.   FROM casting c JOIN movie m ON
  165.     m.id = c.movieid
  166.   WHERE m.yr = 1978
  167. GROUP BY m.title
  168. ORDER BY COUNT(c.actorid) DESC
  169.  
  170.  
  171.  
  172.  
  173. Problem 16 : List ALL the people who have worked WITH ‘Art Garfunkel’.
  174.  
  175. SELECT a.name
  176.  FROM casting c JOIN actor a ON
  177.   a.id = c.actorid
  178.  WHERE
  179.   a.name <> 'Art Garfunkel' AND
  180.   c.movieid
  181.  IN (
  182.   SELECT m.id
  183.      FROM casting c JOIN movie m ON
  184.        m.id = c.movieid
  185.      JOIN actor a ON
  186.        c.actorid = a.id
  187.      WHERE a.name = 'ART Garfunkel'
  188. )
  189. ORDER BY a.name
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement