SQL Server: Select in vs or?

11,930

Solution 1

Due to Sql Server's optimization of queries these will run at the same speed since they are logically equivalent.

i favor the IN syntax for brevity and readability though.

Solution 2

Actually it is the same.

If you display the estimated execution plan you will see that it is performing the same action.

Share:
11,930
MCF
Author by

MCF

Updated on June 04, 2022

Comments

  • MCF
    MCF about 2 years

    Which is faster?

    SELECT UserName
    FROM dbo.UserTable
    WHERE UserID in (1,3,4)
    

    SELECT UserName
    FROM dbo.UserTable
    WHERE UserID = 1 
          OR UserID = 3
          OR UserID = 4
    
  • Worgon
    Worgon about 11 years
    Rather then Or/In is there any way to query? that will perform faster?
  • Martin Smith
    Martin Smith almost 10 years
    @Worgon if the number of values is large you could put the distinct values into an indexed temp table and join on that.
  • tbodt
    tbodt almost 10 years
    Can you please clarify that? I have no idea what you are saying.