[SOLVED] Selecting data that does not have a reverse from table

Issue

having trouble figuring this out. The question is basically a table with 2 integer datas, p1 and p2. So lets say p1=100, p2=101. There may or may not exist another row with the values p1=101,p2=100 ( the reverse). I have to find a query that will list ONLY THE ROWS THAT DO NOT HAVE THEIR REVERSE VERSION. Hopefully i was able to explain the question clearly but englando is hard… Any help is much appreciated.

EDIT: Forgot to mention, i must not use INNER,OUTER JOIN statements in the solution of this question.

An example Table: Looking at this table, i need to select only the 3rd row p1=106, p2=104.

   p1=101 , p2=103
   p1=103 , p2=101
   p1=106 , p2=104
   p1=108 , p2=105
   p1=105 , p2=108

Solution

This will work too (and no JOINs used):

select t1.p1,t1.p2
from tbl t1
where not exists(select p2,p1 from tbl where p2=t1.p1 and p1=t1.p2)

Answered By – Luuk

Answer Checked By – David Marino (BugsFixing Volunteer)

Leave a Reply

Your email address will not be published. Required fields are marked *