sql练习:More JOIN operations - SQLZoo
三个表的关系
movie: (id, title, yr, director, budget, gross)
actor: (id, name)
casting: (movieid, actorid, ord)
movie表:
id | title | yr | director | budget | gross |
---|---|---|---|---|---|
10003 | "Crocodile" Dundee II | 1988 | 38 | 15800000 | 239606210 |
10004 | 'Til There Was You | 1997 | 49 | 10000000 |
actor表:
id | name |
---|---|
20 | Paul Hogan |
50 | Jeanne Tripplehorn |
casting表:
movieid | actorid | ord |
---|---|---|
10003 | 20 | 4 |
10004 | 50 | 1 |
1.
List the films where the yr is 1962 [Show id, title]
SELECT id, title
FROM movie
WHERE yr=1962
2.
Give year of 'Citizen Kane'.
movie: (id, title, yr, director, budget, gross)
actor: (id, name)
casting: (movieid, actorid, ord)
select yr
from movie
where title='Citizen Kane'
3.order by
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.
movie: (id, title, yr, director, budget, gross)
actor: (id, name)
casting: (movieid, actorid, ord)
select id, title, yr
from movie
where title like '%Star Trek%'
order by yr
4.
What id number does the actor 'Glenn Close' have?
movie: (id, title, yr, director, budget, gross)
actor: (id, name)
casting: (movieid, actorid, ord)
select id
from actor
where name='Glenn Close'
5.
What is the id of the film 'Casablanca'
movie: (id, title, yr, director, budget, gross)
actor: (id, name)
casting: (movieid, actorid, ord)
select id
from movie
where title='Casablanca'
6.
Obtain the cast list for 'Casablanca'.
what is a cast list?
Use movieid=11768, (or whatever value you got from the previous question)
获取《卡萨布兰卡》的演员表。 什么是演员表? 使用movieid=11768,(或从上一个问题中获得的任何值)
movie: (id, title, yr, director, budget, gross)
actor: (id, name)
casting: (movieid, actorid, ord)
select name
from actor
where id in
(select actorid from casting
where movieid in
(select id from movie
where title='Casablanca'))
select name from actor a
join casting b on a.id=b.actorid
where b.movieid=11768
7.
Obtain the cast list for the film 'Alien'
select name
from actor
where id in
(select actorid from casting
where movieid in
(select id from movie
where title='Alien'))
或者三个表连接起来
movie: (id, title, yr, director, budget, gross)
actor: (id, name)
casting: (movieid, actorid, ord)
select name
from movie m
join casting on(id=movieid)
join actor a on(a.id=actorid)
where title='Alien'
8.
List the films in which 'Harrison Ford' has appeared
select title
from movie m
join casting on id=movieid
join actor a on a.id=actorid
where name='Harrison Ford'
9.
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]
列出哈里森·福特出演但不是主演的电影。【注:演员表中的 ord 字段给出了演员的位置。如果 ord=1,则该演员是主演】
select title
from movie m
join casting on id=movieid
join actor a on a.id = actorid
where ord!=1
and name='Harrison Ford'
10.
List the films together with the leading star for all 1962 films.
select title, name
from movie m
join casting on id=movieid
join actor a on a.id=actorid
where ord='1'
and yr='1962'
11.group by、having
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.
哪些年份是“罗克·赫德森”最繁忙的年份,显示每年他制作的电影数量,只包括他每年制作超过2部电影的年份。
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
12.join
List the film title and the leading actor for all of the films 'Julie Andrews' played in.
列出所有朱莉·安德鲁斯参演的电影的片名和主演。
Did you get "Little Miss Marker twice"?
Julie Andrews starred in the 1980 remake of Little Miss Marker and not the original(1934).
Title is not a unique field, create a table of IDs in your subquery
你得到“小小姐马克”两次了吗?
朱莉·安德鲁斯主演了1980年重制的《小 Miss Marker》,而不是原始版本(1934年)。 标题不是唯一字段,在子查询中创建一个ID表。
SELECT title, name FROM casting
join movie m on id=movieid
join actor a on a.id=actorid
WHERE movieid in(
select movieid from casting
join actor on id=actorid
where name='Julie Andrews')
and ord=1
13.group by、having、order by
Obtain a list, in alphabetical order, of actors who've had at least 15 starring roles.
获取按字母顺序排列的演员名单,这些演员至少有15个主演角色。
select name
from actor
join casting on id=actorid
where ord=1
group by name
having count(actorid)>=15
order by name
14.order by ... desc
List the films released in the year 1978 ordered by the number of actors in the cast, then by title.
列出1978年发行的电影,按照演员人数排序,然后按片名排序。
select title,count(actorid)
from movie
join casting on id=movieid
where yr='1978'
group by title
order by count(actorid) desc ,title
15.distinct
List all the people who have worked with 'Art Garfunkel'.
列出所有与'阿特·加芬克尔'合作过的人。
select distinct name
from actor
join casting on id=actorid
where name!='Art Garfunkel'
and movieid in
(select movieid from actor
join casting on id=actorid
where name='Art Garfunkel')