aakashgupta.0205 aakashgupta.0205 - 6 months ago 8
SQL Question

SQL comparing all values in a many-one table

I have a table as follows

column1 column2
a 1
a 2
b 2


I need to write an sql query that will go through the table and return me all column 2 values that have both a and b in column 1.

Answer

Try this:

SELECT column2
FROM mytable
WHERE column1 IN ('a', 'b')
GROUP BY column2
HAVING COUNT(DISTINCT column1) = 2
Comments