Solved

Is Inner Join and intersect the same?

Posted on 2014-12-21
2
119 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
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 33

Author Closing Comment

by:Mike Eghtebas
ID: 40512090
Thank you.
0

Featured Post

U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

Question has a verified solution.

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

I wrote this interesting script that really help me find jobs or procedures when working in a huge environment. I could I have written it as a Procedure but then I would have to have it on each machine or have a link to a server-related search that …
Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
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.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

863 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

19 Experts available now in Live!

Get 1:1 Help Now