I’ve a data base with two tables.
x
Table Players Table Wins
ID Name ID Player_won
1 Mick 1 2
2 Frank 2 1
3 Sarah 3 4
4 Eva 4 5
5 Joe 5 1
I need a SQL query which show “The players who have not won any game”. I tried but I don’t know even how to begin. Thank you
Advertisement
Answer
You need all the rows from players
that don’t have corresponding rows in wins
. For this you need a left join, filtering for rows that don’t join:
select
p.id,
p.name
from Players p
left join Wins w on w.Player_won = p.id
where w.Player_won is null
You can also use not in
:
select
id,
name
from Players
where id not in (select Player_won from Wins)