tags:

views:

313

answers:

8

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?

+1  A: 
select a.Id, a.Name 
from Table1 a 
left outer join Table2 b 
on a.Name = b.Name 
where b.Id is null

EDIT: My answer is wrong.... it will give you only the results from Table1 that are not found in Table2. Thanks for pointing that out.

joeslice
This query will only give the records from table1 that are not in table2. The question asks for the unmatched records in both tables.
Yaakov Ellis
+6  A: 

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
brianegge
+1 thanks for pointing out my mistake.
joeslice
I guess that it is good enough to join on ID column instead of NAME
van
Keep in mind, this is more expensive than a full outer join.
Sam Saffron
A: 

Since you want to get the unmatched records from both tables, I think that you will need two queries (one for each table) which will be unioned together:

(SELECT t1.Id, t1.Name 
 FROM Table1 as t1 
 LEFT OUTER JOIN Table2 as t2 on t1.Name = t2.Name 
 WHERE t2.Id is null)
UNION
(SELECT t2.Id, t2.Name 
 FROM Table2 as t2 
 LEFT OUTER JOIN Table1 as t1 on t2.Name = t1.Name 
 WHERE t1.Id is null)
Yaakov Ellis
+1  A: 
SELECT * FROM 
(
  SELECT * FROM Table1
 MINUS
  SELECT * FROM Table2
)
UNION
(
  SELECT * FROM Table2
 MINUS
  SELECT * FROM Table1
)

or

SELECT * FROM
 Table1 a 
FULL OUTER JOIN
 Table2 b 
ON
 a.ID=b.ID AND a.Name=b.NAME
WHERE
 a.ID IS NULL OR b.ID IS NULL
J-16 SDiZ
+1 for the second solution with a single select.
Lars Haugseth
`FULL OUTER JOIN` does not work in MySQL. But who care, MySQL sucks anyway :)
J-16 SDiZ
+4  A: 
create table #t1 (Id int, name varchar(50)) 
create table #t2 (Id int, name varchar(50)) 

insert #t1 values (1,       'Prashant')
insert #t1 values (2,      'Ravi')
insert #t1 values (3,       'Gaurav')
insert #t1 values (5,       'Naween')
insert #t1 values (7,       'Sachin')

insert #t2 values (1,       'Prashant')
insert #t2 values (2,       'Ravi')
insert #t2 values (4,       'Alok')
insert #t2 values (6,       'Raja')

select isnull(#t1.id, #t2.id), isnull(#t1.name,#t2.name)  from #t1 
full outer join #t2 on #t1.id = #t2.id
where #t2.Id is null or #t1.id is null

results:

3   Gaurav
5   Naween
7   Sachin
4   Alok
6   Raja
Sam Saffron
+1 good trick with "isnull" I had to use Union All to accomplish this!
TheVillageIdiot
A: 
 select t.ID, t.Name from Table1 t
 left outer join Table2 tt
 ON tt.name = t.name
 where tt.id is null

 union all

 select t.ID, t.Name from Table2 t
 left outer join Table1 tt
 ON tt.name = t.name
 where tt.id is null
TheVillageIdiot
+2  A: 

You actually CAN do it with one query:

SELECT      COALESCE(table1.ID, table2.ID) AS ID,
            COALESCE(table1.Name, table2.Name) AS Name
FROM        table1
FULL JOIN   table2
        ON  table1.ID = table2.ID
WHERE       table1.ID IS NULL OR table2.ID IS NULL

will result in:

ID          Name
----------- --------
3           Gaurav
5           Naween
7           Sachin
6           Raja
4           Alok
van
similar answer to mine :p but i managed to squeeze in the isnull as well :p
Sam Saffron
@Sam: +1: I prefer COALESCE though :)
van
+1 COALESCE is the true ANSI SQL standard, ISNULL only works on some DBs (like SQL Server)
Sam Saffron
A: 
Select Id, Name
from Table1
where Id not in (select Id from Table2)
UNION 
Select Id, Name
from Table2
where Id not in (select Id from Table1)
Rashmi Pandit