Вы можете использовать агрегацию:
select *
from table1
where exists (
select 1
from table2
where table2.bar in (table1.column1, table1.column2)
having count(distinct bar) = case when table1.column1 = table1.column2 then 1 else 2 end
);
Так что, если table1 содержит пару (foo, bar)
, а table2 содержит значения (foo), (baz)
, это не будет совпадать.