I have a SQL table countryTable structured as shown:
id | country
---+--------
1 | US
2 | GB
2 | US
3 | AU
4 | AU
5 | CA
4 | US
and I want to select only rows where the id has a single corresponding country - in this case, the output would be
id | country
---+--------
1 | US
3 | AU
5 | CA
since ids 2 & 4 map to (GB, US) and (AU, US) and not exclusively single countries.
So far, I've tried this query:
select *
from countryTable
group by 1,2
having count(distinct country) = 1
However, the result includes the rows with ids mapping to multiple countries.
Any guidance would be greatly appreciated.