Skip to content
Advertisement

SQL, Determine if Records in Table A exist in Table B

TableA has columns AccountNum and RoutingNum

TableB also has columns AccountNum and RoutingNum

Table B is more trustworthy. So, I would like to find if all records in Table A exist in Table B. And if not, which records do not match.

Is this on the right track? Your solution would be greatly appreciated.

select * 
from TableA a 
where a.AccountNum not in (select b.AccountNum from TableB)

Advertisement

Answer

You can use not exists. The following query gives you all records in tablea that cannot be found in tableb:

select a.*
from tablea a
where not exists (
    select 1 
    from tableb b 
    where a.accountNum = b.accountNum and a.routingNum = b.routingNum
)

This assumes that you want to match on both columns (which is what your description suggests). You can adapt the where conditions in the subquery to match on one column only.

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