.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Code Snippets >> SQL Query >> Post New Resource Bookmark and Share   

 Subscribe to Code Snippets

INTERSECT vs INNER JOIN

Posted By:Narayanan       Posted Date: May 09, 2013    Points: 40    Category: SQL Query    URL: http://www.dotnetspark.com  

Today , we will discuss about the 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 Serve.
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

     

Further Readings:

Responses

No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More code samples in C#, ASP.Net, Vb.Net and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend