공부/SQL

SQL Zoo - More JOIN operations 문제 풀이

thegreatjy 2022. 3. 2. 23:10
728x90

https://sqlzoo.net/wiki/More_JOIN_operations

 

More JOIN operations - SQLZOO

This tutorial introduces the notion of a join. The database consists of three tables movie , actor and casting . movie id title yr director budget gross casting movieid actorid ord More details about the database. 1962 movies List the films where the yr is

sqlzoo.net


1. 1962 movies

SELECT id, title
 FROM movie
 WHERE yr=1962

 

2. 

select yr
from movie
where title = 'Citizen Kane';

 

3.

제목에 Star Trek 이 포함된 영화의 id, title, yr을 조회하는 sql문

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

 

4.

select id
from actor
where name = 'Glenn Close';

 

5.

select id
from movie
where title = 'Casablanca';

 

6. cast list for Casablanca ; join 찐시작~

<Casablanca> 영화에 출연한 연기자의 이름을 조회하는 명령문

 

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

 

7. 

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

 

8.

Harrison Ford 연기자가 출연한 영화의 이름을 조회

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

 

9.

Harrison Ford 연기자가 출연한 영화의 이름을 조회 + HF의 ord가 1이 아닌 영화 (casting.ord 는 integer)

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

 

10. (구글링)

1962년 영화 중 주연 배우의 이름과 영화 이름을 조회

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

 

11.

Rock Hudson배우가 1년에 2개 초과 영화에 출연한 년도와 출연 영화의 개수를 조회 

select yr, count(title)
from movie m 
join casting c on m.id = c.movieid
where c.actorid in 
(select id
from actor
where name = 'Rock Hudson')
group by m.yr
having count(title)>2;

 

12.

/*줄리가 출연한 영화의 이름과 주연 배우를 조회*/
select m.title, a.name
from casting c
join actor a on a.id = c.actorid
join movie m on m.id = c.movieid
where c.ord = 1
and m.id in (
select casting.movieid
from casting
where actorid in (select id from actor where name = 'Julie Andrews'));

 

13.

/*최소 15개의 주연을 한 적있는 배우를 알파벳순으로 조회*/
select name
from actor
where id in (
select a.id
from actor a
join casting c on c.actorid = a.id
where c.ord = 1
group by id, ord
having count(*)>=15)
order by name;

 

14.

/*1978년에 개봉한 영화의 이름과 출연 배우 수 조회 // 출연한 배우의 수, 제목 순으로 정렬*/
select title, count(actorid)
from movie m
join casting c on m.id = c.movieid
where m.yr = 1978
group by title
order by count(actorid) desc, title;

 

15.

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

..참고로 본인은 제외해야된다.. 여기서 조금 헤맸다 ㅋㅋㅋ

728x90