Difference between revisions of "JOIN Quiz 2"

From SQLZOO
Jump to: navigation, search
 
(26 intermediate revisions by 7 users not shown)
Line 1: Line 1:
 
JOIN Quiz - part 2
 
JOIN Quiz - part 2
  
<div class='db_ref' style='background:none'>
+
<div class=quiz>
<table class='zoo'>
+
<div class=q>Select the statement which lists the unfortunate directors of the movies which have caused financial loses (gross < budget)
<caption>teacher</caption>
+
<syntaxhighlight class=d lang='sql'>
<tr>
+
SELECT JOIN(name FROM actor, movie
<th>id</th>
+
      ON actor.id:director WHERE gross < budget)
<th>dept</th>
+
GROUP BY name </syntaxhighlight>
<th>name</th>
+
<syntaxhighlight class=d lang='sql'>
<th>phone</th>
+
SELECT name
<th>mobile</th>
+
FROM actor INNER JOIN movie BY actor.id = director
</tr>
+
HAVING gross < budget </syntaxhighlight>
<tr>
+
<syntaxhighlight class='d y' lang='sql'>
<td align='right'>101</td>
+
SELECT name
<td align='right'>1</td>
+
  FROM actor INNER JOIN movie ON actor.id = director
<td>Shrivell</td>
+
WHERE gross < budget </syntaxhighlight>
<td align='right'>2753</td>
+
<syntaxhighlight class=d lang='sql'>
<td>07986 555 1234</td>
+
SELECT name
</tr>
+
  FROM actor INNER JOIN movie ON actor.id:director
<tr>
+
WHERE gross < budget </syntaxhighlight>
<td align='right'>102</td>
+
<syntaxhighlight class=d lang='sql'>
<td align='right'>1</td>
+
SELECT name
<td>Throd</td>
+
  FROM director INNER JOIN movie ON movie.id = director.id
<td align='right'>2754</td>
+
WHERE gross < budget </syntaxhighlight>
<td>07122 555 1920</td>
 
</tr>
 
<tr><td align='right'>103</td>
 
<td align='right'>1</td>
 
<td>Splint</td>
 
<td align='right'>2293</td>
 
<td></td>
 
</tr>
 
<tr><td align='right'>104</td>
 
<td align='right'></td>
 
<td>Spiregrain</td>
 
<td align='right'>3287</td>
 
<td></td>
 
</tr>
 
<tr>
 
<td align='right'>105</td>
 
<td align='right'>2</td>
 
<td>Cutflower</td>
 
<td align='right'>3212</td>
 
<td>07996 555 6574</td>
 
</tr>
 
<tr>
 
<td align='right'>106</td>
 
<td align='right'></td>
 
<td>Deadyawn</td>
 
<td align='right'>3345</td>
 
<td></td>
 
</tr>
 
</table>
 
<table class='zoo'>
 
<caption>dept</caption>
 
<tr>
 
<th>id</th>
 
<th>name</th>
 
</tr>
 
<tr>
 
<td align='right'>1</td>
 
<td>Computing</td>
 
</tr>
 
<tr>
 
<td align='right'>2</td>
 
<td>Design</td>
 
</tr>
 
<tr>
 
<td align='right'>3</td>
 
<td>Engineering</td>
 
</tr>
 
</table>
 
 
</div>
 
</div>
  
<quiz shuffle=none display=simple>
+
<div class=q>Select the correct example of JOINing three tables
{Which of the following uses of JOIN is correct?
+
<syntaxhighlight class=d lang='sql'>
|type="()"}
+
SELECT *
- SELECT teacher.name, dept.name FROM teacher JOIN dept ON (dept = id)
+
  FROM actor JOIN casting BY actor.id = actorid
- SELECT teacher.name, dept.name FROM teacher, dept INNER JOIN ON (teacher.dept = dept.id)
+
JOIN movie BY movie.id = movieid </syntaxhighlight>
- SELECT teacher.name, dept.name FROM teacher, dept JOIN WHERE(teacher.dept = dept.id)
+
<syntaxhighlight class=d lang='sql'>
- SELECT teacher.name, dept.name FROM teacher OUTER JOIN dept ON dept.id
+
SELECT *
+ SELECT teacher.name, dept.name FROM teacher LEFT OUTER JOIN dept ON (teacher.dept > dept.id)
+
  FROM actor JOIN casting ON actor.id = actorid
 +
  AND JOIN movie ON movie.id = movieid </syntaxhighlight>
 +
<syntaxhighlight class=d lang='sql'>
 +
SELECT *
 +
  FROM actor JOIN casting
 +
  JOIN movie ON actor.id = actorid
 +
  AND movie.id = movieid </syntaxhighlight>
 +
<syntaxhighlight class=d lang='sql'>
 +
SELECT *
 +
  FROM actor JOIN casting ON actor.id = actorid
 +
  AND movie ON movie.id = movieid </syntaxhighlight>
 +
<syntaxhighlight class='d y' lang='sql'>
 +
SELECT *
 +
  FROM actor JOIN casting ON actor.id = actorid
 +
  JOIN movie ON movie.id = movieid </syntaxhighlight>
 +
</div>
 +
 
 +
<div class=q>Select the statement that shows the list of actors called 'John' by order of number of movies in which they acted
 +
<syntaxhighlight class=d lang='sql'>
 +
SELECT name, COUNT(movieid)
 +
  FROM actor JOIN casting ON actorid=actor.id
 +
WHERE name IN 'John %'
 +
GROUP BY name ORDER BY 2 </syntaxhighlight>
 +
<syntaxhighlight class=d lang='sql'>
 +
SELECT name, COUNT(movieid)
 +
  FROM actor JOIN casting ON actorid=actor.id
 +
WHERE name LIKE 'J%'
 +
GROUP BY name ORDER BY 2 DESC </syntaxhighlight>
 +
<syntaxhighlight class='d y' lang='sql'>
 +
SELECT name, COUNT(movieid)
 +
  FROM casting JOIN actor ON actorid=actor.id
 +
WHERE name LIKE 'John %'
 +
GROUP BY name ORDER BY 2 DESC </syntaxhighlight>
 +
<syntaxhighlight class=d lang='sql'>
 +
SELECT name, COUNT(movieid)
 +
  FROM casting JOIN actor
 +
WHERE (actorid ON actor.id)
 +
  AND name LIKE 'John %'
 +
GROUP BY name ORDER BY 2 DESC </syntaxhighlight>
 +
<syntaxhighlight class=d lang='sql'>
 +
SELECT name, COUNT(movieid)
 +
  FROM casting JOIN actor
 +
WHERE name LIKE 'John %'
 +
GROUP BY name ORDER BY COUNT(movieid) DESC </syntaxhighlight>
 +
</div>
 +
 
 +
<div class=q>Select the result that would be obtained from the following code:
 +
<syntaxhighlight lang='sql'>
 +
SELECT title
 +
  FROM movie JOIN casting ON (movieid=movie.id)
 +
              JOIN actor  ON (actorid=actor.id)
 +
  WHERE name='Paul Hogan' AND ord = 1
 +
</syntaxhighlight>
 +
<table class=d><caption>Table-A</caption><tr><td>"Crocodile" Dundee</td><td>1</td></tr><tr><td>Crocodile Dundee in Los Angeles</td><td>1</td></tr><tr><td>Flipper</td><td>1</td></tr><tr><td>Lightning Jack</td><td>1</td></tr></table>
 +
<table class='d y'><caption>Table-B</caption><tr><td>"Crocodile" Dundee</td></tr><tr><td>Crocodile Dundee in Los Angeles</td></tr><tr><td>Flipper</td></tr><tr><td>Lightning Jack</td></tr></table>
 +
<table class=d><caption>Table-C</caption><tr><td>"Crocodile" Dundee</td></tr><tr><td>Paul Hogan</td></tr><tr><td>1</td></tr></table>
 +
<table class=d><caption>Table-D</caption><tr><td>"Crocodile" Dundee</td><td>Paul Hogan</td><td>1</td></tr><tr><td>Crocodile Dundee in Los Angeles</td><td>Paul Hogan</td><td>1</td></tr><tr><td>Flipper</td><td>Paul Hogan</td><td>1</td></tr><tr><td>Lightning Jack</td><td>Paul Hogan</td><td>1</td></tr></table>
 +
<table class=d><caption>Table-E</caption><tr><td>"Crocodile" Dundee</td><td>Paul Hogan</td></tr><tr><td>Crocodile Dundee in Los Angeles</td><td>Paul Hogan</td></tr><tr><td>Flipper</td><td>Paul Hogan</td></tr><tr><td>Lightning Jack</td><td>Paul Hogan</td></tr></table>
 +
</div>
  
{Select the correct statement that shows the name of department which employs Cutflower
+
<div class=q>Select the statement that lists all the actors that starred in movies directed by Ridley Scott who has id 351
|type="()"}
+
<syntaxhighlight class=d lang='sql'>
- SELECT dept.name FROM teacher JOIN dept ON (dept.id = teacher.dept) WHERE dept.id = (SELECT dept FROM teacher HAVING name = 'Cutflower')
+
SELECT name
- SELECT dept.name FROM teacher JOIN dept WHERE dept.id = (SELECT dept FROM teacher WHERE name = 'Cutflower')
+
  FROM movie JOIN casting
- SELECT dept.name FROM teacher JOIN dept ON (dept.id = (SELECT dept FROM teacher WHERE name = 'Cutflower'))
+
  AND actor ON movie.id = movieid
- SELECT dept.name FROM teacher JOIN dept ON (dept.id = teacher.dept) WHERE teacher.name = 'Cutflower'
+
  AND actor.id = actorid
- SELECT name FROM teacher JOIN dept ON (id = dept) WHERE id = (SELECT dept FROM teacher WHERE name = 'Cutflower')
+
WHERE ord = 1
 +
  AND actor = 351 </syntaxhighlight>
 +
<syntaxhighlight class=d lang='sql'>
 +
SELECT name
 +
  FROM movie JOIN casting
 +
  JOIN actor ON movie.id = movieid
 +
    OR actor.id = actorid
 +
WHERE ord = 1 AND director = 351 </syntaxhighlight>
 +
<syntaxhighlight class=d lang='sql'>
 +
SELECT name
 +
  FROM movie JOIN casting ON movie.id = movieid
 +
  JOIN actor ON actor.id = actorid
 +
WHERE ord = 1 AND actorid = 351 </syntaxhighlight>
 +
<syntaxhighlight class='d y' lang='sql'>
 +
SELECT name
 +
  FROM movie JOIN casting ON movie.id = movieid
 +
  JOIN actor ON actor.id = actorid
 +
WHERE ord = 1 AND director = 351 </syntaxhighlight>
 +
<syntaxhighlight class=d lang='sql'>
 +
SELECT name
 +
  FROM movie JOIN casting ON movie.id = actorid
 +
  JOIN actor ON actor.id = movieid
 +
WHERE director = 351 </syntaxhighlight>
 +
</div>
  
 +
<div class=q>There are two sensible ways to connect movie and actor. They are:
 +
<div class=d><ul>
 +
  <li>link the director column in movies with the id column in actor</li>
 +
  <li>join casting to itself</li>
 +
</ul></div>
 +
<div class=d><ul>
 +
  <li>link the actor column in movies with the primary key in actor</li>
 +
  <li>connect the primary keys of movie and actor via the casting table</li>
 +
</ul></div>
 +
<div class='d y'><ul>
 +
  <li>link the director column in movies with the primary key in actor</li>
 +
  <li>connect the primary keys of movie and actor via the casting table</li>
 +
</ul></div>
 +
<div class=d><ul>
 +
  <li>link the director column in movies with the primary key in actor</li>
 +
  <li>connect the primary keys of movie and casting via the actor table</li>
 +
</ul></div>
 +
<div class=d><ul>
 +
  <li>link the movie column in actor with the director column in actor</li>
 +
  <li>connect movie and actor via the casting table</li>
 +
</ul></div>
 +
</div>
  
</quiz>
+
<div class=q>Select the result that would be obtained from the following code:
 +
<syntaxhighlight lang='sql'>
 +
SELECT title, yr
 +
  FROM movie, casting, actor
 +
  WHERE name='Robert De Niro' AND movieid=movie.id AND actorid=actor.id AND ord = 3
 +
</syntaxhighlight>
 +
<table  class=d><caption>Table-A</caption><tr><td>A Bronx Tale</td><td>1993</td><td>3</td></tr><tr><td>Bang the Drum Slowly</td><td>1973</td><td>3</td></tr><tr><td>Limitless</td><td>2011</td><td>3</td></tr></table>
 +
<table class='d y'><caption>Table-B</caption><tr><td>A Bronx Tale</td><td>1993</td></tr><tr><td>Bang the Drum Slowly</td><td>1973</td></tr><tr><td>Limitless</td><td>2011</td></tr></table>
 +
<table class=d><caption>Table-C</caption><tr><td>A Bronx Tale</td><td>3</td></tr><tr><td>Bang the Drum Slowly</td><td>3</td></tr><tr><td>Limitless</td><td>3</td></tr></table>
 +
<table class=d><caption>Table-D</caption><tr><td>A Bronx Tale</td></tr><tr><td>Bang the Drum Slowly</td></tr><tr><td>Limitless</td></tr></table>
 +
<table class=d><caption>Table-E</caption><tr><td>A Bronx Tale</td><td>Robert De Niro</td><td>1993</td></tr><tr><td>Bang the Drum Slowly</td><td>Robert De Niro</td><td>1973</td></tr><tr><td>Limitless</td><td>Robert De Niro</td><td>2011</td></tr></table>
 +
</div>
  
 
[[Category:Quizzes]]
 
[[Category:Quizzes]]

Latest revision as of 15:51, 30 September 2016

JOIN Quiz - part 2

Select the statement which lists the unfortunate directors of the movies which have caused financial loses (gross < budget)
SELECT JOIN(name FROM actor, movie
       ON actor.id:director WHERE gross < budget)
 GROUP BY name
SELECT name
 FROM actor INNER JOIN movie BY actor.id = director
 HAVING gross < budget
SELECT name
  FROM actor INNER JOIN movie ON actor.id = director
 WHERE gross < budget
SELECT name
  FROM actor INNER JOIN movie ON actor.id:director
 WHERE gross < budget
SELECT name
  FROM director INNER JOIN movie ON movie.id = director.id
 WHERE gross < budget
Select the correct example of JOINing three tables
SELECT * 
  FROM actor JOIN casting BY actor.id = actorid
 JOIN movie BY movie.id = movieid
SELECT *
  FROM actor JOIN casting ON actor.id = actorid
   AND JOIN movie ON movie.id = movieid
SELECT *
  FROM actor JOIN casting
  JOIN movie ON actor.id = actorid
   AND movie.id = movieid
SELECT *
  FROM actor JOIN casting ON actor.id = actorid
   AND movie ON movie.id = movieid
SELECT *
  FROM actor JOIN casting ON actor.id = actorid
  JOIN movie ON movie.id = movieid
Select the statement that shows the list of actors called 'John' by order of number of movies in which they acted
SELECT name, COUNT(movieid)
  FROM actor JOIN casting ON actorid=actor.id
 WHERE name IN 'John %'
 GROUP BY name ORDER BY 2
SELECT name, COUNT(movieid)
  FROM actor JOIN casting ON actorid=actor.id
 WHERE name LIKE 'J%'
 GROUP BY name ORDER BY 2 DESC
SELECT name, COUNT(movieid)
  FROM casting JOIN actor ON actorid=actor.id
 WHERE name LIKE 'John %'
 GROUP BY name ORDER BY 2 DESC
SELECT name, COUNT(movieid)
  FROM casting JOIN actor
 WHERE (actorid ON actor.id)
   AND name LIKE 'John %'
 GROUP BY name ORDER BY 2 DESC
SELECT name, COUNT(movieid)
  FROM casting JOIN actor
 WHERE name LIKE 'John %'
 GROUP BY name ORDER BY COUNT(movieid) DESC
Select the result that would be obtained from the following code:
 SELECT title 
   FROM movie JOIN casting ON (movieid=movie.id)
              JOIN actor   ON (actorid=actor.id)
  WHERE name='Paul Hogan' AND ord = 1
Table-A
"Crocodile" Dundee1
Crocodile Dundee in Los Angeles1
Flipper1
Lightning Jack1
Table-B
"Crocodile" Dundee
Crocodile Dundee in Los Angeles
Flipper
Lightning Jack
Table-C
"Crocodile" Dundee
Paul Hogan
1
Table-D
"Crocodile" DundeePaul Hogan1
Crocodile Dundee in Los AngelesPaul Hogan1
FlipperPaul Hogan1
Lightning JackPaul Hogan1
Table-E
"Crocodile" DundeePaul Hogan
Crocodile Dundee in Los AngelesPaul Hogan
FlipperPaul Hogan
Lightning JackPaul Hogan
Select the statement that lists all the actors that starred in movies directed by Ridley Scott who has id 351
SELECT name
  FROM movie JOIN casting
   AND actor ON movie.id = movieid
   AND actor.id = actorid
 WHERE ord = 1
  AND actor = 351
SELECT name
  FROM movie JOIN casting
  JOIN actor ON movie.id = movieid
    OR actor.id = actorid
 WHERE ord = 1 AND director = 351
SELECT name
  FROM movie JOIN casting ON movie.id = movieid
  JOIN actor ON actor.id = actorid
 WHERE ord = 1 AND actorid = 351
SELECT name
  FROM movie JOIN casting ON movie.id = movieid
  JOIN actor ON actor.id = actorid
WHERE ord = 1 AND director = 351
SELECT name
  FROM movie JOIN casting ON movie.id = actorid
  JOIN actor ON actor.id = movieid
 WHERE director = 351
There are two sensible ways to connect movie and actor. They are:
  • link the director column in movies with the id column in actor
  • join casting to itself
  • link the actor column in movies with the primary key in actor
  • connect the primary keys of movie and actor via the casting table
  • link the director column in movies with the primary key in actor
  • connect the primary keys of movie and actor via the casting table
  • link the director column in movies with the primary key in actor
  • connect the primary keys of movie and casting via the actor table
  • link the movie column in actor with the director column in actor
  • connect movie and actor via the casting table
Select the result that would be obtained from the following code:
 SELECT title, yr 
   FROM movie, casting, actor 
  WHERE name='Robert De Niro' AND movieid=movie.id AND actorid=actor.id AND ord = 3
Table-A
A Bronx Tale19933
Bang the Drum Slowly19733
Limitless20113
Table-B
A Bronx Tale1993
Bang the Drum Slowly1973
Limitless2011
Table-C
A Bronx Tale3
Bang the Drum Slowly3
Limitless3
Table-D
A Bronx Tale
Bang the Drum Slowly
Limitless
Table-E
A Bronx TaleRobert De Niro1993
Bang the Drum SlowlyRobert De Niro1973
LimitlessRobert De Niro2011