r/learnSQL • • Apr 23 '26

Last problem from SQLZoo Joins page

SELECT mdate,
  team1,
  SUM(CASE WHEN teamid=team1 THEN 1 ELSE 0 END) AS score1,
  team2,
  SUM(CASE WHEN teamid=team2 THEN 1 ELSE 0 END) AS score2
FROM game JOIN goal ON matchid = id
WHERE team1 = 'ENG' OR team2 = 'ENG'
GROUP BY mdate, matchid, team1, team2
ORDER BY mdate, matchid, team1, team2

This was my solution to the problem. I can't decide whether I am just an SQL noob or the question is actually wrong.
I get these rows back

mdateteam1score1team2score2
11 June 2012FRA1ENG1
15 June 2012SWE2ENG3
19 June 2012ENG1UKR0

These rows are in the solution but it doesn't have one row

mdateteam1score1team2score2
24 June 2012ENG0ITA0

I checked if game had this date. It did. The problem is that the table goal doesn't have the matchid 1028. So matchid = id is false here.

https://www.sqlzoo.net/wiki/The_JOIN_operation

5 Upvotes

1 comment sorted by

View all comments

1

u/ComicOzzy Apr 23 '26

If there were no goals scored during a game, then there won't be a record in the goals table for that game. If you want the row from that game in the output, even though there is no matching row in goals, you'll need to use an OUTER JOIN, not an INNER JOIN.