Skip to content
Advertisement

Oracle Why Exists Query Returns This Result

I am confusing using exists to compare two data between two tables. I am expecting to get 0 rows from query but it returns some data. I don’t know why.

select main_rule, child_rule from scoredef_detail s where s.main_rule = 515;
1   515 516
2   515 517

select main_rule, child_rule from scoredef_detail_at s where s.main_rule = 515;
1   515 516
2   515 517

This query returns all of them above. I am trying to get differences. These columns are foreign keys of some other tables. Can it related with other table’s records?

select main_rule, child_rule
  from scoredef_detail s
 where exists
 (select main_rule, child_rule
          from scoredef_detail_at sa
         where s.main_rule = sa.main_rule and (s.child_rule <> sa.child_rule));

Thanks in advance.

Advertisement

Answer

You can use a full join to get mismatches:

select s.*, sa.*
from (select main_rule, child_rule
      from scoredef_detail s
      where s.main_rule = 515
     ) s full join
     (select main_rule, child_rule
      from scoredef_detail_at s
      where s.main_rule = 515
     ) sa
     on sa.main_rule = s.main_rule and sa.child_rule = s.child_rule
where sa.main_rule is null or s.main_rule is null;

This will show the pairs that are in one table but not the other.

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