Monday, July 13, 2015

How to get not matching Records from 2 tables using SQL Server

There are 2 ways to do that :

Solution 1:

SELECT * FROM Table1 t1
RIGHT OUTER JOIN Table2 t2 de ON t1.ID=t2.ID
WHERE t1.ID is null

Solution 2:

SELECT * FROM Table2 t2
LEFT OUTER JOIN Table1 t1 de ON t1.ID=t2.ID
WHERE t1.ID is null

Question : -

TableA

ID Name
1 N1
2 N2
3 N3
4 N4
5 N5

TableB

ID Name
1 N1
2 N2
3 N3
4 N4
6 N6

Result:-
ID Name
5 N5
6 N6



Solution 3:-

Select * From TableA
Where ID Not In (Select ID From TableB) OR
(Select Name From TableB)

Union

Select * From TableB
Where ID Not In (Select ID From TableA) OR
(Select Name From TableA)

No comments:

Post a Comment