So I’m working on an SQL problem, in which I have a bars and beer database from which I want to:
list all bars which serve a beer that Mike likes, and which are frequented by more than one drinker.
The database consists of:
likes (drinker, beer)
frequents(dinker, bar)
sells(bar, beer)
So I tried:
SELECT bar
FROM beer.sells
WHERE beer IN
(SELECT beer
FROM beer.likes
WHERE drinker = 'Mike') AND bar IN
(SELECT bar FROM beer.frequents HAVING COUNT(drinker) > 1)
Which didn’t work… why doesn’t the count(drinker) comparison work as a filter to filter out bars that have less than 2 drinkers who frequent them?
Looks like you need a
GROUP BY barin the bar subquery. Otherwise, you’ll effectively getCOUNT(*)frombeer.frequents, which isn’t what you want: