Difference between revisions of "The JOIN operation"

From SQLZOO
Jump to: navigation, search
m (correction)
 
(94 intermediate revisions by 18 users not shown)
Line 1: Line 1:
 +
{{Languages}}
 +
[[File:footballERD.png]]
 
<div class="ref_section">
 
<div class="ref_section">
 +
<table class='db_ref'>
 +
<caption>game</caption>
 +
<tr>
 +
<th>id</th>
 +
<th>mdate</th>
 +
<th>stadium</th>
 +
<th>team1</th>
 +
<th>team2</th>
 +
</tr>
 +
<tr>
 +
<td>1001</td>
 +
<td>8 June 2012</td>
 +
<td>National Stadium, Warsaw</td>
 +
<td>POL</td>
 +
<td>GRE</td>
 +
</tr>
 +
<tr>
 +
<td>1002</td>
 +
<td>8 June 2012</td>
 +
<td>Stadion Miejski (Wroclaw)</td>
 +
<td>RUS</td>
 +
<td>CZE</td>
 +
</tr>
 +
<tr>
 +
<td>1003</td>
 +
<td>12 June 2012</td>
 +
<td>Stadion Miejski (Wroclaw)</td>
 +
<td>GRE</td>
 +
<td>CZE</td>
 +
</tr>
 +
<tr>
 +
<td>1004</td>
 +
<td>12 June 2012</td>
 +
<td>National Stadium, Warsaw</td>
 +
<td>POL</td>
 +
<td>RUS</td>
 +
</tr>
 +
<tr>
 +
<td colspan='5'>...</td>
 +
</tr>
 +
</table>
 +
<br/>
 +
<table class='db_ref'>
 +
<caption>goal</caption>
 +
<tr>
 +
<th>matchid</th>
 +
<th>teamid</th>
 +
<th>player</th>
 +
<th>gtime</th>
 +
</tr>
 +
<tr>
 +
<td>1001</td>
 +
<td>POL</td>
 +
<td>Robert Lewandowski</td>
 +
<td>17</td>
 +
</tr>
 +
<tr>
 +
<td>1001</td>
 +
<td>GRE</td>
 +
<td>Dimitris Salpingidis</td>
 +
<td>51</td>
 +
</tr>
 +
<tr>
 +
<td>1002</td>
 +
<td>RUS</td>
 +
<td>Alan Dzagoev</td>
 +
<td>15</td>
 +
</tr>
 +
<tr>
 +
<td>1002</td>
 +
<td>RUS</td>
 +
<td>Roman Pavlyuchenko</td>
 +
<td>82</td>
 +
</tr>
 +
<tr>
 +
<td colspan='5'>...</td>
 +
</tr>
 +
</table>
 +
<br/>
 +
<table class='db_ref'>
 +
<caption>eteam</caption>
 +
<tr>
 +
<th>id</th>
 +
<th>teamname</th>
 +
<th>coach</th>
 +
</tr>
 +
<tr>
 +
<td>POL</td>
 +
<td>Poland</td>
 +
<td>Franciszek Smuda</td>
 +
</tr>
 +
<tr>
 +
<td>RUS</td>
 +
<td>Russia</td>
 +
<td>Dick Advocaat</td>
 +
</tr>
 +
<tr>
 +
<td>CZE</td>
 +
<td>Czech Republic</td>
 +
<td>Michal Bilek</td>
 +
</tr>
 +
<tr>
 +
<td>GRE</td>
 +
<td>Greece</td>
 +
<td>Fernando Santos</td>
 +
</tr>
 +
<tr>
 +
<td colspan='5'>...</td>
 +
</tr>
 +
</table>
 +
</div>
  
</div>
 
  
 
   <h2>JOIN and UEFA EURO 2012</h2>
 
   <h2>JOIN and UEFA EURO 2012</h2>
   <p>This tutorial introduces JOIN which allows us to use relations between tables. The tables contain all matches and goals from UEFA EURO 2012 Football Championship in Poland and Ukraine.</p>
+
   <p>This tutorial introduces <code>JOIN</code> 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.</p>
  
 +
<p>The data is available (mysql format) at http://sqlzoo.net/euro2012.sql</p>
 
<div class="progress_panel"><div>
 
<div class="progress_panel"><div>
 
   <div class="summary">Summary</div>
 
   <div class="summary">Summary</div>
Line 16: Line 129:
 
    
 
    
 
<div class='qu'>
 
<div class='qu'>
The first example shows the goal scored by 'Wayne Rooney'. JOIN has been used to associate the goal to a game.
+
The first example shows the goal scored by a player with the last name 'Bender'. The <code>*</code> says to list all the columns in the table - a shorter way of saying <code>matchid, teamid, player, gtime</code>
  
<p class='imper'>Show '''matchid''' and '''player''' name all goals scored by English players.
+
<p class='imper'>Modify it to show the ''matchid'' and ''player'' name for all goals scored by Germany. To identify German players, check for:
<code>teamid = 'ENG'</code></p>
+
<code>teamid = 'GER'</code></p>
  
 
<source lang='sql' class='def'>
 
<source lang='sql' class='def'>
SELECT * FROM game JOIN goal ON matchid = id
+
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'>
<div>This query shows all goals scored in Germany-Greece quarterfinal.</div>
+
<p>From the previous query you can see that Lars Bender's scored a goal in game 1012. Now we want to know what teams were playing in that match.</P>
 +
<p>Notice in the that the column <code>matchid</code> in the <code>goal</code> table corresponds to the <code>id</code> column in the <code>game</code> table. We can look up information about game 1012 by finding that row in the '''game''' table.</p>
 +
<p class='imper'>Show id, stadium, team1, team2 for just game 1012</p>
  
<p class='imper'>Show names of all non-German players who scored a goal in matches against Germany.</p>
+
<source lang='sql' class='def'>
<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>
+
SELECT id,stadium,team1,team2
 +
  FROM game
 +
</source>
 +
 
 +
<source lang='sql' class='ans'>
 +
SELECT id,stadium,team1,team2
 +
FROM game
 +
WHERE id=1012</source>
 +
</div>
 +
 
 +
<div class='qu'>
 +
You can combine the two steps into a single query with a <code>JOIN</code>.
 +
SELECT *
 +
  FROM game JOIN goal ON (id=matchid)
 +
<p>The '''FROM''' clause says to merge data from the goal table with that from the game table. The '''ON''' says how to figure out which rows in '''game''' go with which rows in '''goal''' - the '''matchid''' from '''goal''' must match '''id''' from '''game'''. (If we wanted to be more clear/specific we could say <br/><code>ON (game.id=goal.matchid)</code></p>
 +
<p>The code below shows the player (from the goal) and stadium name (from the game table) for every goal scored.</p>
 +
<p class='imper'>Modify it to show the player, teamid, stadium and mdate for every German goal.</p>
  
 
<source lang='sql' class='def'>
 
<source lang='sql' class='def'>
SELECT player, time, matchid  
+
SELECT player,stadium
 +
  FROM game JOIN goal ON (id=matchid)
 +
</source>
 +
 
 +
<source lang='sql' class='ans'>
 +
SELECT player,teamid,stadium,mdate
 +
  FROM game JOIN goal ON (id=matchid)
 +
WHERE teamid='GER'
 +
</source>
 +
</div>
 +
 
 +
<div class='qu'>
 +
Use the same <code>JOIN</code> 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>
 +
 
 +
<source lang='sql' class='def'>
 +
 
 +
</source>
 +
 
 +
<source lang='sql' class='ans'>
 +
SELECT team1, team2, player
 +
  FROM game JOIN goal ON (id=matchid)
 +
WHERE player LIKE 'Mario%'</source>
 +
</div>
 +
 
 +
<div class='qu'>
 +
The table <code>eteam</code> gives details of every national team including the coach. You can <code>JOIN</code> <code>goal</code> to <code>eteam</code> using the phrase <code>goal JOIN eteam on teamid=id</code>
 +
<p class='imper'>Show <code>player</code>, <code>teamid</code>, <code>coach</code>, <code>gtime</code> for all goals scored in the first 10 minutes <code>gtime&lt;=10</code></p>
 +
 
 +
<source lang='sql' class='def'>
 +
SELECT player, teamid, gtime
 +
  FROM goal
 +
WHERE gtime<=10
 +
</source>
 +
 
 +
<source lang='sql' class='ans'>
 +
SELECT player, teamid, coach, gtime
 +
  FROM goal JOIN eteam ON (teamid=id)
 +
WHERE gtime<=10
 +
</source>
 +
</div>
 +
 
 +
<div class='qu'>
 +
To <code>JOIN</code> <code>game</code> with <code>eteam</code> you could use either<br/> <code>game JOIN eteam ON (team1=eteam.id)</code> or <code>game JOIN eteam ON (team2=eteam.id)</code>
 +
 
 +
Notice that because <code>id</code> is a column name in both <code>game</code> and <code>eteam</code> you must specify <code>eteam.id</code> instead of just <code>id</code>
 +
<p class='imper'>List the the dates of the matches and the name of the team in which 'Fernando Santos' was the team1 coach.</p>
 +
 
 +
<source lang='sql' class='def'>
 +
 
 +
</source>
 +
 
 +
<source lang='sql' class='ans'>
 +
SELECT mdate,teamname
 +
  FROM game JOIN eteam ON (team1=eteam.id)
 +
WHERE coach='Fernando Santos'
 +
</source>
 +
</div>
 +
 
 +
<div class='qu'>
 +
<p class='imper'>List the player for every goal scored in a game where the stadium was 'National Stadium, Warsaw'</p>
 +
 
 +
<source lang='sql' class='def'>
 +
</source>
 +
 
 +
<source lang='sql' class='ans'>
 +
SELECT player
 +
  FROM goal JOIN game ON (id=matchid)
 +
WHERE stadium = 'National Stadium, Warsaw'
 +
</source>
 +
</div>
 +
 
 +
 
 +
 
 +
<br/><br/><br/><br/>
 +
<h2>More difficult questions</h2>
 +
<div class='qu'>
 +
<div>The example query shows all goals scored in the Germany-Greece quarterfinal.</div>
 +
 
 +
<p class='imper'>Instead show the '''name''' of all players who scored a goal against Germany.</p>
 +
<div class="hint" title="HINT">
 +
Select goals scored only 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 <code>DISTINCT</code> to stop players being listed twice.
 +
</div>
 +
 
 +
<source lang='sql' class='def'>
 +
SELECT player, gtime
 
   FROM game JOIN goal ON matchid = id  
 
   FROM game JOIN goal ON matchid = id  
     WHERE matchid LIKE 'GER-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") and teamid NOT LIKE "GER"
+
WHERE (team1 = 'GER' OR team2 = 'GER')
 +
  AND teamid!='GER'
 
</source>
 
</source>
 
</div>
 
</div>
  
 
<div class='qu'>
 
<div class='qu'>
Where to find some very small, very rich countries.<br/>
+
 
<b>We use <code>AND</code> to ensure that two or more conditions hold
+
<div class='imper'>Show '''teamname''' and the total number of goals scored.</div>
true.</b>
+
<div class="hint" title="COUNT and GROUP BY">
<div>The example shows the countries where the population is small and the
+
You should COUNT(*) in the SELECT line and GROUP BY teamname
gdp is high.</div>
+
</div>
<div class='imper'>Show the <b>name</b> and <b>region</b> where the area is less then 2000 and the gdp is more than 5000000000</div>
 
 
<source lang='sql' class='def'>
 
<source lang='sql' class='def'>
SELECT name , region
+
SELECT teamname, player
   FROM bbc
+
   FROM eteam JOIN goal ON id=teamid
  WHERE population < 2000000
+
ORDER BY teamname
    AND gdp > 5000000000
 
 
</source>
 
</source>
  
 
<source lang='sql' class='ans'>
 
<source lang='sql' class='ans'>
SELECT name , region
+
SELECT teamname,COUNT(teamid)
   FROM bbc
+
   FROM eteam JOIN goal ON id=teamid
  WHERE area < 2000
+
GROUP BY teamname
    AND gdp > 5000000000
 
 
</source>
 
</source>
 
</div>
 
</div>
Line 76: Line 294:
  
 
<div class='qu'>
 
<div class='qu'>
Checking a list The word <b>IN</b> allows us to check if an item is in a list.
+
<div class='imper'>Show the stadium and the number of goals scored in each stadium.
The example shows the name and population for the countries 'Ireland', 'Iceland' and 'Denmark'
 
<div class='imper'>Show the '''name''' and the '''population''' for 'Denmark', 'Finland', 'Norway', 'Sweden'
 
 
</div>
 
</div>
 
<source lang='sql' class='def'>
 
<source lang='sql' class='def'>
SELECT name, population FROM bbc
+
</source>
  WHERE name IN ('Ireland', 'Iceland',
 
                'Denmark')</source>
 
  
 
<source lang='sql' class='ans'>
 
<source lang='sql' class='ans'>
SELECT name, population FROM bbc
+
SELECT stadium,COUNT(1)
   WHERE name IN ('Denmark', 'Finland',
+
   FROM goal JOIN game ON id=matchid
                'Norway', 'Sweden')</source>
+
GROUP BY stadium</source>
 
</div>
 
</div>
  
 
<div class='qu'>
 
<div class='qu'>
What are the countries beginning with G?
+
<div class='imper'>For every match involving 'POL', show the matchid, date and the number of goals scored.</div>
'''The word''' <code>LIKE</code> '''permits pattern matching''' - % '''is the wildcard'''.
 
The examples shows countries beginning with D
 
<div class='imper'>Show each country that begins with G</div>
 
 
<source lang='sql' class='def'>
 
<source lang='sql' class='def'>
SELECT name FROM bbc
+
SELECT matchid,mdate, team1, team2,teamid
  WHERE name LIKE 'D%'
+
  FROM game JOIN goal ON matchid = id
 +
WHERE (team1 = 'POL' OR team2 = 'POL')
 
</source>
 
</source>
  
 
<source lang='sql' class='ans'>
 
<source lang='sql' class='ans'>
SELECT name FROM bbc
+
SELECT matchid,mdate,COUNT(teamid)
   WHERE name LIKE 'G%'
+
  FROM game JOIN goal ON matchid = id
 +
WHERE (team1 = 'POL' OR team2 = 'POL')
 +
GROUP BY matchid,mdate
 +
</source>
 +
</div>
 +
 
 +
<div class='qu'>
 +
 
 +
<div class='imper'>For every match where 'GER' scored, show matchid, match date and the number of goals scored by 'GER'</div>
 +
<source lang='sql' class='def'>
 +
</source>
 +
<source lang='sql' class='ans'>
 +
SELECT matchid,mdate,COUNT(teamid)
 +
   FROM game JOIN goal ON matchid = id
 +
WHERE (teamid='GER')
 +
GROUP BY matchid,mdate
 
</source>
 
</source>
 
</div>
 
</div>
 +
<div>
  
 
<div class='qu'>
 
<div class='qu'>
Which countries are not too small and not too big?
+
 
<code>BETWEEN</code> allows range checking - note that it is inclusive.
+
<div class='imper'>List every match with the goals scored by each team as shown. This will use "[[CASE|CASE WHEN]]" which has not been explained in any previous exercises.</div>
<div class='imper'>'''Show the area in 1000 square km. Show''' ''area''/1000 '''instead of''' ''area''</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. '''Sort your result by mdate, matchid, team1 and team2.'''
 
<source lang='sql' class='def'>
 
<source lang='sql' class='def'>
SELECT name, area FROM bbc
+
SELECT mdate,
   WHERE area BETWEEN 207600 AND 244820
+
   team1,
 +
  CASE WHEN teamid=team1 THEN 1 ELSE 0 END score1
 +
  FROM game JOIN goal ON matchid = id
 
</source>
 
</source>
  
 
<source lang='sql' class='ans'>
 
<source lang='sql' class='ans'>
SELECT name, area/1000 FROM bbc
+
SELECT mdate,
  WHERE area BETWEEN 207600 AND 244820
+
  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 LEFT JOIN goal ON matchid = id
 +
GROUP BY mdate,matchid,team1,team2
 
</source>
 
</source>
 +
 
</div>
 
</div>
 
+
<div>
 
<div class="lsclear">Clear your results</div>
 
<div class="lsclear">Clear your results</div>
  
[[JOIN Quiz]]
+
<p><div class="quizlink">[[JOIN Quiz]]</div></p>
 +
</div>
 
[[Old JOIN Tutorial]]
 
[[Old JOIN Tutorial]]
 
<p>[http://sqlzoo.net/w/index.php/More_JOIN_operations The next tutorial about the Movie database involves some slightly more complicated joins.]</p>
 
<p>[http://sqlzoo.net/w/index.php/More_JOIN_operations The next tutorial about the Movie database involves some slightly more complicated joins.]</p>

Latest revision as of 10:38, 16 October 2018

Language:Project:Language policy English  • 日本語 • 中文

FootballERD.png

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

The data is available (mysql format) at http://sqlzoo.net/euro2012.sql

Summary

The first example shows the goal scored by a player with the last name 'Bender'. The * says to list all the columns in the table - a shorter way of saying matchid, teamid, player, gtime

Modify it to show the matchid and player name for all goals scored by Germany. To identify German players, check for: 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 scored a goal in game 1012. Now we want to know what teams were playing in that match.

Notice in the that the column matchid in the goal table corresponds to the id column in the game table. We can look up information about game 1012 by finding that row in the game table.

Show id, stadium, team1, team2 for just game 1012

SELECT id,stadium,team1,team2
  FROM game
SELECT id,stadium,team1,team2
 FROM game
WHERE id=1012

You can combine the two steps into a single query with a JOIN.

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

The FROM clause says to merge data from the goal table with that from the game table. The ON says how to figure out which rows in game go with which rows in goal - the matchid from goal must match id from game. (If we wanted to be more clear/specific we could say
ON (game.id=goal.matchid)

The code below shows the player (from the goal) and stadium name (from the game table) for every goal scored.

Modify it to show the player, teamid, stadium and mdate for every German goal.

SELECT player,stadium
  FROM game JOIN goal ON (id=matchid)
SELECT player,teamid,stadium,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 the dates of the matches and the name of the team 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 stadium was 'National Stadium, Warsaw'

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






More difficult questions

The example query shows all goals scored in the Germany-Greece quarterfinal.

Instead show the name of all players who scored a goal against Germany.

Select goals scored only 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 matchid, match date and 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. This will use "CASE WHEN" which has not been explained in any previous exercises.
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. Sort your result by mdate, matchid, team1 and team2.

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