Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase 12»»

IN Vs INNER JOIN Expand / Collapse
Author
Message
Posted Sunday, September 16, 2012 11:22 PM
Valued Member

Valued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued 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
Posted Monday, September 17, 2012 12:18 AM


Mr or Mrs. 500

Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500

Group: General Forum Members
Last Login: Wednesday, December 17, 2014 3:28 AM
Points: 579, Visits: 914
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
Posted Monday, September 17, 2012 2:01 AM


Right there with Babe

Right there with BabeRight there with BabeRight there with BabeRight there with BabeRight there with BabeRight there with BabeRight there with BabeRight there with Babe

Group: General Forum Members
Last Login: Friday, June 27, 2014 7:41 AM
Points: 739, Visits: 518
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
Posted Monday, September 17, 2012 3:20 AM


Mr or Mrs. 500

Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500

Group: General Forum Members
Last Login: Wednesday, December 17, 2014 3:28 AM
Points: 579, Visits: 914
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
Posted Monday, September 17, 2012 3:59 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: 2 days ago @ 8:22 AM
Points: 14,205, Visits: 28,534
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 Query Performance Tuning
and
SQL Server Execution Plans

Product Evangelist for Red Gate Software
Post #1360065
Posted Monday, September 17, 2012 4:18 AM
Valued Member

Valued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued 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
Posted Monday, September 17, 2012 4:46 AM
Old Hand

Old HandOld HandOld HandOld HandOld HandOld HandOld HandOld Hand

Group: General Forum Members
Last Login: Tuesday, September 16, 2014 2:11 AM
Points: 336, Visits: 1,155
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
Posted Monday, September 17, 2012 4:56 AM


Mr or Mrs. 500

Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500

Group: General Forum Members
Last Login: Wednesday, December 17, 2014 3:28 AM
Points: 579, Visits: 914
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
Posted Monday, September 17, 2012 4:58 AM


Mr or Mrs. 500

Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500

Group: General Forum Members
Last Login: Wednesday, December 17, 2014 3:28 AM
Points: 579, Visits: 914
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
Posted Monday, September 17, 2012 5:58 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: 2 days ago @ 8:22 AM
Points: 14,205, Visits: 28,534
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 Query Performance Tuning
and
SQL Server Execution Plans

Product Evangelist for Red Gate Software
Post #1360130
« Prev Topic | Next Topic »

Add to briefcase 12»»

Permissions Expand / Collapse