SQLZOO中文版答案记录/More JOIN operations

1

SELECT id, title
 FROM movie
 WHERE yr=1962

2

SELECT yr
 FROM movie
 WHERE title like 'Citizen Kane%'

3

SELECT id, title, yr
 FROM movie
 WHERE title like 'Star Trek%'
 order by yr

4

select title from movie
 where id in (11768,11955,21191)

5

SELECT id
 FROM actor
 WHERE name like 'Glenn Close'

6

select id
 from movie
 where title like 'Casablanca'

7、这里movieid是6结果得到的id

select name from actor
 where id in
 (select actorid from casting where movieid=27)

8

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

9

select title from movie where id in
 (select movieid from casting c join actor a on c.actorid=a.id
 where name like 'Harrison Ford')

10、ord>1或ord!=1

select title from movie where id in
 (select movieid from casting c join actor a on c.actorid=a.id
 where name like 'Harrison Ford' and ord>1)

11、两次合并

select title, name from actor a join
 movie m join casting c on m.id=c.movieid
 on a.id=c.actorid
 where yr=1962 and ord=1

12

SELECT yr,COUNT(title) FROM movie 
 JOIN casting ON movie.id=movieid
 JOIN actor   ON actorid=actor.id
 where name='John Travolta'
 GROUP BY yr
 HAVING COUNT(title)=(SELECT MAX(c) FROM
 (SELECT yr,COUNT(title) AS c FROM movie 
   JOIN casting ON movie.id=movieid
   JOIN actor   ON actorid=actor.id
 where name='John Travolta'
 GROUP BY yr) AS t)

13、两种方式,区别在后面,一种不合并直接从actorid出发,一种先合并,再用movieid

select title,name from casting c
 join movie m on m.id=c.movieid
 join actor a on a.id=c.actorid
 where movieid in
(select movieid from casting
where actorid in 
(select id from actor where name='Julie Andrews'))
and ord=1
select title,name from casting c
 join movie m on m.id=c.movieid
 join actor a on a.id=c.actorid
 where movieid in
(select movieid from casting c
 join actor a on a.id=c.actorid
 where name='Julie Andrews') and ord=1

14、注意区分是等于30次还是大于30次。英文版里是大于15次 >=15

select name from actor a
 join casting c on a.id=c.actorid
 where ord=1
 group by name
 having count(name)=30
 order by name

15

select title, count(actorid) from casting c
 join movie m on m.id=c.movieid
 where yr=1978
 group by title
 order by count(actorid) desc

16

select name from actor a
 join casting c on a.id=c.actorid
 where movieid in
 (select movieid from casting c join actor a on c.actorid=a.id
 where name='Art Garfunkel') 
 and name!='Art Garfunkel'
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值