Search code examples
sqlpostgresqlrelational-division

Finding combinations of specific values


I don't know how to write the query for below.

My table is

col1 col2 
5   1
5   5
5   6
5   7
4   5
4   8
4   9
4   3
3   3
3   5

I need to select distinct col1 id where both parameters exists in col2. eg. if i send 6,7 it should send me 5


Solution

  • Try:

    SELECT col1
    FROM mytable
    WHERE col2 IN (6, 7)
    GROUP BY col1
    HAVING COUNT(DISTINCT col2) = 2