Difference between revisions of "More JOIN operations"
(Created page with "==Movie Database== <p>This tutorial introduces the notion of a join. The database consists of three tables <code>movie</code> , <code>actor</code> and <code>c...") |
|||
| Line 11: | Line 11: | ||
<div class='schema'>movie(<b>id</b>, title, yr, <i>director</i>)<br/> actor(<b>id</b>, name)<br/> casting(<b><i>movieid</i>, <i>actorid</i></b>, ord)<br/></div> | <div class='schema'>movie(<b>id</b>, title, yr, <i>director</i>)<br/> actor(<b>id</b>, name)<br/> casting(<b><i>movieid</i>, <i>actorid</i></b>, ord)<br/></div> | ||
<p> | <p> | ||
| − | More details about the database. | + | [[More details about the database.]] |
</p> | </p> | ||
<h2>Let's go to work.</h2> | <h2>Let's go to work.</h2> | ||
| Line 99: | Line 99: | ||
</div> | </div> | ||
| − | <h2>Get to the point</h2> | + | <h2>[[Get to the point]]</h2> |
<div class='qu'> | <div class='qu'> | ||
Obtain the cast list for 'Casablanca'. | Obtain the cast list for 'Casablanca'. | ||
| Line 289: | Line 289: | ||
</source> | </source> | ||
</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.] | ||
Revision as of 14:52, 10 July 2012
Contents |
Movie Database
This tutorial introduces the notion of a join. The database
consists of three tables
movie
,
actor
and
casting
.
actor(id, name)
casting(movieid, actorid, ord)
More details about the database.
Let's go to work.
Limbering up
List the films where the yr is 1962 [Show id, title]
movie(id,title,yr,director)
SELECT id, title FROM movie WHERE yr=1962
SELECT id, title FROM movie WHERE yr=1962
Give year of 'Citizen Kane'.
movie(id,title,yr,director)
SELECT yr FROM movie WHERE title='Citizen Kane'
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.)
movie(id,title,yr,director)
SELECT id,title, yr FROM movie WHERE title LIKE 'Star Trek%' ORDER BY yr
Looking at the id field.
What are the titles of the films with id 11768, 11955, 21191
movie(id,title,yr,director)
SELECT title FROM movie WHERE id IN (11768, 11955, 21191)
What id number does the actor 'Glenn Close' have?
movie(id,title,yr,director) actor(id,name) casting(movieid,actorid,ord)
SELECT id FROM actor WHERE name= 'Glenn Close'
What is the id of the film 'Casablanca'
movie(id,title,yr,director)
SELECT id FROM movie WHERE title='Casablanca'
Get to the point
Obtain the cast list for 'Casablanca'. Use the id value that you obtained in the previous question.
actor(id,name) casting(movieid,actorid,ord)
SELECT name FROM casting, actor WHERE movieid=(SELECT id FROM movie WHERE title='Casablanca') AND actorid=actor.id
Obtain the cast list for the film 'Alien'
movie(id,title,yr,director) actor(id,name) casting(movieid,actorid,ord)
SELECT name FROM movie, casting, actor WHERE title='Alien' AND movieid=movie.id AND actorid=actor.id
List the films in which 'Harrison Ford' has appeared
movie(id,title,yr,director) actor(id,name) casting(movieid,actorid,ord)
SELECT title FROM movie, casting, actor WHERE name='Harrison Ford' AND movieid=movie.id AND actorid=actor.id
List the films where 'Harrison Ford' has appeared - but not in the star role. [Note: the ord field of casting gives the position of the actor. If ord=1 then this actor is in the starring role]
movie(id,title,yr,director) actor(id,name) casting(movieid,actorid,ord)
SELECT title FROM movie, casting, actor WHERE name='Harrison Ford' AND movieid=movie.id AND actorid=actor.id AND ord<>1
List the films together with the leading star for all 1962 films.
movie(id,title,yr,director) actor(id,name) casting(movieid,actorid,ord)
SELECT title, name FROM movie, casting, actor WHERE yr=1962 AND movieid=movie.id AND actorid=actor.id AND ord=1
That's plenty joins for now. Students with an unhealthy interest in databases or movies may try the following harder questions; although they might be better advised to go out and get some fresh air.
List the films together with the leading star for all 1962 films.
movie(id,title,yr,director) actor(id,name) casting(movieid,actorid,ord)
SELECT yr,COUNT(title) FROM movie JOIN casting ON movie.id=movieid JOIN actor ON actorid=actor.id WHERE name='John Travolta' GROUP BY yr HAVING COUNT(title)=(SELECT MAX(c) FROM (SELECT yr,COUNT(title) AS c FROM movie JOIN casting ON movie.id=movieid JOIN actor ON actorid=actor.id WHERE name='John Travolta' GROUP BY yr) AS t )
List the film title and the leading actor for all of 'Julie Andrews' films.
movie(id,title,yr,director) actor(id,name) casting(movieid,actorid,ord)
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')
Obtain a list of actors in who have had at least 30 starring roles.
movie(id,title,yr,director) actor(id,name) casting(movieid,actorid,ord)
SELECT name FROM casting JOIN actor ON actorid = actor.id WHERE ord=1 GROUP BY name HAVING COUNT(movieid)>=30
List the 1978 films by order of cast list size.
movie(id,title,yr,director) actor(id,name) casting(movieid,actorid,ord)
SELECT title, COUNT(actorid) FROM casting, movie WHERE yr=1978 AND movieid=movie.id GROUP BY title ORDER BY 2 DESC
List all the people who have worked with 'Art Garfunkel'.
movie(id,title,yr,director) actor(id,name) casting(movieid,actorid,ord)
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