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 distinct player
from goal join game
on(game.id=goal.matchid)
where teamid!='GER' and (team1='GER' or team2='GER')
9.Show teamname and the total number of goals scored.
SELECT teamname, count(teamid)
FROM eteam JOIN goal
ON eteam.id=goal.teamid
group BY teamname
11.For every match involving 'POL', show the matchid, date and the number of goals scored.
SELECT matchid,mdate, count(teamid)
FROM game JOIN goal
ON (game.id= goal.matchid)
WHERE (team1 = 'POL' OR team2 = 'POL')
group by matchid,mdate
12.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(game.id=goal.matchid)
where teamid='GER'
group by matchid,mdate
13.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 left JOIN goal ON matchid = id
group by mdate, matchid, team1,team2