Skip to content
Advertisement

sql select query in same table

i have a newby sql related question.

let’s say i have this simple table:

i’d like to query for records which have “counterparts” only, i.e. i want to get a b only if there is b a in the table but i want to skip the “back links” (which is b ahere). to sum up i’d like to get the following result

this sql query does not work since in the case b a is handled the a b is removed from my result set.

the 2nd part of the WHERE clause does not work as desired.

do you have any hints? any help with this would be greatly appreciated.

regards peter

p.s. i am using the derby db.

Advertisement

Answer

You could change your last line to:

This assumes that either your columns are never self-referential (eg: a, a) or that you don’t want circular references to appear. If you do, then:

EDIT:

You’re probably better off using explicit joins as well:

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement