Solved

Is Inner Join and intersect the same?

Posted on 2014-12-21
2
118 Views
Last Modified: 2014-12-21
Query 1: Select * From Table1 inner join Table2 On Table1.ID = Table2.ID
Query 2: Select * From Table1 Intersect Table2

Are these two queries valid? If so, do they give the same result? And, why should we use one but not the other?

One things the clear, there is no need for ID field in Query 2.
0
Comment
Question by:Mike Eghtebas
2 Comments
 
LVL 22

Accepted Solution

by:
Steve Wales earned 500 total points
Comment Utility
Intersect is not quite the same.  The number and order of columns from the tables in the query must be the same and the data types must be compatible or the same with Intersect.

Then the database engine will return any rows that appear in both queries as a single row in the result set.

So, I suppose, for certain tables that are the same - maybe a master table and a history table that have the same layout, it might work the same as an inner join, but for the most part, probably not so.

You couldn't use intersect on tableA with 4 columns and tableB with 3 columns.

For me - there's also that added benefit of legibility later on - by specifying the join condition, you can see exactly what the two tables are joined on and it makes debugging the code later easier - you don't have to go check the structures of the two tables to make sure they are the same.

On top of that, should the structure of one of the tables change without the same change being applied to the other your query breaks.

Intersect behaves similarly to a Union - intersect only returns the matches, where as union returns all rows.

Also (without testing), I'm pretty certain that "select *" used with a join will return you all columns from tableA and also all columns from tableB.   (so 4 columns in each table, you'd have 8 columns output).

When using "select *" from intersect you're just seeing the columns that match in the result set of each query (so your output, based upon the same example) is 4 columns.

As always, have a read of the documentation on Intersect from the SQL Server Books Online: http://msdn.microsoft.com/en-us/library/ms188055.aspx
0
 
LVL 33

Author Closing Comment

by:Mike Eghtebas
Comment Utility
Thank you.
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Introduced in Microsoft SQL Server 2005, the Copy Database Wizard (http://msdn.microsoft.com/en-us/library/ms188664.aspx) is useful in copying databases and associated objects between SQL instances; therefore, it is a good migration and upgrade tool…
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

7 Experts available now in Live!

Get 1:1 Help Now