Solved

Is Inner Join and intersect the same?

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

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
T-SQL: Episode III - Revenge of The Dude 24 53
How to share SSIS Package? 6 37
This query failed in sql 2014 5 30
Create snapshot on MSSQL 2012 3 18
Everyone has problem when going to load data into Data warehouse (EDW). They all need to confirm that data quality is good but they don't no how to proceed. Microsoft has provided new task within SSIS 2008 called "Data Profiler Task". It solve th…
JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.

770 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