Lets say I have a table1 in SQL as follows.
Team PlayerID
Team1 12
Team2 56
Team1 78
Team1 96
Team3 23
Team2 45
Team3 89
Team3 78
Now I have a new table2 as
PlayerID Gender
12 Male
56 Female
78 Female
96 Male
23 Female
45 Male
89 Female
78 Female
Is there a way to list out all the teams with only female genders?
You can use join
, group by
, and having
:
select t.team
from teams t join
players p
on p.playerid = t.playerid
group by t.team
having min(gender) = max(gender) and min(gender) = 'Female';
If you are sure to have at least 1 player per team then this should work:
SELECT T1.Team
FROM Table1 T1
LEFT JOIN Table2 T2 ON T1.PlayerID = T2.PlayerID AND t2.Gender = 'Male'
GROUP BY T1.Team
Having COUNT(Gender) = 0
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.