11 May 2013

INTERSECT vs INNER JOIN

 Both gave a same result. But One difference that is Column value is Null. Please see the Examples:
First create a temp Table in the Sql Server.
Example :1
Declare @m_table1 table (id int, firstName varchar(50))
Declare @m_table2 table (id int, firstName varchar(50))
-- Insert the Value
Insert into @m_table1 values (1,NULL)
Insert into @m_table2 values (1,NULL)
-- Inner Join
Select t1.*
from @m_table1 t1
inner join @m_table2 t2
On
t1.id=t2.id and t1.firstName=t2.firstName
-- INTERSECT
Select * from @m_table1
INTERSECT
Select * from @m_table2

Result for Example 1:
  Inner Join -- No data displays.
 INTERSECT -- Row displays.

Example --2:

Declare @m_table1 table (id int, firstName varchar(50))
Declare @m_table2 table (id int, firstName varchar(50))
-- Insert the Value
Insert into @m_table1 values (1,1)
Insert into @m_table2 values (1,NULL)
-- Inner Join
Select t1.*
from @m_table1 t1
inner join @m_table2 t2
On
t1.id=t2.id and t1.firstName=t2.firstName
-- INTERSECT
Select * from @m_table1
INTERSECT
Select * from @m_table2

Result for Example 1:
 Inner Join -- No data displays.
 INTERSECT -- No data displays.

I hope , you understand the difference between INTERSECT vs INNER JOIN


No comments: