Suppose I have two tables with a foreign key relationship. How can I find the rows in the "parent" table for which there is no corresponding "child" row?
For example:
create table a(x number primary key);
create table b(x number);
alter table b add constraint b_fk foreign key(x) references a(x) enable;
insert into a values(1);
insert into b values(1);
insert into a values(2);
commit;
delete from a where x = 2; -- will succeed
delete from a where x = 1; -- will fail, because there is a child row.
select ??? as "rows with no children" from a;
rows with no children
---------------------
2
select a.* from a where x not in (select x from b);
or:select a.* from a where not exists (select 1 from b where b.x = a.x);