sqlzoo 刷题答案

网址:https://sqlzoo.net/wiki/More_JOIN_operations

1.

List the films where the yr is 1962 [Show idtitle]

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 idtitle 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;

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'.

what is a cast list?

The cast list is the names of the actors who were in the movie.

Use movieid=11768, (or whatever value you got from the previous question)

select name from actor 
join casting on actor.id=casting.actorid 
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 
where actorid=(
select id from actor 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]

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

10.

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 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.

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.

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

select title,actor.name from movie 
join casting on casting.movieid=movie.id 
join actor on actor.id=casting.actorid  
where movieid in( 
SELECT movieid FROM casting
WHERE actorid IN (
  SELECT id FROM actor
  WHERE name='Julie Andrews')) and ord=1;

13.

Obtain a list, in alphabetical order, of actors who've had at least 15 starring roles.

select distinct name from actor 
join casting on casting.actorid=actor.id 
where actorid in (
select actorid from casting 
where ord=1 
group by actorid 
having count(*)>=15
) 
order by actor.name;

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 casting on movie.id=casting.movieid 
where yr=1978 
group by movieid,title 
order by (count(*)) desc,title;

15.

List all the people who have worked with 'Art Garfunkel'.

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

 

全部评论

相关推荐

整顿职场的柯基很威猛:这种不可怕,最可怕的是夹在一帮名校里的二本选手,人家才是最稳的。
点赞 评论 收藏
分享
头像
10-16 09:58
已编辑
门头沟学院 Java
点赞 评论 收藏
分享
评论
点赞
收藏
分享
牛客网
牛客企业服务