Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to fetch unmatching records from two SQL tables?

Tags:

sql

I want to fetch the unmatching records from two table in SQL, the table structure is as follows:

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

What will be the query to fetch the required output in SQL?

like image 297
djmzfKnm Avatar asked Jul 08 '09 06:07

djmzfKnm


1 Answers

I think joeslice's answer will only give half the results. You need to union the other table. Alternatively, you could do a full outer join.

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
like image 117
brianegge Avatar answered Nov 03 '22 22:11

brianegge