SQL - 예제로 배우는 SQL 8

산하·2022년 5월 18일
0

SQL

목록 보기
9/9
post-thumbnail

문제 링크 바로가기 👀



👉🏻 More JOIN operations


1. 1962 movies

Q. List the films where the yr is 1962 [Show id, title]

A.

SELECT id, title FROM movie
 WHERE yr=1962

2. When was Citizen Kane released?

Q. Give year of 'Citizen Kane'.

A.

SELECT yr FROM movie
  WHERE title = 'Citizen Kane'

3. Star Trek movies

Q. 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). Order results by year.

A.

SELECT id, title, yr FROM movie 
  WHERE title LIKE 'Star Trek%'
    ORDER BY yr

4. id for actor Glenn Close

Q. What id number does the actor 'Glenn Close' have?

A.

SELECT id FROM actor 
WHERE name = 'Glenn Close'

5. id for Casablanca

Q. What is the id of the film 'Casablanca'

A.

SELECT id FROM movie 
WHERE title = 'Casablanca'

6. Cast list for Casablanca

Q. Obtain the cast list for 'Casablanca'.

Use movieid=11768, (or whatever value you got from the previous question)

A.

SELECT name FROM actor JOIN casting ON (id = actorid)
WHERE movieid = '27

7. Alien cast list

Q. Obtain the cast list for the film 'Alien'

A.

SELECT name FROM actor JOIN casting ON (id = actorid) 
  WHERE movieid = (SELECT id FROM movie WHERE title = 'Alien') 

8. Harrison Ford movies

Q. List the films in which 'Harrison Ford' has appeared

A.

SELECT title FROM movie JOIN casting ON (id = movieid) 
  WHERE actorid = (SELECT id FROM actor WHERE name = 'Harrison Ford')

9. Harrison Ford as a supporting actor

Q. List the films where 'Harrison Ford' has appeared - but not in the starring role. [Note: the ord field of casting gives the position of the actor. If ord=1 then this actor is in the starring role]

A.

SELECT title FROM movie JOIN casting ON (id = movieid)
  WHERE ord !=1
  AND actorid = (SELECT id FROM actor WHERE name = 'Harrison Ford')

10. Lead actors in 1962 movies

Q. List the films together with the leading star for all 1962 films.

A.

SELECT title, name FROM movie 
  JOIN casting ON (movie.id = casting.movieid)
  JOIN actor ON (casting.actorid = actor.id)
    WHERE yr = 1962 
    AND ord = 1

11. Busy years for Rock Hudson

Q. Which were the busiest years for 'Rock Hudson', show the year and the number of movies he made each year for any year in which he made more than 2 movies.

A.

SELECT yr, COUNT(title) FROM movie 
  JOIN casting ON (movie.id=movieid)
  JOIN actor ON (actorid=actor.id)
    WHERE name = 'Rock Hudson'
    GROUP BY yr
    HAVING COUNT(title) > 1

12. Lead actor in Julie Andrews movies

Q. List the film title and the leading actor for all of the films 'Julie Andrews' played in

A.

SELECT title, name FROM movie 
  JOIN casting ON (movie.id = movieid)
  JOIN actor ON (actorid = actor.id)
    WHERE ord = 1
    AND movieid IN 
      (SELECT movieid FROM casting, actor
         WHERE actorid = id
         AND name = 'Julie Andrews')

13. Actors with 15 leading roles

Q. Obtain a list, in alphabetical order, of actors who've had at least 15 starring roles.

A.

SELECT name FROM actor JOIN casting ON (id = actorid)
  WHERE ord = 1
  GROUP BY name
    HAVING 14 < COUNT(name)

14.

Q. List the films released in the year 1978 ordered by the number of actors in the cast, then by title.

A.

SELECT  title,COUNT(actorid)  FROM movie JOIN casting ON (id = movieid)
  WHERE yr = '1978' 
    GROUP BY title
      ORDER BY COUNT(actorid) DESC , title

15.

Q. List all the people who have worked with 'Art Garfunkel'.

A.

SELECT name FROM actor 
  JOIN casting ON (actor.id = actorid)
  JOIN movie ON (movieid = movie.id)
    WHERE movieid IN 
      (SELECT movieid FROM movie,casting,actor 
         WHERE actorid = actor.id
         AND name = 'Art Garfunkel' )
    AND name != 'Art Garfunkel'
      ORDER BY name

profile
반갑습니다 :) 백앤드 개발자 산하입니다!

1개의 댓글

comment-user-thumbnail
2023년 2월 17일

Good

답글 달기