SQL Zoo 7.More JOIN operations

以下数据均来自SQL Zoo

1.List the films where the yr is 1962 [Show idtitle](列出1962年的电影)

SELECT id, title
 FROM movie
 WHERE yr=1962

2.Give year of 'Citizen Kane'.(给出《公民凯恩》的年份)

select yr from movie where title = 'Citizen Kane'

3.List all of the Star Trek movies, include the id, title and yr. Order results by year.(列出所有星际迷航电影,包括id,标题和年份。按年排序结果)

select id,title,yr from movie 
where title like 'Star Trek%' order by yr

4.What id number does the actor 'Glenn Close' have?(演员格伦·克洛斯的身份证号码)

select id from actor where name = 'Glenn Close'

5.What is the id of the film 'Casablanca'.(电影《卡萨布兰卡》的主题)

select id from movie where title = 'Casablanca'

6.Obtain the cast list for 'Casablanca'.(获得《卡萨布兰卡》的演员名单)

select name from casting 
join actor on casting.actorid = actor.id where movieid = 11768

7.Obtain the cast list for the film 'Alien'.(获取电影《异形》的演员名单)

select name from actor 
join casting on actor.id = casting.actorid where movieid = 
(select id from movie where title = 'Alien')

8.List the films in which 'Harrison Ford' has appeared.(列出哈里森·福特出演过的电影)

select title from movie 
join casting on movie.id = casting.movieid
join actor on casting.actorid = actor.id
where name = 'Harrison Ford'

9.List the films where 'Harrison Ford' has appeared - but not in the starring role.(列出哈里森·福特出演过但不是主演的电影)

select title from movie 
join casting on movie.id = casting.movieid
join actor on casting.actorid = actor.id
where name = 'Harrison Ford' and ord !=1

10.List the films together with the leading star for all 1962 films.(列出所有1962年电影的电影和主演)

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.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.(这是Rock Hudson最繁忙的年份,显示了他每年制作的电影数量,其中任何一年他制作了超过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.List the film title and the leading actor for all of the films 'Julie Andrews' played in.(列出朱莉·安德鲁斯出演的所有电影的片名和主演)

select distinct(title),name 
from movie join casting on movie.id=movieid
join actor on actorid=actor.id 
and ord=1
and movieid in (
  select t2.movieid 
  from actor t1  join casting t2 on t2.actorid=t1.id 
  and name='Julie Andrews')

13.Obtain a list, in alphabetical order, of actors who've had at least 15 starring roles.(按字母顺序获得一份出演过至少15个主演角色的演员名单)

select name from actor 
left join casting on actor.id = casting.actorid 
where ord=1 group by name having count(ord)>=15

14.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 movie.id = casting.movieid and yr = 1978 
group by title 
order by count(actorid) desc,title 

15.List all the people who have worked with 'Art Garfunkel'.(列出所有与“Art Garfunkel”合作过的人)

select name from casting 
join actor on casting.actorid = actor.id 
where movieid in 
(select movieid from casting where actorid = 
(select id from actor where name = 'Art Garfunkel')) and name != 'Art Garfunkel'

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

世尘07

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值