SQL练习3-SQLZoo第7章 More JOIN

621 阅读3分钟

1. Introduction

地址sqlzoo.net/wiki/More_J…

表结构
movie (id, title, yr, director, budget, gross)
actor (id, name)
casting (movieid, actorid, ord)

错题 知识点
10 三表联查

2. Q & A

  1. List the films where the yr is 1962 [Show id, title]
SELECT id,title
    FROM movie
    WHERE yr=1962
  1. Give year of 'Citizen Kane'.
SELECT yr 
    FROM movie
    WHERE title = 'Citizen Kane'
  1. 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.
SELECT id, title, yr
    FROM movie
    WHERE title like '%Star Trek%'
    ORDER BY yr
  1. What id number does the actor 'Glenn Close' have?
SELECT id 
    FROM actor
    WHERE name = 'Glenn Close'
  1. What is the id of the film 'Casablanca'
SELECT id 
    FROM movie
    WHERE title = 'Casablanca'
  1. Obtain the cast list for 'Casablanca'. Use movieid=11768, (or whatever value you got from the previous question)
SELECT name FROM actor
    JOIN casting ON id=actorid
    WHERE movieid=11768
  1. Obtain the cast list for the film 'Alien'
SELECT name FROM actor
    JOIN casting ON id=actorid
    WHERE movieid=(
        SELECT id FROM movie 
            WHERE title='Alien'
    )
  1. List the films in which 'Harrison Ford' has appeared
SELECT title FROM movie
    JOIN casting ON id=movieid
    WHERE actorid=(
        SELECT id FROM actor 
            WHERE name='Harrison Ford'
    )
  1. 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]
SELECT title FROM movie
    JOIN casting ON id=movieid
    WHERE actorid=(
        SELECT id FROM actor 
            WHERE name='Harrison Ford')
    and ord!=1
  1. List the films together with the leading star for all 1962 films.
  • 错题
SELECT movie.title,actor.name FROM 
    movie JOIN casting ON movie.id=casting.movieid
          JOIN actor ON actor.id=casting.actorid
    WHERE yr=1962 AND casting.ord=1
  • 困难:三表联查的逻辑没想通,在后续训练中一定要注意理清查询逻辑.
  1. 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.
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) > 2
  1. List the film title and the leading actor for all of the films 'Julie Andrews' played in.
  • 思路:castingactor两表联查出'Julie Andrews'参演过的movieid,然后再联合表movie查出电影名称和主演.
SELECT movie.title,actor.name FROM
    movie JOIN casting ON movie.id=movieid
          JOIN actor   ON actorid=actor.id
    WHERE movie.id in 
        (SELECT movieid FROM
            casting JOIN actor ON actor.id = actorid
         WHERE actor.name='Julie Andrews')
    and casting.ord=1
  1. Obtain a list, in alphabetical order, of actors who've had at least 15 starring roles.
  • 思路:先在表casting中查出主演过至少15部电影的actorid(即内层子查询),然后根据演员id在表actor中查演员姓名。也可不用子查询,得到actorid后联查表actor也可得到演员姓名。
SELECT name FROM actor 
    WHERE id IN(
        SELECT actorid FROM casting
            WHERE ord=1
            GROUP BY actorid
            HAVING count(movieid)>=15
    )
ORDER BY name
  1. List the films released in the year 1978 ordered by the number of actors in the cast, then by title.
SELECT title,count(actorid) FROM movie 
    JOIN casting ON movie.id=casting.movieid
    WHERE yr=1978
    GROUP BY movieid
    ORDER BY count(actorid) desc,title
  1. List all the people who have worked with 'Art Garfunkel'.
  • 思路:
    1)在表actor中查出名为'Art Garfunkel'的演员id
    2)在表casting中根据actorid查出这样演员参演的电影movieid
    3)在表casting中根据movieid查出参演这些电影的演员id
    4)回到表actor根据演员id查出演员姓名.
SELECT name FROM actor 
    WHERE id IN(
        SELECT actorid FROM casting
            WHERE movieid IN(
                SELECT movieid FROM casting
                    WHERE actorid IN(
                        SELECT id FROM actor
                            WHERE name='Art Garfunkel'
                    )
            )
    AND name<>'Art Garfunkel'
    )
  • 但是这个套娃太绕了,换成联查.
SELECT name FROM actor
    JOIN casting ON actor.id=casting.actorid
    WHERE movieid IN (
		SELECT movieid FROM 
			casting JOIN actor on (actor.id = actorid)
			WHERE name = 'Art Garfunkel'
	) AND name <> 'Art Garfunkel'