Checking if specific tuple exists in table Checking if specific tuple exists in table sqlite sqlite

Checking if specific tuple exists in table


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

SELECT A,BFROM TestWHERE (B,A) IN (SELECT B,A FROM Test);

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


Join Test to itself thusly:

select t1.A, t1.Bfrom Test t1join Test t2 on t1.A = t2.B and t1.B = t2.A

Or use an intersection:

select A, B from Testintersectselect B, A from Test

The self-join would probably be faster though.


You may get some confused looks when you use the word "tuple" in that way in the context of a database, because the word "tuple" also has a formal definition in database theory that is different from the set theory definition implied in your question.

If you're trying to identify unwanted tuples, you could try this approach:

SELECT t1.A, t1.B From Test t1 JOIN Test t2 ON t1.A=t2.B AND t1.B=t2.A WHERE t1.A > t1.B