Wednesday, January 18, 2012

How to fetch unmatching records from two SQL tables?

Table1
Id Name
1 Prashant
2 Ravi
3 Gaurav
5 Naween
7 Sachin

Table2
Id Name
1 Prashant
2 Ravi
4 Alok
6 Raja

The output I want is
Id Name
3 Gaurav
4 Alok
5 Naween
6 Raja
7 Sachin


select a.Id, a.Name from Table1 a left outer join Table2 b on a.Name = b.Name where b.Id is null
UNION ALL
select a.Id, a.Name from Table2 a left outer join Table1 b on a.Name = b.Name where b.Id is null

No comments:

Post a Comment