Log in
::
Register
::
Not logged in
Home
Tags
Articles
Editorials
Stairways
Forums
Scripts
Videos
Blogs
QotD
Books
Ask SSC
SQL Jobs
Training
Authors
About us
Contact us
Newsletters
Write for us
Recent Posts
Recent Posts
Popular Topics
Popular Topics
Home
Search
Members
Calendar
Who's On
Home
»
SQL Server 2008
»
SQL Server 2008 - General
»
IN Vs INNER JOIN
17 posts, Page 1 of 2
1
2
»»
IN Vs INNER JOIN
Rate Topic
Display Mode
Topic Options
Author
Message
purushottam2
purushottam2
Posted Sunday, September 16, 2012 11:22 PM
Valued Member
Group: General Forum Members
Last Login: Monday, April 29, 2013 1:59 AM
Points: 53,
Visits: 101
I want to know which query is more optimized?
SELECT 1 FROM ABC WHERE Id IN (SELECT Id FROM ABC1)
===================================================
SELECT 1 FROM ABC a JOIN ABC1 b ON a.Id = b.Id
Thanks
Puru
Post #1359973
rhythmk
rhythmk
Posted Monday, September 17, 2012 12:18 AM
Old Hand
Group: General Forum Members
Last Login: Saturday, November 10, 2012 9:30 AM
Points: 388,
Visits: 603
purushottam2 (9/16/2012)
I want to know which query is more optimized?
SELECT 1 FROM ABC WHERE Id IN (SELECT Id FROM ABC1)
===================================================
SELECT 1 FROM ABC a JOIN ABC1 b ON a.Id = b.Id
Thanks
Puru
Actually depends upon the number of records in the table.
--rhythmk
------------------------------------------------------------------
To post your question use below link
http://www.sqlservercentral.com/articles/Best+Practices/61537/
Post #1359986
Robin Sasson
Robin Sasson
Posted Monday, September 17, 2012 2:01 AM
Say Hey Kid
Group: General Forum Members
Last Login: Monday, February 18, 2013 6:17 AM
Points: 674,
Visits: 472
Its also depends on whether the columns you are joining on are indexed.
Indexing and performance is a large topic in it's own right.
Too many indexes on a large table may slow down INSERT, UPDATE and DELETE whilst giving improvement to SELECT.
Where possible, I prefer JOINS from a readability point of view.
"Be brave. Take risks. Nothing can substitute experience."
Post #1360014
rhythmk
rhythmk
Posted Monday, September 17, 2012 3:20 AM
Old Hand
Group: General Forum Members
Last Login: Saturday, November 10, 2012 9:30 AM
Points: 388,
Visits: 603
Robin Sasson (9/17/2012)
Its also depends on whether the columns you are joining on are indexed.
Indexing and performance is a large topic in it's own right.
Too many indexes on a large table may slow down INSERT, UPDATE and DELETE whilst giving improvement to SELECT.
Where possible, I prefer JOINS from a readability point of view.
But in many places I have observed that IN beats INNER JOIN in performance.
--rhythmk
------------------------------------------------------------------
To post your question use below link
http://www.sqlservercentral.com/articles/Best+Practices/61537/
Post #1360045
Grant Fritchey
Grant Fritchey
Posted Monday, September 17, 2012 3:59 AM
SSChampion
Group: General Forum Members
Last Login: Yesterday @ 9:49 AM
Points: 13,436,
Visits: 25,281
Thing is, those are two different queries. The IN statement can act to filter the results of ABC, but none of the columns from ABC1 are available in the SELECT or WHERE clause of the statement using IN. The JOIN statement is combining two tables, so you'll get more data returned. So we're actually not comparing the same things here. These are not equivalent statements and result sets that can be realistically compared, one to the other.
----------------------------------------------------
"The credit belongs to the man who is actually in the arena, whose face is marred by dust and sweat and blood..." Theodore Roosevelt
The Scary DBA
Author of:
SQL Server 2012 Query Performance Tuning
SQL Server 2008 Query Performance Tuning Distilled
and
SQL Server Execution Plans
Product Evangelist for
Red Gate Software
Post #1360065
purushottam2
purushottam2
Posted Monday, September 17, 2012 4:18 AM
Valued Member
Group: General Forum Members
Last Login: Monday, April 29, 2013 1:59 AM
Points: 53,
Visits: 101
Agree, Suppose ABC is master tables and ABC1 is child table, in this case IN and Inner Join both will return the same data.
Post #1360076
yuvipoy
yuvipoy
Posted Monday, September 17, 2012 4:46 AM
SSC-Enthusiastic
Group: General Forum Members
Last Login: Friday, May 24, 2013 2:53 AM
Points: 111,
Visits: 517
An inner join between two tables does a complete join, it checks for matches and returns rows. This means, if there are multiple matching rows in the second table, multiple rows will be returned. Also, when two tables are joined, columns can be returned from either.
With an IN, what is done is a semi-join, a join that checks for matches but does not return rows. This means if there are multiple matching tables in the resultset used for the IN, it doesn’t matter. Only one row from the first table will be returned. Also, because the rows are not returned, columns from the table referenced in the IN cannot be returned.
Thanks!
Post #1360091
rhythmk
rhythmk
Posted Monday, September 17, 2012 4:56 AM
Old Hand
Group: General Forum Members
Last Login: Saturday, November 10, 2012 9:30 AM
Points: 388,
Visits: 603
Grant Fritchey (9/17/2012)
Thing is, those are two different queries. The IN statement can act to filter the results of ABC, but none of the columns from ABC1 are available in the SELECT or WHERE clause of the statement using IN. The JOIN statement is combining two tables, so you'll get more data returned. So we're actually not comparing the same things here. These are not equivalent statements and result sets that can be realistically compared, one to the other.
Agreed...but OP is asking about the scenario where both IN and INNER JOIN returns same result.
--rhythmk
------------------------------------------------------------------
To post your question use below link
http://www.sqlservercentral.com/articles/Best+Practices/61537/
Post #1360097
rhythmk
rhythmk
Posted Monday, September 17, 2012 4:58 AM
Old Hand
Group: General Forum Members
Last Login: Saturday, November 10, 2012 9:30 AM
Points: 388,
Visits: 603
Just found below article from Gail on the same
http://sqlinthewild.co.za/index.php/2010/01/12/in-vs-inner-join/
--rhythmk
------------------------------------------------------------------
To post your question use below link
http://www.sqlservercentral.com/articles/Best+Practices/61537/
Post #1360100
Grant Fritchey
Grant Fritchey
Posted Monday, September 17, 2012 5:58 AM
SSChampion
Group: General Forum Members
Last Login: Yesterday @ 9:49 AM
Points: 13,436,
Visits: 25,281
yuvipoy (9/17/2012)
An inner join between two tables does a complete join, it checks for matches and returns rows. This means, if there are multiple matching rows in the second table, multiple rows will be returned. Also, when two tables are joined, columns can be returned from either.
With an IN, what is done is a semi-join, a join that checks for matches but does not return rows. This means if there are multiple matching tables in the resultset used for the IN, it doesn’t matter. Only one row from the first table will be returned. Also, because the rows are not returned, columns from the table referenced in the IN cannot be returned.
Thanks!
Copied from Gail's blog. Not exactly the right thing to do unless you also provide the link. It smacks of plagiarism and is most definitely frowned on around here.
----------------------------------------------------
"The credit belongs to the man who is actually in the arena, whose face is marred by dust and sweat and blood..." Theodore Roosevelt
The Scary DBA
Author of:
SQL Server 2012 Query Performance Tuning
SQL Server 2008 Query Performance Tuning Distilled
and
SQL Server Execution Plans
Product Evangelist for
Red Gate Software
Post #1360130
« Prev Topic
|
Next Topic »
17 posts, Page 1 of 2
1
2
»»
Permissions
You
cannot
post new topics.
You
cannot
post topic replies.
You
cannot
post new polls.
You
cannot
post replies to polls.
You
cannot
edit your own topics.
You
cannot
delete your own topics.
You
cannot
edit other topics.
You
cannot
delete other topics.
You
cannot
edit your own posts.
You
cannot
edit other posts.
You
cannot
delete your own posts.
You
cannot
delete other posts.
You
cannot
post events.
You
cannot
edit your own events.
You
cannot
edit other events.
You
cannot
delete your own events.
You
cannot
delete other events.
You
cannot
send private messages.
You
cannot
send emails.
You
may
read topics.
You
cannot
rate topics.
You
cannot
vote within polls.
You
cannot
upload attachments.
You
may
download attachments.
You
cannot
post HTML code.
You
cannot
edit HTML code.
You
cannot
post IFCode.
You
cannot
post JavaScript.
You
cannot
post EmotIcons.
You
cannot
post or upload images.
Copyright © 2002-2013 Simple Talk Publishing. All Rights Reserved.
Privacy Policy.
Terms of Use.
Report Abuse.