Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6679211
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T04:20:00+00:00 2026-05-26T04:20:00+00:00

Given: Table y id int clustered index name nvarchar(25) Table anothertable id int clustered

  • 0

Given:

Table y

  • id int clustered index
  • name nvarchar(25)

Table anothertable

  • id int clustered Index
  • name nvarchar(25)

Table someFunction

  • does some math then returns a valid ID

Compare:

SELECT y.name
  FROM y
 WHERE dbo.SomeFunction(y.id) IN (SELECT anotherTable.id 
                                    FROM AnotherTable)

vs:

SELECT y.name 
  FROM y
  JOIN AnotherTable ON dbo.SomeFunction(y.id) ON anotherTable.id

Question:

While timing these two queries out I found that at large data sets the first query using IN is much faster then the second query using an INNER JOIN. I do not understand why can someone help explain please.

Execution Plan

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T04:20:01+00:00Added an answer on May 26, 2026 at 4:20 am

    Generally speaking IN is different from JOIN in that a JOIN can return additional rows where a row has more than one match in the JOIN-ed table.

    From your estimated execution plan though it can be seen that in this case the 2 queries are semantically the same

    SELECT
            A.Col1
            ,dbo.Foo(A.Col1)
            ,MAX(A.Col2)
            FROM A
            WHERE dbo.Foo(A.Col1)  IN (SELECT Col1 FROM B)
        GROUP BY
            A.Col1,
            dbo.Foo(A.Col1)
    

    versus

    SELECT
            A.Col1
            ,dbo.Foo(A.Col1)
            ,MAX(A.Col2)
            FROM A
            JOIN B ON dbo.Foo(A.Col1) = B.Col1
        GROUP BY
            A.Col1,
            dbo.Foo(A.Col1)     
    

    Even if duplicates are introduced by the JOIN then they will be removed by the GROUP BY as it only references columns from the left hand table. Additionally these duplicate rows will not alter the result as MAX(A.Col2) will not change. This would not be the case for all aggregates however. If you were to use SUM(A.Col2) (or AVG or COUNT) then the presence of the duplicates would change the result.

    It seems that SQL Server doesn’t have any logic to differentiate between aggregates such as MAX and those such as SUM and so quite possibly it is expanding out all the duplicates then aggregating them later and simply doing a lot more work.

    The estimated number of rows being aggregated is 2893.54 for IN vs 28271800 for JOIN but these estimates won’t necessarily be very reliable as the join predicate is unsargable.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Given a table structure like this: CREATE TABLE `user` ( `id` int(10) unsigned NOT
Here's what I want to do: Given a table PeopleOutfit (id int primary key,
Given the following: declare @a table ( pkid int, value int ) declare @b
Given these tables: create table Orders ( Id INT IDENTITY NOT NULL, primary key
Given a table of structure: CREATE TABLE [dbo].[Example]( [FiscalYear] INT NOT NULL, [Hours] [decimal](28,
I have a organization name table with the following structure given below: CREATE TABLE
Given this table: CREATE TABLE [Comments] ( [Id] [int] IDENTITY(1, 1) NOT NULL, [Text]
Given a table: CREATE TABLE [GENERIC_TABLE] ( [RECORD_ID] [int] IDENTITY(1,1) NOT NULL, [SHORT_DESC] [varchar]
given this table and data: DECLARE @Table table (RowID int, RowCode char(1), RowValue int);set
Given the table: CREATE TABLE Table1 ( UniqueID int IDENTITY(1,1) ...etc ) Now why

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.