Han Ben
Han Ben

Reputation: 17

SQL: How to find entry that appears more than twice with specific value

I want to find table entry that appears more than once with certain value

    Name | ID 
==================
    Peter   1
    James   2
    Peter   2
    James   2

I want to select entries where ID is both 1 and 2, in this example only Peter appears twice with value 1 and 2 while James appear twice but ID of James is not 1 and 2

Is there operator for this kind of query?

Upvotes: 0

Views: 1031

Answers (1)

Gordon Linoff
Gordon Linoff

Reputation: 1269443

You can do:

select name
from t
where id in (1, 2)
group by name
having count(distinct id) = 2;

Upvotes: 1

Related Questions