Checking if specific tuple exists in table

Acorn picture Acorn · Nov 6, 2011 · Viewed 10k times · Source

Is there a way to check if a specific tuple exists in a table in a where-in statement?

Something like:

create table Test(A int, B int);

insert into Test values (3, 9);
insert into Test values (6, 7);
insert into Test values (7, 6);
insert into Test values (3, 4);

select A, B
from Test
where (B, A) in Test;

Expected output:

6|7
7|6

Answer

FrankieTheKneeMan picture FrankieTheKneeMan · Nov 6, 2011

You were super close, the second half of an "in" clause has to be a select... so

SELECT A,B
FROM Test
WHERE (B,A) IN (SELECT B,A FROM Test);

The test (IN) must be in the same fields (or types of fields)