-1

I have a table called frequents with 2 columns, drinkers and the bars they frequent.

I currently have answered this statement:

 Drinkers who frequent at least 2 bars

With the following query:

SELECT DISTINCT drinker
FROM frequents f
WHERE EXISTS(
SELECT drinker
FROM frequents y
WHERE y.drinker=f.drinker
AND f.bar<>y.bar);

I am trying to modify this query to answer the following:

Drinkers who frequent exactly two bars 

but I am stuck as to what modifications to my logic must be implemented to satisfy this statement.

Glen Thomas
  • 10,190
  • 5
  • 33
  • 65
Bret
  • 167
  • 1
  • 1
  • 12

1 Answers1

2

To get a list of drinkers where they have 2 rows in the table, group by drinker and then using having to get where count is 2:

SELECT drinker
  FROM frequents
  GROUP BY drinker
HAVING COUNT(drinker) = 2
Glen Thomas
  • 10,190
  • 5
  • 33
  • 65