Wednesday, 8 January 2014

How to get Non matching records from 2 tables in Sql Server



We are having 2 tables like below

select * from table_2

select * from Table_1


Now write the below queries:

1. (select table_2.id from table_2
except
select Table_1.id from Table_1)
union
(select Table_1.id from Table_1
except
select table_2.id from table_2)

OR

2. (select table_2.id from table_2
left join Table_1
on table_2.ID=Table_1.id)
union
(select Table_1.id from Table_1
left join table_2
on table_2.ID=Table_1.id)
except
(select Table_1.id from Table_1
 join table_2
on table_2.ID=Table_1.id)


Result is:




No comments:

Post a Comment