Difference between revisions of "More JOIN operations"
m |
Kobashi.kaz (talk | contribs) () |
||
(32 intermediate revisions by 5 users not shown) | |||
Line 1: | Line 1: | ||
{{Languages|More_JOIN_operations}} | |||
<p>This tutorial introduces the notion of a join. The database | |||
consists of three tables | |||
<code>movie</code> | <code>movie</code> | ||
, | , | ||
Line 9: | Line 9: | ||
. | . | ||
</p> | </p> | ||
<table style='display:inline-block;margin:2ex;'><caption>movie</caption> | |||
<table | <tr> | ||
<td><b>id</b></td> | |||
< | <td>title</td> | ||
<td>yr</td> | |||
<td><i>director</i></td> | |||
<td>budget</td> | |||
<td>gross</td> | |||
</tr> | |||
</table> | </table> | ||
<table style='display:inline-block;margin:2ex;'><caption>actor</caption> | |||
<tr> | |||
<td><b>id</b></td> | |||
<td>name</td> | |||
</tr> | |||
</table> | |||
<table style='display:inline-block;margin:2ex;'><caption>casting</caption> | |||
<tr> | |||
<td><b><i>movieid</i></b></td> | |||
<td><b><i>actorid</i></b></td> | |||
<td><b>ord</b></td> | |||
</tr> | |||
</table> | |||
[[File:movie-er.png]] | |||
</div> | </div> | ||
<p> | <p> | ||
Line 34: | Line 50: | ||
</div></div> | </div></div> | ||
==1962 movies== | |||
<div class='qu'> | <div class='qu'> | ||
List the films where the '''yr''' is 1962 [Show '''id''', '''title'''] | List the films where the '''yr''' is 1962 [Show '''id''', '''title'''] | ||
Line 51: | Line 66: | ||
</div> | </div> | ||
==When was Citizen Kane released?== | |||
<div class='qu'> | <div class='qu'> | ||
Give year of 'Citizen Kane'. | Give year of 'Citizen Kane'. | ||
Line 63: | Line 79: | ||
</div> | </div> | ||
==Star Trek movies== | |||
<div class='qu'> | <div class='qu'> | ||
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. | 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. | ||
Line 75: | Line 92: | ||
</div> | </div> | ||
==id for actor Glenn Close== | |||
<div class='qu'> | <div class='qu'> | ||
What '''id''' number does the | What '''id''' number does the actor 'Glenn Close' have? | ||
<source lang='sql' class='def'> | <source lang='sql' class='def'> | ||
</source> | </source> | ||
Line 100: | Line 105: | ||
==id for Casablanca== | |||
<div class='qu'> | <div class='qu'> | ||
What is the '''id''' of the film 'Casablanca' | What is the '''id''' of the film 'Casablanca' | ||
Line 112: | Line 118: | ||
</div> | </div> | ||
[[Get to the point]] | |||
==Cast list for Casablanca== | |||
<div class='qu'> | <div class='qu'> | ||
Obtain the cast list for 'Casablanca'. | Obtain the cast list for 'Casablanca'. | ||
Line 118: | Line 125: | ||
The cast list is the names of the actors who were in the movie. | The cast list is the names of the actors who were in the movie. | ||
</div> | </div> | ||
Use '''movieid=11768''', | Use '''movieid=11768''', (or whatever value you got from the previous question) | ||
<source lang='sql' class='def'> | <source lang='sql' class='def'> | ||
</source> | </source> | ||
Line 132: | Line 139: | ||
</div> | </div> | ||
==Alien cast list== | |||
<div class='qu'> | <div class='qu'> | ||
Obtain the cast list for the film 'Alien' | Obtain the cast list for the film 'Alien' | ||
Line 146: | Line 154: | ||
</div> | </div> | ||
==Harrison Ford movies== | |||
<div class='qu'> | <div class='qu'> | ||
List the films in which 'Harrison Ford' has appeared | List the films in which 'Harrison Ford' has appeared | ||
Line 160: | Line 169: | ||
</div> | </div> | ||
==Harrison Ford as a supporting actor== | |||
<div class='qu'> | <div class='qu'> | ||
List the films where 'Harrison Ford' has appeared - but not in the starring role. | List the films where 'Harrison Ford' has appeared - but not in the starring role. | ||
Line 177: | Line 187: | ||
</div> | </div> | ||
==Lead actors in 1962 movies== | |||
<div class='qu'> | <div class='qu'> | ||
List the films together with the leading star for all 1962 films. | List the films together with the leading star for all 1962 films. | ||
Line 191: | Line 202: | ||
</source> | </source> | ||
</div> | </div> | ||
<hr> | |||
Harder Questions | |||
<hr> | |||
==Busy years for Rock Hudson== | |||
<div class='qu'> | <div class='qu'> | ||
Which were the busiest years for ' | 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. | ||
<source lang='sql' class='def'> | <source lang='sql' class='def'> | ||
SELECT yr,COUNT(title) FROM | SELECT yr,COUNT(title) FROM | ||
movie JOIN casting ON movie.id=movieid | movie JOIN casting ON movie.id=movieid | ||
JOIN actor ON actorid=actor.id | |||
WHERE name='Doris Day' | |||
GROUP BY yr | GROUP BY yr | ||
HAVING COUNT(title) | HAVING COUNT(title) > 1 | ||
</source> | </source> | ||
Line 213: | Line 221: | ||
SELECT yr,COUNT(title) FROM | SELECT yr,COUNT(title) FROM | ||
movie JOIN casting ON movie.id=movieid | movie JOIN casting ON movie.id=movieid | ||
JOIN actor ON actorid=actor.id | |||
where name=' | where name='Rock Hudson' | ||
GROUP BY yr | GROUP BY yr | ||
HAVING COUNT(title) | HAVING COUNT(title) > 2 | ||
</source> | </source> | ||
</div> | </div> | ||
==Lead actor in Julie Andrews movies== | |||
<div class='qu'> | <div class='qu'> | ||
List the film title and the leading actor for all of the films 'Julie Andrews' played in. | List the film title and the leading actor for all of the films 'Julie Andrews' played in. | ||
<div class='hint' title='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 | |||
</div> | |||
<source lang='sql' class='def'> | <source lang='sql' class='def'> | ||
SELECT movieid FROM casting | SELECT movieid FROM casting | ||
WHERE | WHERE actorid IN ( | ||
SELECT id FROM actor | SELECT id FROM actor | ||
WHERE name='Julie Andrews') | WHERE name='Julie Andrews') | ||
Line 246: | Line 254: | ||
AND name='Julie Andrews') | AND name='Julie Andrews') | ||
</source> | </source> | ||
{{#ev:youtube|BcNIDK5qYx8|854x480}} | |||
</div> | </div> | ||
==Actors with 15 leading roles== | |||
<div class='qu'> | <div class='qu'> | ||
Obtain a list, in alphabetical order, of actors who've had at least | Obtain a list, in alphabetical order, of actors who've had at least 15 '''starring''' roles. | ||
<source lang='sql' class='def'> | <source lang='sql' class='def'> | ||
</source> | </source> | ||
Line 259: | Line 269: | ||
WHERE ord=1 | WHERE ord=1 | ||
GROUP BY name | GROUP BY name | ||
HAVING COUNT(movieid)>= | HAVING COUNT(movieid)>=15 | ||
</source> | </source> | ||
</div> | </div> | ||
== released in the year 1978 == | |||
<div class='qu'> | <div class='qu'> | ||
List the films released in the year 1978 ordered by the number of actors in the cast. | <span class='param respectorder'></span> | ||
List the films released in the year 1978 ordered by the number of actors in the cast, then by title. | |||
<source lang='sql' class='def'> | <source lang='sql' class='def'> | ||
</source> | </source> | ||
Line 274: | Line 286: | ||
AND movieid=movie.id | AND movieid=movie.id | ||
GROUP BY title | GROUP BY title | ||
ORDER BY 2 DESC | ORDER BY 2 DESC,1 ASC | ||
</source> | </source> | ||
</div> | </div> | ||
== with 'Art Garfunkel' == | |||
<div class='qu'> | <div class='qu'> | ||
List all the people who have worked with 'Art Garfunkel'. | List all the people who have worked with 'Art Garfunkel'. | ||
Line 293: | Line 306: | ||
</div> | </div> | ||
<div> | <div> | ||
<div class="lsclear">Clear your results</div> | <div class="lsclear">Clear your results</div> | ||
<p><div class="quizlink">[[JOIN Quiz 2]]</div></p> | <p><div class="quizlink">[[JOIN Quiz 2]]</div></p> | ||
</div> | </div> | ||
[http://sqlzoo.net/w/index.php/Using_Null That is definitely enough. Students should, under no circumstances look at the next tutorial, concerning outer joins.] | [http://sqlzoo.net/w/index.php/Using_Null That is definitely enough. Students should, under no circumstances look at the next tutorial, concerning outer joins.] |
Latest revision as of 05:22, 26 May 2022
Language: | English • 日本語 • 中文 |
---|
This tutorial introduces the notion of a join. The database
consists of three tables
movie
,
actor
and
casting
.
id | title | yr | director | budget | gross |
id | name |
movieid | actorid | ord |
More details about the database.
1962 movies
List the films where the yr is 1962 [Show id, title]
SELECT id, title
FROM movie
WHERE yr=1962
SELECT id, title
FROM movie
WHERE yr=1962
When was Citizen Kane released?
Give year of 'Citizen Kane'.
SELECT yr
FROM movie
WHERE title='Citizen Kane'
Star Trek movies
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, yr FROM movie
WHERE title LIKE 'Star Trek%'
ORDER BY yr
id for actor Glenn Close
What id number does the actor 'Glenn Close' have?
SELECT id FROM actor
WHERE name= 'Glenn Close'
id for Casablanca
What is the id of the film 'Casablanca'
SELECT id
FROM movie
WHERE title='Casablanca'
Cast list for Casablanca
Obtain the cast list for 'Casablanca'.
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 casting, actor
WHERE movieid=(SELECT id
FROM movie
WHERE title='Casablanca')
AND actorid=actor.id
Alien cast list
Obtain the cast list for the film 'Alien'
SELECT name
FROM movie, casting, actor
WHERE title='Alien'
AND movieid=movie.id
AND actorid=actor.id
Harrison Ford movies
List the films in which 'Harrison Ford' has appeared
SELECT title
FROM movie, casting, actor
WHERE name='Harrison Ford'
AND movieid=movie.id
AND actorid=actor.id
Harrison Ford as a supporting actor
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, casting, actor
WHERE name='Harrison Ford'
AND movieid=movie.id
AND actorid=actor.id
AND ord<>1
Lead actors in 1962 movies
List the films together with the leading star for all 1962 films.
SELECT title, name
FROM movie, casting, actor
WHERE yr=1962
AND movieid=movie.id
AND actorid=actor.id
AND ord=1
Harder Questions
Busy years for Rock Hudson
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='Doris Day'
GROUP BY yr
HAVING COUNT(title) > 1
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
List the film title and the leading actor for all of the films 'Julie Andrews' played in.
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 movieid FROM casting
WHERE actorid IN (
SELECT id FROM actor
WHERE name='Julie Andrews')
SELECT title, name
FROM movie, casting, actor
WHERE movieid=movie.id
AND actorid=actor.id
AND ord=1
AND movieid IN
(SELECT movieid FROM casting, actor
WHERE actorid=actor.id
AND name='Julie Andrews')
Actors with 15 leading roles
Obtain a list, in alphabetical order, of actors who've had at least 15 starring roles.
SELECT name
FROM casting JOIN actor
ON actorid = actor.id
WHERE ord=1
GROUP BY name
HAVING COUNT(movieid)>=15
released in the year 1978
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 casting,movie
WHERE yr=1978
AND movieid=movie.id
GROUP BY title
ORDER BY 2 DESC,1 ASC
with 'Art Garfunkel'
List all the people who have worked with 'Art Garfunkel'.
SELECT DISTINCT d.name
FROM actor d JOIN casting a ON (a.actorid=d.id)
JOIN casting b on (a.movieid=b.movieid)
JOIN actor c on (b.actorid=c.id
and c.name='Art Garfunkel')
WHERE d.id!=c.id