Difference between revisions of "The JOIN operation"

From SQLZOO
Jump to: navigation, search
m (Q2: "for game 1012" in question, "WHERE id=1023")
(27 intermediate revisions by 3 users not shown)
Line 41: Line 41:
 
</tr>
 
</tr>
 
</table>
 
</table>
 
+
<br/>
 
<table class='db_ref'>
 
<table class='db_ref'>
 
<caption>goal</caption>
 
<caption>goal</caption>
Line 78: Line 78:
 
</tr>
 
</tr>
 
</table>
 
</table>
 
+
<br/>
 
<table class='db_ref'>
 
<table class='db_ref'>
 
<caption>eteam</caption>
 
<caption>eteam</caption>
Line 126: Line 126:
 
    
 
    
 
<div class='qu'>
 
<div class='qu'>
The first example shows the goal scored by 'Wayne Rooney'.
+
The first example shows the goal scored by 'Bender'.
  
<p class='imper'>Show '''matchid''' and '''player''' name for all goals scored by English players.  
+
<p class='imper'>Show '''matchid''' and '''player''' name for all goals scored by Germany.  
<code>teamid = 'ENG'</code></p>
+
<code>teamid = 'GER'</code></p>
  
 
<source lang='sql' class='def'>
 
<source lang='sql' class='def'>
 
SELECT * FROM goal  
 
SELECT * FROM goal  
   WHERE player LIKE '%Rooney'
+
   WHERE player LIKE '%Bender'
 
</source>
 
</source>
  
 
<source lang='sql' class='ans'>
 
<source lang='sql' class='ans'>
 
SELECT matchid, player  
 
SELECT matchid, player  
  FROM game JOIN goal ON matchid = id
+
  FROM goal
   WHERE teamid LIKE 'ENG'
+
   WHERE teamid LIKE 'GER'
 
</source>
 
</source>
 
</div>
 
</div>
  
 
<div class='qu'>
 
<div class='qu'>
This example shows all the games played in Warsaw.
+
From the previous query you can see that Lars Bender's goal was scored in game 1012.
 
+
Notice that the column '''matchid''' in the '''goal''' table corresponds to the '''id''' column in the '''game''' table.
<p class='imper'>Show '''mdate''', '''team1''' and '''team2''' for all matches that Polish ('POL') and Czech ('CZE') teams played in Warsaw.</p>
+
<p class='imper'>Show id, stadium, team1, team2 for game 1012</p>
  
 
<source lang='sql' class='def'>
 
<source lang='sql' class='def'>
SELECT mdate, team1, team2 FROM game  
+
SELECT id,stadium,team1,team2
  WHERE stadium = 'National Stadium, Warsaw'
+
  FROM game  
 +
WHERE stadium LIKE '%Warsaw%'
 
</source>
 
</source>
  
 
<source lang='sql' class='ans'>
 
<source lang='sql' class='ans'>
SELECT mdate, team1, team2 FROM game  
+
SELECT id,stadium,team1,team2
  WHERE stadium = 'National Stadium, Warsaw'
+
FROM game
    AND (team1 = 'POL' OR team1 = 'CZE')
+
WHERE id=1012</source>
</source>
+
 
</div>
 
</div>
  
 
<div class='qu'>
 
<div class='qu'>
This query shows names of coaches from Germany and England.
+
You can combine the two steps into a single query with a join. You will get all the '''game''' details and all the '''goal''' details if you use
 
+
SELECT *
<p class='imper'>Show '''teamname''' and '''coach''' of the teams which have coaches who's names start with 'M'.</p>
+
  FROM game JOIN goal ON (id=matchid)
 +
<p class='imper'>Show the player, teamid and mdate and for every German goal. <code>teamid='GER'</code></p>
  
 
<source lang='sql' class='def'>
 
<source lang='sql' class='def'>
SELECT coach from eteam
+
SELECT player,stadium
   WHERE teamname = 'Germany'
+
   FROM game JOIN goal ON (id=matchid)
    OR teamname = 'England'
+
 
</source>
 
</source>
  
 
<source lang='sql' class='ans'>
 
<source lang='sql' class='ans'>
SELECT teamname, coach from eteam
+
SELECT player,teamid,mdate
   WHERE coach LIKE 'M%'
+
   FROM game JOIN goal ON (id=matchid)
 +
WHERE teamid='GER'
 
</source>
 
</source>
 
</div>
 
</div>
  
 
<div class='qu'>
 
<div class='qu'>
The following query shows coaches of teams that played on 9 June 2012. <code>JOIN</code> has been used to make relation between two tables. Because of this we can select '''coach''' from <code>eteam</code> table and '''mdate''' from <code>game</code> table.
+
Use the same join as in the previous question.
 +
<p class='imper'>Show the team1, team2 and player for every goal scored by a player called Mario <code>player LIKE 'Mario%'</code></p>
  
<p class='imper'>Show '''stadium''' and '''mdate''' of matches played by the team having 'Vicente del Bosque' as the coach. </p>
+
<source lang='sql' class='def'>
  
<source lang='sql' class='def'>
 
SELECT coach, mdate FROM eteam JOIN game
 
ON (team1 = eteam.id OR team2 = eteam.id)
 
WHERE mdate = '9 June 2012'
 
 
</source>
 
</source>
  
 
<source lang='sql' class='ans'>
 
<source lang='sql' class='ans'>
SELECT stadium, mdate FROM eteam JOIN game
+
SELECT team1, team2, player
ON (team1 = eteam.id OR team2 = eteam.id)  
+
  FROM game JOIN goal ON (id=matchid)
WHERE coach= 'Vicente del Bosque'
+
WHERE player LIKE 'Mario%'</source>
</source>
+
 
</div>
 
</div>
  
 
<div class='qu'>
 
<div class='qu'>
This example is using JOIN to display all goals scored during the UEFA EURO 2012 final (it was the only match in July).
+
The table '''eteam''' gives details of every national team including the coach. You can JOIN '''goal''' to '''eteam''' using the phrase <code>goal JOIN eteam on teamid=id</code>
 
+
<p class='imper'>Show player, teamid, coach, gtime for all goals scored in the first 10 minutes <code>gtime&lt;=10</code></p>
<p class='imper'>Show all goals scored in Germany - Greece match. Use team1 and team2 values to find the match.</p>
+
  
 
<source lang='sql' class='def'>
 
<source lang='sql' class='def'>
SELECT player, gtime FROM goal  
+
SELECT player, teamid, gtime
  JOIN game ON id = matchid
+
  FROM goal  
    WHERE mdate LIKE '%July%'
+
WHERE gtime<=10
 
</source>
 
</source>
  
 
<source lang='sql' class='ans'>
 
<source lang='sql' class='ans'>
SELECT player, gtime FROM goal
+
SELECT player, teamid, coach, gtime
   JOIN game ON id = matchid
+
   FROM goal JOIN eteam ON (teamid=id)
    WHERE team1 = 'GER' AND team2 = 'GRE'
+
WHERE gtime<=10
 
</source>
 
</source>
 
</div>
 
</div>
  
 
<div class='qu'>
 
<div class='qu'>
This query shows names of the teams that have scored less than 5 goals in the entire championship.
+
To JOIN '''game''' with '''eteam''' you could use either<br/> <code>game JOIN eteam ON (team1=eteam.id)</code> or <code>game JOIN eteam ON (team2=eteam.id)</code>
  
<p class='imper'>Show '''player''' names and '''count''' of goals for players who scored more than 2 goals.</p>
+
Notice that because '''id''' is a column name in both '''game''' and '''eteam''' you must specify '''eteam.id''' instead of just '''id'''
 +
<p class='imper'>List the dates of the matches in which 'Fernando Santos' was the team1 coach.</p>
  
 
<source lang='sql' class='def'>
 
<source lang='sql' class='def'>
SELECT teamname, COUNT(*)
+
 
  FROM eteam JOIN goal ON teamid = id
+
    GROUP BY teamname
+
      HAVING COUNT(*) < 5
+
 
</source>
 
</source>
  
 
<source lang='sql' class='ans'>
 
<source lang='sql' class='ans'>
SELECT player, COUNT(*)
+
SELECT mdate,teamname
   FROM eteam JOIN goal ON teamid = id
+
   FROM game JOIN eteam ON (team1=eteam.id)
    GROUP BY player
+
WHERE coach='Fernando Santos'
      HAVING COUNT(*) > 2
+
 
</source>
 
</source>
 
</div>
 
</div>
  
 
<div class='qu'>
 
<div class='qu'>
This example uses <code>COUNT()</code>, <code>GROUP BY</code> and <code>SELECT within SELECT</code> to show teams that have scored more goals than England. Add <code>JOIN</code> and...
+
<p class='imper'>List the player for every goal scored in a game where the staium was 'National Stadium, Warsaw'</p>
 
+
<p class='imper'>Show '''stadium''' and '''count''' of goals for stadiums where the number of goals is larger than number of goals scored by Spanish players (teamid = 'ESP') during the entire championship.</p>
+
  
 
<source lang='sql' class='def'>
 
<source lang='sql' class='def'>
SELECT teamid, COUNT(*)
 
  FROM goal
 
    GROUP BY teamid
 
      HAVING COUNT(*) >
 
      (SELECT COUNT(*)
 
        FROM goal WHERE teamid = 'ENG')
 
 
</source>
 
</source>
  
 
<source lang='sql' class='ans'>
 
<source lang='sql' class='ans'>
SELECT stadium, COUNT(*)
+
SELECT player
   FROM game JOIN goal ON matchid = id
+
   FROM goal JOIN game ON (id=matchid)
    GROUP BY stadium
+
WHERE stadium = 'National Stadium, Warsaw'
      HAVING COUNT(*) >=  
+
      (SELECT COUNT(*)  
+
        FROM goal WHERE teamid = 'ESP')
+
 
</source>
 
</source>
 
</div>
 
</div>
Line 264: Line 246:
 
<div>This query shows all goals scored in Germany-Greece quarterfinal.</div>
 
<div>This query shows all goals scored in Germany-Greece quarterfinal.</div>
  
<p class='imper'>Show names of all non-German players who scored a goal in matches against Germany.</p>
+
<p class='imper'>Show names of all players who scored a goal against Germany.</p>
<div class="hint" title="HINT">Select goals scored by non-German players in matches where GER was the id of either '''team1''' or '''team2'''.</div>
+
<div class="hint" title="HINT">
 +
Select goals scored by non-German players in matches where GER was the id of either '''team1''' or '''team2'''.
 +
 
 +
You can use <code>teamid!='GER'</code> to prevent listing German players.
 +
 
 +
You can use DISTINCT to stop players being listed twice.
 +
</div>
  
 
<source lang='sql' class='def'>
 
<source lang='sql' class='def'>
 
SELECT player, gtime
 
SELECT player, gtime
 
   FROM game JOIN goal ON matchid = id  
 
   FROM game JOIN goal ON matchid = id  
     WHERE (team1 = "GER" AND team2 = "GRE")  
+
     WHERE (team1='GER' AND team2='GRE')  
 
</source>
 
</source>
  
 
<source lang='sql' class='ans'>
 
<source lang='sql' class='ans'>
SELECT player  
+
SELECT DISTINCT player
 
   FROM game JOIN goal ON matchid = id  
 
   FROM game JOIN goal ON matchid = id  
    WHERE (team1 = "GER" OR team2 = "GER")  
+
WHERE (team1 = 'GER' OR team2 = 'GER')
    AND teamid != "GER"
+
  AND teamid!='GER'
 
</source>
 
</source>
 
</div>
 
</div>
  
 
<div class='qu'>
 
<div class='qu'>
The example shows result of the final match. <code>COUNT()</code> has been used to count the total number of goals.<br/>
 
  
<div class='imper'>Show '''team1''', '''team2''' and results of all matches played at 'PGE Arena Gdansk' stadium.</div>
+
<div class='imper'>Show '''teamname''' and the total number of goals scored.</div>
 +
<div class="hint" title="COUNT and GROUP BY">
 +
You should COUNT(*) in the SELECT line and GROUP BY teamname
 +
</div>
 
<source lang='sql' class='def'>
 
<source lang='sql' class='def'>
select team1, team2,  
+
SELECT teamname, player
  (select count(*) from goal  
+
  FROM eteam JOIN goal ON id=teamid
    where matchid = id AND teamid = team1)
+
ORDER BY teamname
  as team1,
+
  (select count(*) from goal
+
    where matchid = id AND teamid = team2)
+
  as team2
+
  FROM game WHERE id = 1031
+
 
</source>
 
</source>
  
 
<source lang='sql' class='ans'>
 
<source lang='sql' class='ans'>
select team1, team2,
+
SELECT teamname,COUNT(teamid)
  (select count(*) from goal
+
  FROM eteam JOIN goal ON id=teamid
    where matchid = id AND teamid = team1)  
+
GROUP BY teamname
  as team1,
+
  (select count(*) from goal  
+
    where matchid = id AND teamid = team2)
+
  as team2
+
  FROM game WHERE stadium = 'PGE Arena Gdansk'
+
 
</source>
 
</source>
 
</div>
 
</div>
Line 310: Line 290:
  
 
<div class='qu'>
 
<div class='qu'>
The following query shows matches which were won by team1.
+
<div class='imper'>Show the stadium and the number of goals scored in each stadium.
<div class='imper'>Show '''id''' of matches in which '''team1''' scored at least two more goals than '''team2'''.
+
 
</div>
 
</div>
 
<source lang='sql' class='def'>
 
<source lang='sql' class='def'>
select team1, team2 FROM game WHERE
+
</source>
  (select count(*) from goal
+
    where matchid = id AND teamid = team1) >
+
  (select count(*) from goal
+
    where matchid = id AND teamid = team2)</source>
+
  
 
<source lang='sql' class='ans'>
 
<source lang='sql' class='ans'>
select id FROM game WHERE
+
SELECT stadium,COUNT(1)
  (select count(*) from goal  
+
  FROM goal JOIN game ON id=matchid
    where matchid = id AND teamid = team1) -
+
GROUP BY stadium</source>
  (select count(*) from goal
+
    where matchid = id AND teamid = team2) >= 2</source>
+
 
</div>
 
</div>
  
 
<div class='qu'>
 
<div class='qu'>
The example shows teams that scored a goal at stadiums where Poland has played its matches.
+
<div class='imper'>For every match involving 'POL', show the matchid date and the number of goals scored.</div>
<div class='imper'>Show '''players''' that scored a goal at 'National Stadium, Warsaw' in a day ('''mdate''') when Czech Republic ('CZE') didn't play a match. Use <code>DISTINCT</code> to remove duplicates from your query results.</div>
+
 
<source lang='sql' class='def'>
 
<source lang='sql' class='def'>
SELECT distinct teamid  
+
SELECT matchid,mdate, team1, team2,teamid
 
   FROM game JOIN goal ON matchid = id  
 
   FROM game JOIN goal ON matchid = id  
    WHERE Stadium IN
+
WHERE (team1 = 'POL' OR team2 = 'POL')
        (SELECT stadium FROM game
+
          WHERE team1 = 'POL' OR team2 = 'POL')
+
 
</source>
 
</source>
  
 
<source lang='sql' class='ans'>
 
<source lang='sql' class='ans'>
SELECT DISTINCT player
+
SELECT matchid,mdate,COUNT(teamid)
 
   FROM game JOIN goal ON matchid = id  
 
   FROM game JOIN goal ON matchid = id  
    WHERE stadium = 'National Stadium, Warsaw'
+
WHERE (team1 = 'POL' OR team2 = 'POL')
      AND mdate NOT IN
+
GROUP BY matchid,mdate
        (SELECT mdate FROM game
+
          WHERE team1 = 'CZE' OR team2 = 'CZE')
+
 
</source>
 
</source>
 
</div>
 
</div>
  
 
<div class='qu'>
 
<div class='qu'>
The query shows the stadiums, where the teams who scored more than 8 goals played their matches.
+
 
<div class='imper'>Show '''teamid''' and '''count of goals''' for all 8 teams that have played more than 3 matches.</div>
+
<div class='imper'>For every match where 'GER' scored, show the number of goals scored by 'GER'</div>
 +
<source lang='sql' class='def'>
 +
</source>
 
<source lang='sql' class='ans'>
 
<source lang='sql' class='ans'>
SELECT teamid, count(*) FROM goal
+
SELECT matchid,mdate,COUNT(teamid)
   WHERE 3 <
+
   FROM game JOIN goal ON matchid = id
    (SELECT COUNT(distinct id) FROM game  
+
WHERE (teamid='GER')
      WHERE team1 = teamid OR team2 = teamid)  
+
GROUP BY matchid,mdate
  group by teamid
+
 
</source>
 
</source>
 +
</div>
 +
<div>
  
 +
<div class='qu'>
 +
 +
<div class='imper'>List every match with the goals scored by each team as shown.</div>
 +
<table class="sqlmine">
 +
<tr><th>mdate</th><th>team1</th><th>score1</th><th>team2</th><th>score2</th></tr>
 +
<tr><td>1 July 2012</td><td>ESP</td><td class="r">4</td><td>ITA </td><td class="r">0</td></tr>
 +
<tr><td>10 June 2012</td><td>ESP</td><td class="r">1</td><td>ITA</td><td class="r">1</td></tr>
 +
<tr><td>10 June 2012</td><td>IRL</td><td class="r">1</td><td>CRO</td><td class="r">3</td></tr>
 +
<tr><td colspan=5>...</td></tr>
 +
</table>
 +
Notice in the query given every goal is listed. If it was a team1 goal then a 1 appears in score1, otherwise there is a 0. You could SUM this column to get a count of the goals scored by team1.
 
<source lang='sql' class='def'>
 
<source lang='sql' class='def'>
SELECT distinct stadium
+
SELECT mdate,
   FROM game WHERE team1 IN
+
   team1,
    (SELECT teamid FROM goal  
+
  CASE WHEN teamid=team1 THEN 1 ELSE 0 END score1
      group by teamid having count(*) > 8)
+
  FROM game JOIN goal ON matchid = id
 
</source>
 
</source>
 +
 +
<source lang='sql' class='ans'>
 +
SELECT mdate,
 +
  team1,
 +
  SUM(CASE WHEN teamid=team1 THEN 1 ELSE 0 END) score1,
 +
  team2,
 +
  SUM(CASE WHEN teamid=team2 THEN 1 ELSE 0 END) score2
 +
  FROM game JOIN goal ON matchid = id
 +
GROUP BY mdate,matchid,team1,team2
 +
</source>
 +
 
</div>
 
</div>
 
<div>
 
<div>

Revision as of 12:31, 1 October 2012

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


JOIN and UEFA EURO 2012

This tutorial introduces JOIN which allows you to use data from two or more tables. The tables contain all matches and goals from UEFA EURO 2012 Football Championship in Poland and Ukraine.

Summary

The first example shows the goal scored by 'Bender'.

Show matchid and player name for all goals scored by Germany. teamid = 'GER'

SELECT * FROM goal 
  WHERE player LIKE '%Bender'
SELECT matchid, player 
 FROM goal
   WHERE teamid LIKE 'GER'

From the previous query you can see that Lars Bender's goal was scored in game 1012. Notice that the column matchid in the goal table corresponds to the id column in the game table.

Show id, stadium, team1, team2 for game 1012

SELECT id,stadium,team1,team2
  FROM game 
 WHERE stadium LIKE '%Warsaw%'
SELECT id,stadium,team1,team2
 FROM game
WHERE id=1012

You can combine the two steps into a single query with a join. You will get all the game details and all the goal details if you use

SELECT *
  FROM game JOIN goal ON (id=matchid)

Show the player, teamid and mdate and for every German goal. teamid='GER'

SELECT player,stadium
  FROM game JOIN goal ON (id=matchid)
SELECT player,teamid,mdate
  FROM game JOIN goal ON (id=matchid)
 WHERE teamid='GER'

Use the same join as in the previous question.

Show the team1, team2 and player for every goal scored by a player called Mario player LIKE 'Mario%'

 
SELECT team1, team2, player
  FROM game JOIN goal ON (id=matchid)
WHERE player LIKE 'Mario%'

The table eteam gives details of every national team including the coach. You can JOIN goal to eteam using the phrase goal JOIN eteam on teamid=id

Show player, teamid, coach, gtime for all goals scored in the first 10 minutes gtime<=10

SELECT player, teamid, gtime
  FROM goal 
 WHERE gtime<=10
SELECT player, teamid, coach, gtime
  FROM goal JOIN eteam ON (teamid=id)
 WHERE gtime<=10

To JOIN game with eteam you could use either
game JOIN eteam ON (team1=eteam.id) or game JOIN eteam ON (team2=eteam.id)

Notice that because id is a column name in both game and eteam you must specify eteam.id instead of just id

List the dates of the matches in which 'Fernando Santos' was the team1 coach.

 
SELECT mdate,teamname
  FROM game JOIN eteam ON (team1=eteam.id)
WHERE coach='Fernando Santos'

List the player for every goal scored in a game where the staium was 'National Stadium, Warsaw'

 
SELECT player
  FROM goal JOIN game ON (id=matchid)
 WHERE stadium = 'National Stadium, Warsaw'






More difficult questions

This query shows all goals scored in Germany-Greece quarterfinal.

Show names of all players who scored a goal against Germany.

Select goals scored by non-German players in matches where GER was the id of either team1 or team2.

You can use teamid!='GER' to prevent listing German players.

You can use DISTINCT to stop players being listed twice.

SELECT player, gtime
  FROM game JOIN goal ON matchid = id 
    WHERE (team1='GER' AND team2='GRE')
SELECT DISTINCT player
  FROM game JOIN goal ON matchid = id 
 WHERE (team1 = 'GER' OR team2 = 'GER')
   AND teamid!='GER'
Show teamname and the total number of goals scored.

You should COUNT(*) in the SELECT line and GROUP BY teamname

SELECT teamname, player
  FROM eteam JOIN goal ON id=teamid
 ORDER BY teamname
SELECT teamname,COUNT(teamid)
  FROM eteam JOIN goal ON id=teamid
GROUP BY teamname


Show the stadium and the number of goals scored in each stadium.
 
SELECT stadium,COUNT(1)
  FROM goal JOIN game ON id=matchid
GROUP BY stadium
For every match involving 'POL', show the matchid date and the number of goals scored.
SELECT matchid,mdate, team1, team2,teamid
  FROM game JOIN goal ON matchid = id 
 WHERE (team1 = 'POL' OR team2 = 'POL')
SELECT matchid,mdate,COUNT(teamid)
  FROM game JOIN goal ON matchid = id 
 WHERE (team1 = 'POL' OR team2 = 'POL')
GROUP BY matchid,mdate
For every match where 'GER' scored, show the number of goals scored by 'GER'
 
SELECT matchid,mdate,COUNT(teamid)
  FROM game JOIN goal ON matchid = id 
 WHERE (teamid='GER')
GROUP BY matchid,mdate
List every match with the goals scored by each team as shown.
mdateteam1score1team2score2
1 July 2012ESP4ITA 0
10 June 2012ESP1ITA1
10 June 2012IRL1CRO3
...

Notice in the query given every goal is listed. If it was a team1 goal then a 1 appears in score1, otherwise there is a 0. You could SUM this column to get a count of the goals scored by team1.

SELECT mdate,
  team1,
  CASE WHEN teamid=team1 THEN 1 ELSE 0 END score1
  FROM game JOIN goal ON matchid = id
SELECT mdate,
  team1,
  SUM(CASE WHEN teamid=team1 THEN 1 ELSE 0 END) score1,
  team2,
  SUM(CASE WHEN teamid=team2 THEN 1 ELSE 0 END) score2
  FROM game JOIN goal ON matchid = id 
GROUP BY mdate,matchid,team1,team2
Clear your results

Old JOIN Tutorial

The next tutorial about the Movie database involves some slightly more complicated joins.

Personal tools
Namespaces

Variants
Actions
Reference
Toolbox
Google AdSense