Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Is Inner Join and intersect the same?

Posted on 2014-12-21
2
Medium Priority
?
146 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 23

Accepted Solution

by:
Steve Wales earned 2000 total points
ID: 40512071
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 34

Author Closing Comment

by:Mike Eghtebas
ID: 40512090
Thank you.
0

Featured Post

Ask an Anonymous Question!

Don't feel intimidated by what you don't know. Ask your question anonymously. It's easy! Learn more and upgrade.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In the first part of this tutorial we will cover the prerequisites for installing SQL Server vNext on Linux.
What if you have to shut down the entire Citrix infrastructure for hardware maintenance, software upgrades or "the unknown"? I developed this plan for "the unknown" and hope that it helps you as well. This article explains how to properly shut down …
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
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.

773 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