Sql Query help to get non matching records from two tables
create table #one (id int,acc nvarchar(25))insert into #one (id , acc) values(1,'one') insert into #one (id , acc) values(2,'two') insert into #one (id , acc) values(3,'three') create table #two (acct nvarchar(25),ids int)insert into #two (acct,ids) values('one',1) insert into #two (acct,ids) values('two',3) insert into #two (acct,ids) values('four',4) select ids from #two EXCEPT select id from #one drop table #one drop table #two
test this one
SELECT B.Accountid FROM TableB AS B LEFT JOIN TableA AS A ON A.ID = B.Accountid WHERE A.ID IS NULL;
LEFT JOIN means it takes all the rows from the first table - if there are no matches on the first join condition, the result table columns for table B will be null - that's why it works.
SELECT B.AccountidFROM TableB AS B LEFT JOIN TableA AS A ON A.ID = B.Accountid WHERE A.ID IS NULL