The question states "List every match with the goals scored by each team as shown below."
This is the result that the question is asking me to show.
I'm quite confused with LEFT JOIN
in particular for this problem. Initially, I used the this code:
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, team1, team2
However, this does not give the right answer, as the SQLZOO result is not correct. So, I looked up on the Internet for the answer, and it states this:
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, team1, team2
How did they know which kind of JOIN
to use? I know that for the LEFT JOIN
it takes all information from the game table and merges to the goal which includes only the matching information to the goal table. The JOIN
table will only include information that both tables have in common.