Difference between revisions of "JOIN Quiz"

From SQLZOO
Jump to: navigation, search
m (Added distinct to question 4)
 
(10 intermediate revisions by 3 users not shown)
Line 112: Line 112:
 
</table>
 
</table>
 
</div>
 
</div>
<quiz shuffle=none display=simple>
+
<div class=quiz>
{You want to find the stadium where player 'Dimitris Salpingidis' scored. Select the JOIN condition to use:
+
<div class=q>You want to find the stadium where player 'Dimitris Salpingidis' scored. Select the JOIN condition to use:
|type="()"}
+
<syntaxhighlight class=d lang='sql'> eteam JOIN game ON (id=team1) </syntaxhighlight>
- <syntaxhighlight lang='sql'> eteam JOIN game ON (id=team1) </syntaxhighlight>
+
<syntaxhighlight class=d lang='sql'> eteam JOIN game ON (id=team2) </syntaxhighlight>
- <syntaxhighlight lang='sql'> eteam JOIN game ON (id=team2) </syntaxhighlight>
+
<syntaxhighlight class=d lang='sql'> eteam JOIN goal ON (teamid=id) </syntaxhighlight>
- <syntaxhighlight lang='sql'> eteam JOIN goal ON (teamid=id) </syntaxhighlight>
+
<syntaxhighlight class='d y' lang='sql'> game  JOIN goal ON (id=matchid) </syntaxhighlight>
+ <syntaxhighlight lang='sql'> game  JOIN goal ON (id=matchid) </syntaxhighlight>
+
<syntaxhighlight class=d lang='sql'> game  JOIN goal ON (team1=teamid OR team2=teamid) </syntaxhighlight>
- <syntaxhighlight lang='sql'> game  JOIN goal ON (team1=teamid OR team2=teamid) </syntaxhighlight>
+
</div>
  
{You JOIN the tables '''goal''' and '''eteam''' in an SQL statement. Indicate the list of column names that may be used in the SELECT line:
+
<div class=q>You JOIN the tables '''goal''' and '''eteam''' in an SQL statement. Indicate the list of column names that may be used in the SELECT line:
|type="()"}
+
<syntaxhighlight class=d lang='sql'> gtime, mdate, stadium, matchid </syntaxhighlight>
- <syntaxhighlight lang='sql'> gtime, mdate, stadium, match.id </syntaxhighlight>
+
<syntaxhighlight class=d lang='sql'> mdate, stadium, id </syntaxhighlight>
- <syntaxhighlight lang='sql'> mdate, stadium, id </syntaxhighlight>
+
<syntaxhighlight class='d y' lang='sql'> matchid, teamid, player, gtime, id, teamname, coach </syntaxhighlight>
+ <syntaxhighlight lang='sql'> matchid, teamid, player, gtime, id, teamname, coach </syntaxhighlight>
+
<syntaxhighlight class=d lang='sql'> matchid, teamid, player, gtime, mdate, stadium, team1 </syntaxhighlight>
- <syntaxhighlight lang='sql'> matchid, teamid, player, gtime, mdate, stadium, team1 </syntaxhighlight>
+
<syntaxhighlight class=d lang='sql'> stadium, team1, team2 </syntaxhighlight>
- <syntaxhighlight lang='sql'> stadium, team1, team2 </syntaxhighlight>
+
</div>
  
{Select the code which shows players, their team and the amount of goals they scored against Greece(GRE).
+
<div class=q>Select the code which shows players, their team and the amount of goals they scored against Greece(GRE).
|type="()"}
+
<syntaxhighlight class='d y' lang='sql'>
+ <syntaxhighlight lang='sql'> SELECT player, COUNT(*), teamid FROM game JOIN goal ON matchid = id WHERE (team1 = "GRE" OR team2 = "GRE") AND teamid != 'GRE' GROUP BY player </syntaxhighlight>
+
SELECT player, teamid, COUNT(*)
- <syntaxhighlight lang='sql'> SELECT player, COUNT(*), teamid FROM game JOIN goal ON matchid = id WHERE (team1 = "GRE") AND teamid != 'GRE' GROUP BY player </syntaxhighlight>
+
  FROM game JOIN goal ON matchid = id
- <syntaxhighlight lang='sql'> SELECT player, COUNT(*), teamid FROM game JOIN goal ON matchid = id WHERE (team1 = "POL" OR team2 = "POL") AND teamid != 'POL' GROUP BY player </syntaxhighlight>
+
WHERE (team1 = "GRE" OR team2 = "GRE")
- <syntaxhighlight lang='sql'> SELECT player, COUNT(*), teamid FROM game JOIN goal WITH matchid = id WHERE (team1 = "GRE" OR team2 = "GRE") AND teamid != 'GRE' GROUP BY player </syntaxhighlight>
+
  AND teamid != 'GRE'
- <syntaxhighlight lang='sql'> SELECT player, teamid FROM game JOIN goal ON matchid = id WHERE (team1 = "GRE" OR team2 = "GRE") AND teamid != 'GRE' GROUP BY player </syntaxhighlight>
+
GROUP BY player, teamid </syntaxhighlight>
 +
<syntaxhighlight class=d lang='sql'>
 +
SELECT player, teamid, COUNT(*)
 +
  FROM game JOIN goal ON matchid = id
 +
WHERE (team1 = "GRE") AND teamid != 'GRE'
 +
GROUP BY player, teamid </syntaxhighlight>
 +
<syntaxhighlight class=d lang='sql'>
 +
SELECT player, teamid, COUNT(*)
 +
  FROM game JOIN goal ON matchid = id
 +
WHERE (team1 = "POL" OR team2 = "POL")
 +
  AND teamid != 'POL'
 +
GROUP BY player, teamid </syntaxhighlight>
 +
<syntaxhighlight class=d lang='sql'>
 +
SELECT player, teamid, COUNT(*)
 +
  FROM game JOIN goal WITH matchid = id
 +
WHERE (team1 = "GRE" OR team2 = "GRE")
 +
  AND teamid != 'GRE'
 +
GROUP BY player, teamid </syntaxhighlight>
 +
<syntaxhighlight class=d lang='sql'>
 +
SELECT player, teamid
 +
  FROM game JOIN goal ON matchid = id
 +
WHERE (team1 = "GRE" OR team2 = "GRE")
 +
  AND teamid != 'GRE'
 +
GROUP BY player, teamid </syntaxhighlight>
 +
</div>
  
{Select the result that would be obtained from this code: SELECT teamid, mdate FROM goal JOIN game on (matchid=id) WHERE mdate = '9 June 2012'
+
<div class=q>Select the result that would be obtained from this code: <source lang=sql>
<table style='float:left'><caption>Table-A</caption><tr><td>DEN</td><td>9 June 2012</td></tr><tr><td>GER</td><td>9 June 2012</td></tr></table>
+
SELECT DISTINCT teamid, mdate
<table style='float:left'><caption>Table-B</caption><tr><td>DEN</td></tr><tr><td>GER</td></tr></table>
+
  FROM goal JOIN game on (matchid=id)
<table style='float:left'><caption>Table-C</caption><tr><td>GRE</td><td>12 June 2012</td></tr><tr><td>CZE</td><td>12 June 2012</td></tr><tr><td>POL</td><td>12 June 2012</td></tr><tr><td>RUS</td><td>12 June 2012</td></tr></table>
+
WHERE mdate = '9 June 2012'</source>
<table style='float:left'><caption>Table-D</caption><tr><td>GRE</td></tr><tr><td>CZE</td></tr><tr><td>POL</td></tr><tr><td>RUS</td></tr></table>
+
<table class='d y'><tr><td>DEN</td><td>9 June 2012</td></tr><tr><td>GER</td><td>9 June 2012</td></tr></table>
<table style='float:left'><caption>Table-E</caption><tr><td>POL</td><td>8 June 2012</td></tr><tr><td>GRE</td><td>8 June 2012</td></tr><tr><td>RUS</td><td>8 June 2012</td></tr><tr><td>CZE</td><td>8 June 2012</td></tr></table>
+
<table class=d><tr><td>DEN</td></tr><tr><td>GER</td></tr></table>
|type="()"}
+
<table class=d><tr><td>DEN</td><td>9 June 2012</td></tr><tr><td>DEN</td><td>9 June 2012</td></tr><tr><td>POL</td><td>9 June 2012</td></tr><tr><td>RUS</td><td>9 June 2012</td></tr></table>
+ Table-A
+
<table class=d><tr><td>GRE</td></tr><tr><td>CZE</td></tr><tr><td>POL</td></tr><tr><td>RUS</td></tr></table>
- Table-B
+
<table class=d><tr><td>RUS</td><td>9 June 2012</td></tr><tr><td>GRE</td><td>9 June 2012</td></tr><tr><td>RUS</td><td>9 June 2012</td></tr><tr><td>CZE</td><td>9 June 2012</td></tr></table>
- Table-C
+
</div>
- Table-D
 
- Table-E
 
  
{Select the code which would show the player and their team for those who have scored against Poland(POL) in National Stadium, Warsaw.
+
<div class=q>Select the code which would show the player and their team for those who have scored against Poland(POL) in National Stadium, Warsaw.
  
<caption>Query-A<syntaxhighlight lang='sql'>  
+
<syntaxhighlight class=d lang='sql'>
 
   SELECT DISTINCT player, teamid  
 
   SELECT DISTINCT player, teamid  
 
   FROM game JOIN goal ON matchid = id  
 
   FROM game JOIN goal ON matchid = id  
   WHERE stadium = 'National Stadium, Warsaw' AND mdate IN
+
   WHERE stadium = 'National Stadium, Warsaw'  
  (SELECT mdate
+
AND (team1 = 'GER' OR team2 = 'GER')
    FROM game
+
   AND teamid != 'GER' </syntaxhighlight>
    WHERE team1 = 'GER' OR team2 = 'GER')  
+
<syntaxhighlight class='d y' lang='sql'>
   AND teamid != 'GER' </syntaxhighlight></caption>
 
<caption>Query-B<syntaxhighlight lang='sql'>
 
 
   SELECT DISTINCT player, teamid  
 
   SELECT DISTINCT player, teamid  
 
   FROM game JOIN goal ON matchid = id  
 
   FROM game JOIN goal ON matchid = id  
   WHERE stadium = 'National Stadium, Warsaw' AND mdate IN
+
   WHERE stadium = 'National Stadium, Warsaw'  
    (SELECT mdate
+
AND (team1 = 'POL' OR team2 = 'POL')
      FROM game
+
   AND teamid != 'POL' </syntaxhighlight>
    WHERE team1 = 'POL' OR team2 = 'POL')
+
<syntaxhighlight class=d lang='sql'>  
   AND teamid != 'POL' </syntaxhighlight></caption>
 
<caption>Query-C<syntaxhighlight lang='sql'>  
 
 
  SELECT DISTINCT player, teamid  
 
  SELECT DISTINCT player, teamid  
 
   FROM game JOIN goal ON matchid = id  
 
   FROM game JOIN goal ON matchid = id  
   WHERE stadium = 'National Stadium, Warsaw' AND teamid != 'POL' </syntaxhighlight></caption>
+
   WHERE stadium = 'National Stadium, Warsaw' AND teamid != 'POL' </syntaxhighlight>
<caption>Query-D<syntaxhighlight lang='sql'>  
+
<syntaxhighlight class=d lang='sql'>  
 
  SELECT DISTINCT player, teamid  
 
  SELECT DISTINCT player, teamid  
 
   FROM game JOIN goal ON matchid = id  
 
   FROM game JOIN goal ON matchid = id  
   WHERE stadium = 'Stadion Miejski (Wroclaw)' AND mdate IN
+
   WHERE stadium = 'Stadion Miejski (Wroclaw)'  
    (SELECT mdate
+
AND (team1 = 'POL' OR team2 = 'POL')
      FROM game
+
   AND teamid != 'POL' </syntaxhighlight>
    WHERE team1 = 'POL' OR team2 = 'POL')
+
<syntaxhighlight class=d lang='sql'>  
   AND teamid != 'POL' </syntaxhighlight></caption>
 
<caption>Query-E<syntaxhighlight lang='sql'>  
 
 
  SELECT DISTINCT stadium, mdate  
 
  SELECT DISTINCT stadium, mdate  
 
   FROM game JOIN goal ON matchid = id  
 
   FROM game JOIN goal ON matchid = id  
   WHERE stadium = 'National Stadium, Warsaw' AND mdate IN
+
   WHERE stadium = 'National Stadium, Warsaw'  
    (SELECT mdate
+
AND (team1 = 'POL' OR team2 = 'POL')
      FROM game
+
   AND teamid != 'POL' </syntaxhighlight>
      WHERE team1 = 'POL' OR team2 = 'POL')
+
</div>
   AND teamid != 'POL' </syntaxhighlight></caption>
 
|type="()"}
 
- Query-A
 
+ Query-B
 
- Query-C
 
- Query-D
 
- Query-E
 
  
{Select the code which shows the player, their team and the time they scored, for players who have played in Stadion Miejski (Wroclaw) but not against Italy(ITA).
+
<div class=q>Select the code which shows the player, their team and the time they scored, for players who have played in Stadion Miejski (Wroclaw) but not against Italy(ITA).
|type="()"}
+
<syntaxhighlight class=d lang='sql'>
- <syntaxhighlight lang='sql'> SELECT DISTINCT player, teamid, gtime FROM game JOIN goal ON matchid = id WHERE stadium = 'National Stadium, Warsaw' AND (team1 != 'ITA' AND team2 !='ITA') </syntaxhighlight>
+
SELECT DISTINCT player, teamid, gtime
- <syntaxhighlight lang='sql'> SELECT DISTINCT player, teamid, gtime FROM game JOIN goal ON matchid = id WHERE stadium = 'Stadion Miejski (Wroclaw)' AND (team1 != 'ESP' AND team2 !='ESP') </syntaxhighlight>
+
  FROM game JOIN goal ON matchid = id
+ <syntaxhighlight lang='sql'> SELECT DISTINCT player, teamid, gtime FROM game JOIN goal ON matchid = id WHERE stadium = 'Stadion Miejski (Wroclaw)' AND (team1 != 'ITA' AND team2 !='ITA') </syntaxhighlight>
+
WHERE stadium = 'National Stadium, Warsaw'
- <syntaxhighlight lang='sql'> SELECT DISTINCT teamid, gtime FROM game JOIN goal ON matchid = id WHERE stadium = 'Stadion Miejski (Wroclaw)' AND (team1 != 'ITA' AND team2 !='ITA') </syntaxhighlight>
+
  AND (( teamid = team2 AND team1 != 'ITA') OR ( teamid = team1 AND team2 != 'ITA'))
- <syntaxhighlight lang='sql'> SELECT DISTINCT player, teamid, gtime FROM game JOIN goal ON matchid = id WHERE team1 != 'ITA' AND team2 !='ITA' </syntaxhighlight>
+
</syntaxhighlight>
 +
<syntaxhighlight class=d lang='sql'>
 +
SELECT DISTINCT player, teamid, gtime
 +
  FROM game JOIN goal ON matchid = id
 +
WHERE stadium = 'Stadion Miejski (Wroclaw)'
 +
  AND (( teamid = team2 AND team1 != 'ESP') OR ( teamid = team1 AND team2 != 'ESP'))
 +
</syntaxhighlight>
 +
<syntaxhighlight class='d y' lang='sql'>
 +
SELECT DISTINCT player, teamid, gtime
 +
  FROM game JOIN goal ON matchid = id
 +
WHERE stadium = 'Stadion Miejski (Wroclaw)'
 +
  AND (( teamid = team2 AND team1 != 'ITA') OR ( teamid = team1 AND team2 != 'ITA'))
 +
</syntaxhighlight>
 +
<syntaxhighlight class=d lang='sql'>
 +
SELECT DISTINCT teamid, gtime
 +
  FROM game JOIN goal ON matchid = id
 +
WHERE stadium = 'Stadion Miejski (Wroclaw)'
 +
  AND (( teamid = team2 AND team1 != 'ITA') OR ( teamid = team1 AND team2 != 'ITA'))
 +
</syntaxhighlight>
 +
<syntaxhighlight class=d lang='sql'>
 +
SELECT DISTINCT player, teamid, gtime
 +
  FROM game JOIN goal ON matchid = id
 +
WHERE team1 != 'ITA' AND team2 !='ITA' </syntaxhighlight>
 +
</div>
  
{Select the result that would be obtained from this code: SELECT teamname, COUNT(*) FROM eteam JOIN goal ON teamid = id GROUP BY teamname HAVING COUNT(*) < 3
+
<div class=q>Select the result that would be obtained from this code: <source lang=sql>
<table style='float:left'><caption>Table-A</caption><tr><td>2</td></tr><tr><td>2</td></tr><tr><td>1</td></tr><tr><td>2</td></tr></table>
+
SELECT teamname, COUNT(*)
<table style='float:left'><caption>Table-B</caption><tr><td>Netherlands</td><td>2</td></tr><tr><td>Poland</td><td>2</td></tr><tr><td>Republic of Ireland</td><td>1</td></tr><tr><td>Ukraine</td><td>2</td></tr></table>
+
  FROM eteam JOIN goal ON teamid = id
<table style='float:left'><caption>Table-C</caption><tr><td>Netherlands</td></tr><tr><td>Poland</td></tr><tr><td>Republic of Ireland</td></tr><tr><td>Ukraine</td></tr></table>
+
GROUP BY teamname
<table style='float:left'><caption>Table-D</caption><tr><td>Poland</td><td>76</td></tr></table>
+
HAVING COUNT(*) < 3</source>
<table style='float:left'><caption>Table-E</caption><tr><td>Republic of Ireland</td><td>1</td></tr></table>
+
<table class=d><tr><td>2</td></tr><tr><td>2</td></tr><tr><td>1</td></tr><tr><td>2</td></tr></table>
|type="()"}
+
<table class='d y'><tr><td>Netherlands</td><td>2</td></tr><tr><td>Poland</td><td>2</td></tr><tr><td>Republic of Ireland</td><td>1</td></tr><tr><td>Ukraine</td><td>2</td></tr></table>
- Table-A
+
<table class=d><tr><td>Netherlands</td></tr><tr><td>Poland</td></tr><tr><td>Republic of Ireland</td></tr><tr><td>Ukraine</td></tr></table>
+ Table-B
+
<table class=d><tr><td>Poland</td><td>76</td></tr></table>
- Table-C
+
<table class=d><tr><td>Republic of Ireland</td><td>1</td></tr></table>
- Table-D
 
- Table-E
 
  
</quiz>
+
</div>
  
 
[[Category:Quizzes]]
 
[[Category:Quizzes]]

Latest revision as of 14:44, 30 September 2016

JOIN quiz

game
id mdate stadium team1 team2
1001 8 June 2012 National Stadium, Warsaw POL GRE
1002 8 June 2012 Stadion Miejski (Wroclaw) RUS CZE
1003 12 June 2012 Stadion Miejski (Wroclaw) GRE CZE
1004 12 June 2012 National Stadium, Warsaw POL RUS
...
goal
matchid teamid player gtime
1001 POL Robert Lewandowski 17
1001 GRE Dimitris Salpingidis 51
1002 RUS Alan Dzagoev 15
1001 RUS Roman Pavlyuchenko 82
...
eteam
id teamname coach
POL Poland Franciszek Smuda
RUS Russia Dick Advocaat
CZE Czech Republic Michal Bilek
GRE Greece Fernando Santos
...
You want to find the stadium where player 'Dimitris Salpingidis' scored. Select the JOIN condition to use:
 eteam JOIN game ON (id=team1)
 eteam JOIN game ON (id=team2)
 eteam JOIN goal ON (teamid=id)
 game  JOIN goal ON (id=matchid)
 game  JOIN goal ON (team1=teamid OR team2=teamid)
You JOIN the tables goal and eteam in an SQL statement. Indicate the list of column names that may be used in the SELECT line:
 gtime, mdate, stadium, matchid
 mdate, stadium, id
 matchid, teamid, player, gtime, id, teamname, coach
 matchid, teamid, player, gtime, mdate, stadium, team1
 stadium, team1, team2
Select the code which shows players, their team and the amount of goals they scored against Greece(GRE).
SELECT player, teamid, COUNT(*)
  FROM game JOIN goal ON matchid = id
 WHERE (team1 = "GRE" OR team2 = "GRE")
   AND teamid != 'GRE'
 GROUP BY player, teamid
SELECT player, teamid, COUNT(*)
  FROM game JOIN goal ON matchid = id
 WHERE (team1 = "GRE") AND teamid != 'GRE'
 GROUP BY player, teamid
SELECT player, teamid, COUNT(*)
  FROM game JOIN goal ON matchid = id
 WHERE (team1 = "POL" OR team2 = "POL")
   AND teamid != 'POL'
 GROUP BY player, teamid
SELECT player, teamid, COUNT(*)
  FROM game JOIN goal WITH matchid = id
 WHERE (team1 = "GRE" OR team2 = "GRE")
   AND teamid != 'GRE'
 GROUP BY player, teamid
SELECT player, teamid
  FROM game JOIN goal ON matchid = id
 WHERE (team1 = "GRE" OR team2 = "GRE")
   AND teamid != 'GRE'
 GROUP BY player, teamid
Select the result that would be obtained from this code:
SELECT DISTINCT teamid, mdate
  FROM goal JOIN game on (matchid=id)
 WHERE mdate = '9 June 2012'
DEN9 June 2012
GER9 June 2012
DEN
GER
DEN9 June 2012
DEN9 June 2012
POL9 June 2012
RUS9 June 2012
GRE
CZE
POL
RUS
RUS9 June 2012
GRE9 June 2012
RUS9 June 2012
CZE9 June 2012
Select the code which would show the player and their team for those who have scored against Poland(POL) in National Stadium, Warsaw.
  SELECT DISTINCT player, teamid 
  FROM game JOIN goal ON matchid = id 
  WHERE stadium = 'National Stadium, Warsaw' 
 AND (team1 = 'GER' OR team2 = 'GER')
   AND teamid != 'GER'
  SELECT DISTINCT player, teamid 
   FROM game JOIN goal ON matchid = id 
  WHERE stadium = 'National Stadium, Warsaw' 
 AND (team1 = 'POL' OR team2 = 'POL')
   AND teamid != 'POL'
 
 SELECT DISTINCT player, teamid 
   FROM game JOIN goal ON matchid = id 
  WHERE stadium = 'National Stadium, Warsaw' AND teamid != 'POL'
 
 SELECT DISTINCT player, teamid 
   FROM game JOIN goal ON matchid = id 
  WHERE stadium = 'Stadion Miejski (Wroclaw)' 
 AND (team1 = 'POL' OR team2 = 'POL')
  AND teamid != 'POL'
 
 SELECT DISTINCT stadium, mdate 
   FROM game JOIN goal ON matchid = id 
  WHERE stadium = 'National Stadium, Warsaw' 
 AND (team1 = 'POL' OR team2 = 'POL')
  AND teamid != 'POL'
Select the code which shows the player, their team and the time they scored, for players who have played in Stadion Miejski (Wroclaw) but not against Italy(ITA).
SELECT DISTINCT player, teamid, gtime
  FROM game JOIN goal ON matchid = id
 WHERE stadium = 'National Stadium, Warsaw'
   AND (( teamid = team2 AND team1 != 'ITA') OR ( teamid = team1 AND team2 != 'ITA'))
SELECT DISTINCT player, teamid, gtime
  FROM game JOIN goal ON matchid = id
 WHERE stadium = 'Stadion Miejski (Wroclaw)'
   AND (( teamid = team2 AND team1 != 'ESP') OR ( teamid = team1 AND team2 != 'ESP'))
SELECT DISTINCT player, teamid, gtime
  FROM game JOIN goal ON matchid = id
 WHERE stadium = 'Stadion Miejski (Wroclaw)'
   AND (( teamid = team2 AND team1 != 'ITA') OR ( teamid = team1 AND team2 != 'ITA'))
SELECT DISTINCT teamid, gtime
  FROM game JOIN goal ON matchid = id
 WHERE stadium = 'Stadion Miejski (Wroclaw)'
   AND (( teamid = team2 AND team1 != 'ITA') OR ( teamid = team1 AND team2 != 'ITA'))
SELECT DISTINCT player, teamid, gtime
  FROM game JOIN goal ON matchid = id
 WHERE team1 != 'ITA' AND team2 !='ITA'
Select the result that would be obtained from this code:
SELECT teamname, COUNT(*)
  FROM eteam JOIN goal ON teamid = id
 GROUP BY teamname
HAVING COUNT(*) < 3
2
2
1
2
Netherlands2
Poland2
Republic of Ireland1
Ukraine2
Netherlands
Poland
Republic of Ireland
Ukraine
Poland76
Republic of Ireland1