Solved

Is Inner Join and intersect the same?

Posted on 2014-12-21
2
134 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 22

Accepted Solution

by:
Steve Wales earned 500 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

Get MongoDB database support online, now!

At Percona’s web store you can order your MongoDB database support needs in minutes. No hassles, no fuss, just pick and click. Pay online with a credit card. Handle your MongoDB database support now!

Question has a verified solution.

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

In this article we will learn how to fix  “Cannot install SQL Server 2014 Service Pack 2: Unable to install windows installer msi file” error ?
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 …
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
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.

615 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