can can - 3 months ago 7
SQL Question

Sql query to select different row with same ID

I have a simple table and I wanted to get a row with same ID but different values.

lets assume I don't know the IDs;

lets say I have;

I have feature "one" and I want to find the feature that has same ID with feature "one".

Answer

One way to do this is with the exists operator:

SELECT *
FROM   features f_outer
WHERE  EXISTS (SELECT *
               FROM   features f_inner
               WHERE  f_outer.id = f_inner.id AND
                      f_outer.feature != f_inner.feature AND
                      f_inner.feature = 'one')