sqlzoo练习--MORE JION

The JOIN operation

此教程練習表格合拼。數據庫有三個表格:
movie電影(id編號, title電影名稱, yr首影年份, director導演, budget製作費, gross票房收入);
actor演員(id編號, name姓名);
casting角色(movieid電影編號, actorid演員編號, ord角色次序);
1.List the films where the yr is 1962 [Show id, title]

SELECT  id,titleFROM movieWHERE yr=1962

2.Give year of ‘Citizen Kane’.

SELECT  yrFROM movieWHERE title= 'Citizen Kane' 

3.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,yrFROM  movieWHERE title LIKE 'Star Trek%' 

4.What id number does the actor ‘Glenn Close’ have?

SELECT  idFROM  actorWHERE  name='Glenn Close'

5.What is the id of the film ‘Casablanca’

SELECT  idFROM  movieWHERE  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)

SELECT name
FROM actor
WHERE id IN (SELECT actoridFROM castingWHERE movieid IN (SELECT idFROM movieWHERE title='Casablanca'))

7.Obtain the cast list for the film ‘Alien’

SELECT name
FROM actor 
WHERE id IN (SELECT actoridFROM castingWHERE movieid IN (SELECT idFROM movieWHERE title='Alien'))

8.List the films in which ‘Harrison Ford’ has appeared

SELECT title
FROM movie
WHERE id IN (SELECT movieidFROM casting WHERE actorid=(SELECT idFROM actorWHERE 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]

SELECT title
FROM movie
WHERE id IN (SELECT movieidFROM casting WHERE actorid=(SELECT idFROM actorWHERE name='Harrison Ford')AND ord !=1)

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

SELECT title,nameFROM movie JOIN casting ON movie.id=movieidJOIN actor   ON actorid=actor.id
WHERE yr=1962 AND ord=1

11.Which were the busiest years for ‘John Travolta’, 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(*)FROM movie 
WHERE movie.id IN (SELECT movieidFROM castingWHERE actorid IN (SELECT actor.idFROM actorWHERE name='John Travolta'))
GROUP BY yr
ORDER BY COUNT(*) DESC limit 1

12.List the film title and the leading actor for all of the films ‘Julie Andrews’ played in.

SELECT title,nameFROM movie JOIN casting ON movie.id=movieidJOIN actor   ON actorid=actor.id
WHERE movie.id IN (SELECT movieidFROM castingWHERE actorid IN (SELECT actor.idFROM actorWHERE name='Julie Andrews'))AND ord=1

13.Obtain a list, in alphabetical order, of actors who’ve had at least 30 starring roles.

SELECT nameFROM  actor LEFT JOIN casting   ON actorid=actor.id
WHERE ord=1
GROUP BY name
HAVING COUNT(movieid)>=30

14.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 castingON movie.id = casting.movieid JOIN actorON actor.id = casting.actorid
WHERE yr = 1978
GROUP BY title
ORDER BY COUNT(actorid)DESC,title

15.List all the people who have worked with ‘Art Garfunkel’.

SELECT nameFROM  actor JOIN casting   ON actorid=actor.id
WHERE movieid IN (SELECT movieidFROM castingWHERE actorid IN (SELECT idFROM actorWHERE name='Art Garfunkel'))AND name !='Art Garfunkel'


本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!

相关文章

立即
投稿

微信公众账号

微信扫一扫加关注

返回
顶部