ruipacheco
ruipacheco

Reputation: 16422

How do select all elements from table that don't exist in a second table?

I have two table, A and B where B has a foreign key to A. Rows are inserted into A and after some processing of the data in A a row is inserted into B with a foreign key to A.

How can I select all rows in A that do not have a corresponding row in B?

Upvotes: 0

Views: 54

Answers (2)

Rams
Rams

Reputation: 2169

You use not exists keyword to do the same

select key_column from A 
 where not exists (select 1 from B where b.foreign_key_column=a.key_column)

Upvotes: 2

Kacper
Kacper

Reputation: 4818

select key_column from A
minus
select foreign_key_column from B;

This will give you a list of IDs of values that exist in A but not in B

Upvotes: 1

Related Questions