Link to home
Create AccountLog in
Avatar of jpwallen
jpwallenFlag for United States of America

asked on

How to select records that are in one database but not the other

I have two tables with some data. They each share a common characteristic (a customer id field).
I would like to create a SQL statement that would select everything from table A that is not in table B.
For example if table A has the id's 1,2,3,4 and table B has the values of 1,4; I only want to get rows 2 and 3
from table A out of the query. I have looked at an inner join but it seems to only do the opposite of what I want...
How do I get distinct records from table A that are NOT in table B?
Thank's in advance.
ASKER CERTIFIED SOLUTION
Avatar of Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3]
Flag of Luxembourg image

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer
select * from table A where not exists (select b.* from b where a.field = b.field )
You could also try:

SELECT DISTINCT CustomerId from TableA where CustomerId not in (SELECT CustomerId from TableB)