Difference between revisions of "More JOIN operations"

From SQLZOO
Jump to: navigation, search
(37 intermediate revisions by 10 users not shown)
Line 1: Line 1:
<h3>Movie Database></h3>
+
<h1>Movie Database</h1>
 
  <p>This tutorial introduces the notion of a join. The database
 
  <p>This tutorial introduces the notion of a join. The database
 
   consists of three tables  
 
   consists of three tables  
Line 9: Line 9:
 
   .
 
   .
 
</p>
 
</p>
  <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='sc'></div>
 +
<div class = 'ref_section'>
 +
<table class = 'db_ref'>
 +
<tr><th>'''movie'''</th><th>'''actor'''</th><th>'''casting'''</th></tr>
 +
<tr><td>id</td><td>id</td><td>movieid</td></tr>
 +
<tr><td>title</td><td>name</td><td>actorid</td></tr>
 +
<tr><td>yr</td><td></td><td>ord</td></tr>
 +
<tr><td>director</td><td></td><td></td></tr>
 +
<tr><td>budget</td><td></td><td></td></tr>
 +
<tr><td>gross</td><td></td><td></td></tr>
 +
<tr><td></td><td></td><td></td></tr>
 +
</table>
 +
</div>
 
   <p>
 
   <p>
 
     [[More details about the database.]]
 
     [[More details about the database.]]
 
   </p>
 
   </p>
 +
 +
 +
<div class="progress_panel"><div>
 +
  <div class="summary">Summary</div>
 +
  <div class="progressbarbg">
 +
    <div class="progressbar"></div>
 +
  </div>
 +
</div></div>
 +
 
<h2>Let's go to work.</h2>
 
<h2>Let's go to work.</h2>
 
<p>Limbering up</p>
 
<p>Limbering up</p>
 
<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''']   
movie('''id''',title,yr,director)    
+
 
<source lang='sql' class='def'>
 
<source lang='sql' class='def'>
 
SELECT id, title
 
SELECT id, title
Line 32: Line 52:
  
 
<div class='qu'>
 
<div class='qu'>
Give year of 'Citizen Kane'.  
+
Give year of 'Citizen Kane'.  
movie('''id''',title,yr,director) 
+
 
<source lang='sql' class='def'>
 
<source lang='sql' class='def'>
 
</source>
 
</source>
Line 45: Line 64:
  
 
<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.)
+
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.  
movie('''id''',title,yr,director) 
+
 
<source lang='sql' class='def'>
 
<source lang='sql' class='def'>
 
</source>
 
</source>
Line 59: Line 77:
 
<h2>Looking at the '''id''' field.</h2>
 
<h2>Looking at the '''id''' field.</h2>
 
<div class='qu'>
 
<div class='qu'>
What are the titles of the films with '''id''' 11768, 11955, 21191  
+
What are the titles of the films with '''id''' 11768, 11955, 21191   
movie('''id''',title,yr,director)    
+
 
<source lang='sql' class='def'>
 
<source lang='sql' class='def'>
 
</source>
 
</source>
Line 72: Line 89:
  
 
<div class='qu'>
 
<div class='qu'>
What '''id''' number does the actor 'Glenn Close' have?  
+
What '''id''' number does the actor 'Glenn Close' have?  
movie('''id''',title,yr,director)
+
actor('''id''',name)
+
casting('''movieid,actorid''',ord) 
+
 
<source lang='sql' class='def'>
 
<source lang='sql' class='def'>
 
</source>
 
</source>
Line 87: Line 101:
  
 
<div class='qu'>
 
<div class='qu'>
What is the '''id''' of the film 'Casablanca'
+
What is the '''id''' of the film 'Casablanca'  
movie('''id''',title,yr,director) 
+
 
<source lang='sql' class='def'>
 
<source lang='sql' class='def'>
 
</source>
 
</source>
Line 102: Line 115:
 
<div class='qu'>
 
<div class='qu'>
 
Obtain the cast list for 'Casablanca'.  
 
Obtain the cast list for 'Casablanca'.  
Use the '''id''' value that you obtained in the previous question.
+
Use the '''id''' value that you obtained in the previous question.   
actor('''id''',name)
+
casting('''movieid,actorid''',ord)  
+
 
<source lang='sql' class='def'>
 
<source lang='sql' class='def'>
 
</source>
 
</source>
Line 119: Line 130:
  
 
<div class='qu'>
 
<div class='qu'>
Obtain the cast list for the film 'Alien'
+
Obtain the cast list for the film 'Alien'   
movie('''id''',title,yr,director)
+
actor('''id''',name)
+
casting('''movieid,actorid''',ord)  
+
 
<source lang='sql' class='def'>
 
<source lang='sql' class='def'>
 
</source>
 
</source>
Line 136: Line 144:
  
 
<div class='qu'>
 
<div class='qu'>
List the films in which 'Harrison Ford' has appeared
+
List the films in which 'Harrison Ford' has appeared  
movie('''id''',title,yr,director)
+
actor('''id''',name)
+
casting('''movieid,actorid''',ord) 
+
 
<source lang='sql' class='def'>
 
<source lang='sql' class='def'>
 
</source>
 
</source>
Line 155: Line 160:
 
List the films where 'Harrison Ford' has appeared - but not in the star role.  
 
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.  
 
[Note: the '''ord''' field of casting gives the position of the actor.  
If ord=1 then this actor is in the starring role]  
+
If ord=1 then this actor is in the starring role]    
movie('''id''',title,yr,director)
+
actor('''id''',name)
+
casting('''movieid,actorid''',ord) 
+
 
<source lang='sql' class='def'>
 
<source lang='sql' class='def'>
 
</source>
 
</source>
Line 173: Line 175:
  
 
<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.    
movie('''id''',title,yr,director)
+
actor('''id''',name)
+
casting('''movieid,actorid''',ord) 
+
 
<source lang='sql' class='def'>
 
<source lang='sql' class='def'>
 
</source>
 
</source>
Line 190: Line 189:
 
</div>
 
</div>
  
<h2>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.</h2>
+
<h2>Harder Questions</h2>
 
<div class='qu'>
 
<div class='qu'>
List the films together with the leading star for all 1962 films.   
+
Which were the busiest years for 'John Travolta', show the year and the number of movies he made each year for any year in which he made at least 2 movies.     
movie('''id''',title,yr,director)
+
actor('''id''',name)
+
casting('''movieid,actorid''',ord) 
+
 
<source lang='sql' class='def'>
 
<source lang='sql' class='def'>
 +
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
 +
)
 
</source>
 
</source>
  
Line 207: Line 215:
 
HAVING COUNT(title)=(SELECT MAX(c) FROM
 
HAVING COUNT(title)=(SELECT MAX(c) FROM
 
(SELECT yr,COUNT(title) AS c FROM
 
(SELECT yr,COUNT(title) AS c FROM
  movie JOIN casting ON movie.id=movieid
+
movie JOIN casting ON movie.id=movieid  
        JOIN actor  ON actorid=actor.id
+
JOIN actor  ON actorid=actor.id
 
  where name='John Travolta'
 
  where name='John Travolta'
 
  GROUP BY yr) AS t
 
  GROUP BY yr) AS t
Line 216: Line 224:
  
 
<div class='qu'>
 
<div class='qu'>
List the film title and the leading actor for all of 'Julie Andrews' films.    
+
List the film title and the leading actor for all of the films 'Julie Andrews' played in.   
movie('''id''',title,yr,director)
+
actor('''id''',name)
+
casting('''movieid,actorid''',ord)  
+
 
<source lang='sql' class='def'>
 
<source lang='sql' class='def'>
 
</source>
 
</source>
Line 237: Line 242:
  
 
<div class='qu'>
 
<div class='qu'>
Obtain a list of actors in who have had at least 30 starring roles.    
+
Obtain a list in alphabetical order of actors who've had at least 30 starring roles.      
movie('''id''',title,yr,director)
+
actor('''id''',name)
+
casting('''movieid,actorid''',ord) 
+
 
<source lang='sql' class='def'>
 
<source lang='sql' class='def'>
 
</source>
 
</source>
Line 255: Line 257:
  
 
<div class='qu'>
 
<div class='qu'>
List the 1978 films by order of cast list size.      
+
List the 1978 films by order of cast list size.        
movie('''id''',title,yr,director)
+
actor('''id''',name)
+
casting('''movieid,actorid''',ord) 
+
 
<source lang='sql' class='def'>
 
<source lang='sql' class='def'>
 
</source>
 
</source>
  
 
<source lang='sql' class='ans'>
 
<source lang='sql' class='ans'>
SELECT title, COUNT(actorid)
+
  SELECT title, COUNT(actorid)
   FROM casting, movie
+
   FROM casting,movie              
 
   WHERE yr=1978
 
   WHERE yr=1978
    AND movieid=movie.id
+
        AND movieid=movie.id
 
   GROUP BY title
 
   GROUP BY title
 
   ORDER BY 2 DESC
 
   ORDER BY 2 DESC
Line 273: Line 272:
  
 
<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'.        
movie('''id''',title,yr,director)
+
actor('''id''',name)
+
casting('''movieid,actorid''',ord) 
+
 
<source lang='sql' class='def'>
 
<source lang='sql' class='def'>
 
</source>
 
</source>
Line 288: Line 284:
 
   WHERE d.id!=c.id
 
   WHERE d.id!=c.id
 
</source>
 
</source>
 +
</div>
 +
<div>
 +
<div class="lsclear">Clear your results</div>
 +
<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.]

Revision as of 12:18, 27 March 2013

Contents

Movie Database

This tutorial introduces the notion of a join. The database consists of three tables movie , actor and casting .

movieactorcasting
ididmovieid
titlenameactorid
yrord
director
budget
gross

More details about the database.


Summary

Let's go to work.

Limbering up

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

Give year of 'Citizen Kane'.

 
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). Order results by year.

 
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

 
SELECT title 
FROM movie 
WHERE id IN (11768, 11955, 21191)

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'

Get to the point

Obtain the cast list for 'Casablanca'. Use the id value that you obtained in the previous question.

 
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'

 
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

 
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]

 
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.

 
SELECT title, name
  FROM movie, casting, actor
 WHERE yr=1962
    AND movieid=movie.id
    AND actorid=actor.id
    AND ord=1

Harder Questions

Which were the busiest years for 'John Travolta', show the year and the number of movies he made each year for any year in which he made at least 2 movies.

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
)
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 the films 'Julie Andrews' played in.

 
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 in alphabetical order of actors who've had at least 30 starring roles.

 
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.

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

 
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
Clear your results

That is definitely enough. Students should, under no circumstances look at the next tutorial, concerning outer joins.

Personal tools
Namespaces

Variants
Actions
Reference
Toolbox
Google AdSense