-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path7_more_join.sql
More file actions
121 lines (106 loc) · 3.28 KB
/
7_more_join.sql
File metadata and controls
121 lines (106 loc) · 3.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
-- List the films where the yr is 1962 [Show id, title]
SELECT id, title
FROM movie
WHERE yr=1962;
-- When was Citizen Kane released?
SELECT yr
FROM movie
WHERE movie.title='Citizen Kane';
-- Star Trek movies
SELECT id, title, yr FROM movie
WHERE title LIKE 'Star Trek%'
ORDER BY yr;
-- What id number does the actor 'Glenn Close' have?
SELECT id FROM actor
WHERE name = 'Glenn Close'
-- What is the id of the film 'Casablanca'
SELECT id FROM movie
WHERE title = 'Casablanca';
-- Cast list for Casablanca
SELECT actor.name FROM movie
JOIN casting
ON movie.id=casting.movieid
JOIN actor
ON actor.id=casting.actorid
WHERE movie.title = 'Casablanca';
-- Alien cast list
SELECT actor.name FROM movie
JOIN casting
ON movie.id=casting.movieid
JOIN actor
ON actor.id=casting.actorid
WHERE movie.title = 'Alien';
-- List the films in which 'Harrison Ford' has appeared
SELECT movie.title FROM movie
JOIN casting
ON movie.id=casting.movieid
JOIN actor
ON actor.id=casting.actorid
WHERE actor.name = 'Harrison Ford';
-- 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 casting JOIN actor ON id = actorid
INNER JOIN movie ON casting.movieid = movie.id
WHERE name='Harrison Ford' AND ord =1;
-- 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 actor.id=casting.actorid
WHERE movie.yr=1962 AND casting.ord=1;
-- Busy years for Rock Hudson
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;
-- Lead actor in Julie Andrews movies
SELECT movie.yr ,COUNT(movie.title) FROM movie
JOIN casting
ON movie.id=casting.movieid
JOIN actor
ON actor.id=casting.actorid
WHERE actor.name = 'Rock Hudson'
GROUP BY yr
ORDER BY COUNT(movie.title) DESC LIMIT 2;
-- List the film title and the leading actor for all of the films 'Julie Andrews' played in.
SELECT movie.title, actor.name
FROM movie
JOIN casting ON movie.id = movieid
JOIN actor ON actorid = actor.id
WHERE movie.id IN (SELECT casting.movieid
FROM casting
WHERE actorid = (SELECT id
FROM actor
WHERE name = 'Julie Andrews')
AND ord = 1);
-- Actors with 15 leading roles
SELECT name
FROM actor
JOIN casting ON id = casting.actorid
WHERE ord = 1
GROUP BY name
HAVING COUNT(name) >= 15
ORDER BY name;
-- 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 movie.yr = 1978
GROUP BY title
ORDER BY COUNT(casting.actorid)DESC,title ;
-- List all the people who have worked with 'Art Garfunkel'.
SELECT name
FROM actor
JOIN casting ON id = casting.actorid
WHERE movieid IN
(SELECT movieid
FROM casting
WHERE actorid =
(SELECT id
FROM actor
WHERE name = 'Art Garfunkel'))
AND name <> 'Art Garfunkel'
ORDER BY name