Sports Database - The simplest many-to-many relationship database (ER diagram is below)
First, let's do a complete overview of the database, which is a 3-table join but does NOT show childless parent rows:
select
games.game_id
,players.player_id
,game_name
,player_name
from
games
, players
, gamePlayerRel
where
gamePlayerRel.game_id=games.game_id
and gamePlayerRel.player_id=players.player_id
Database Overview, no childless parent rows shown
| Game ID |
Game Name |
Player ID |
Player Name |
| 1 | Soccer | 2 | Dick |
| 3 | Tennis | 2 | Dick |
| 1 | Soccer | 3 | Harry |
| 2 | Rugby | 3 | Harry |
| 3 | Tennis | 3 | Harry |
Next, let's join games (parent) to gamePlayerRel (child), showing (if any) games with no players having NULL for player ID:
select
games.game_id
, game_name
,gamePlayerRel.player_id
from
games
left outer join gamePlayerRel
on games.game_id = gamePlayerRel.game_id
Players by Game
| Game ID |
Game Name |
Player ID |
| 1 | Soccer | 2 |
| 1 | Soccer | 3 |
| 2 | Rugby | 3 |
| 3 | Tennis | 2 |
| 3 | Tennis | 3 |