Link to the page - https://sqlzoo.net/wiki/The_JOIN_operation
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 |
... |
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
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 matchid, player FROM goal
WHERE teamid = 'ger'
matchid | player |
---|---|
1008 | Mario Gómez |
1010 | Mario Gómez |
1010 | Mario Gómez |
1012 | Lukas Podolski |
1012 | Lars Bender |
1026 | Philipp Lahm |
1026 | Sami Khedira |
1026 | Miroslav Klose |
1026 | Marco Reus |
1030 | Mesut Özil |
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
where id = 1012
id | stadium | team1 | team2 |
---|---|---|---|
1012 | Arena Lviv | DEN | GER |
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,teamid,stadium,mdate
FROM game JOIN goal ON (id=matchid)
where teamid = 'ger'
player | teamid | stadium | mdate |
---|---|---|---|
Mario Gómez | GER | Arena Lviv | 9 June 2012 |
Mario Gómez | GER | Metalist Stadium | 13 June 2012 |
Mario Gómez | GER | Metalist Stadium | 13 June 2012 |
Lukas Podolski | GER | Arena Lviv | 17 June 2012 |
Lars Bender | GER | Arena Lviv | 17 June 2012 |
Philipp Lahm | GER | PGE Arena Gdansk | 22 June 2012 |
Sami Khedira | GER | PGE Arena Gdansk | 22 June 2012 |
Miroslav Klose | GER | PGE Arena Gdansk | 22 June 2012 |
Marco Reus | GER | PGE Arena Gdansk | 22 June 2012 |
Mesut Özil | GER | National Stadium, Warsaw | 28 June 2012 |
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%'
team1 | team2 | player |
---|---|---|
GER | POR | Mario Gómez |
NED | GER | Mario Gómez |
NED | GER | Mario Gómez |
IRL | CRO | Mario Mandžukic |
IRL | CRO | Mario Mandžukic |
ITA | CRO | Mario Mandžukic |
ITA | IRL | Mario Balotelli |
GER | ITA | Mario Balotelli |
GER | ITA | Mario Balotelli |
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, coach, gtime
FROM goal JOIN eteam ON goal.teamid=eteam.id
WHERE gtime<=10
player | teamid | coach | gtime |
---|---|---|---|
Petr Jirácek | CZE | Michal Bílek | 3 |
Václav Pilar | CZE | Michal Bílek | 6 |
Mario Mandžukic | CRO | Slaven Bilic | 3 |
Fernando Torres | ESP | Vicente del Bosque | 4 |
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 game.team1=eteam.id
where eteam.coach = 'Fernando Santos'
mdate | teamname |
---|---|
12 June 2012 | Greece |
16 June 2012 | Greece |
List the player for every goal scored in a game where the stadium was 'National Stadium, Warsaw'.
select player
from goal JOIN game ON goal.matchid=game.id
where stadium='National Stadium, Warsaw'
player |
---|
Robert Lewandowski |
Dimitris Salpingidis |
Alan Dzagoev |
Jakub Blaszczykowski |
Giorgos Karagounis |
Cristiano Ronaldo |
Mario Balotelli |
Mario Balotelli |
Mesut Özil |
- NOTE that "correct" asnwer has duplicates. So the better answer will be if one will add
DISTINCT
beforeSELECT
The example query shows all goals scored in the Germany-Greece quarterfinal.
SELECT player, gtime
FROM game JOIN goal ON matchid = id
WHERE (team1='GER' AND team2='GRE')
Instead show the name of all players who scored a goal against Germany.
SELECT DISTINCT player
FROM goal JOIN game ON (goal.matchid = game.id)
WHERE (team1='GER' OR team2='GER') and teamid<>'GER'
player |
---|
Robin van Persie |
Michael Krohn-Dehli |
Georgios Samaras |
Dimitris Salpingidis |
Mario Balotelli |
Show teamname and the total number of goals scored.
SELECT teamname, COUNT(matchid) "goals scored"
FROM eteam JOIN goal ON id=teamid
GROUP BY teamname
ORDER BY teamname
teamname | goals scored |
---|---|
Croatia | 4 |
Czech Republic | 4 |
Denmark | 4 |
England | 5 |
France | 3 |
Germany | 10 |
Greece | 5 |
Italy | 6 |
Netherlands | 2 |
Poland | 2 |
Portugal | 6 |
Republic of Ireland | 1 |
Russia | 5 |
Spain | 12 |
Sweden | 5 |
Ukraine | 2 |
Show the stadium and the number of goals scored in each stadium.
select stadium, COUNT(matchid) goals
FROM game JOIN goal ON goal.matchid=game.id
GROUP BY stadium
stadium | goals |
---|---|
Arena Lviv | 9 |
Donbass Arena | 7 |
Metalist Stadium | 7 |
National Stadium, Warsaw | 9 |
Olimpiyskiy National Sports Complex | 14 |
PGE Arena Gdansk | 13 |
Stadion Miejski (Poznan) | 8 |
Stadion Miejski (Wroclaw) | 9 |
For every match involving 'POL', show the matchid, date and the number of goals scored.
SELECT matchid, mdate, COUNT(matchid) goals
FROM game JOIN goal ON matchid = id
WHERE (team1 = 'POL' OR team2 = 'POL')
GROUP BY matchid, mdate
matchid | mdate | goals |
---|---|---|
1001 | 8 June 2012 | 2 |
1004 | 12 June 2012 | 2 |
1005 | 16 June 2012 | 1 |
For every match where 'GER' scored, show matchid, match date and the number of goals scored by 'GER'
select matchid, mdate, COUNT(matchid)
from goal JOIN game ON matchid=id
where teamid='GER'
GROUP BY matchid, mdate
matchid | mdate | COUNT(matchid) |
---|---|---|
1008 | 9 June 2012 | 1 |
1010 | 13 June 2012 | 2 |
1012 | 17 June 2012 | 2 |
1026 | 22 June 2012 | 4 |
1030 | 28 June 2012 | 1 |
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.
mdate | team1 | score1 | team2 | score2 |
---|---|---|---|---|
1 July 2012 | ESP | 4 | ITA | 0 |
10 June 2012 | ESP | 1 | ITA | 1 |
10 June 2012 | IRL | 1 | CRO | 3 |
... |
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,
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
ORDER BY mdate, matchid, team1, team2
There is no pair 0-0 in my results. How to handle it?
The problem is: if there is a game WITH NO GOALS then there is no row in goal
table. Thus INNER JOIN
doesn't work and it is needed to use LEFT JOIN
.