Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL Server: Select in vs or?

Tags:

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
like image 498
ctrlShiftBryan Avatar asked Jan 21 '10 16:01

ctrlShiftBryan


People also ask

Which is more efficient SQL in OR?

The best way to know is to profile both on your database with your specific data to see which is faster. So in this case the method using OR is about 30% slower. Adding more terms makes the difference larger. Results may vary on other databases and on other data.

Can we use OR in SELECT query?

The SQL OR condition is used to test multiple conditions in a SELECT, INSERT, UPDATE, or DELETE statement. Any one of the conditions must be met for a record to be selected.

Which is better in OR join in SQL?

If the joining column is UNIQUE and marked as such, both these queries yield the same plan in SQL Server . If it's not, then IN is faster than JOIN on DISTINCT .


1 Answers

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.

like image 131
Paul Sasik Avatar answered Oct 12 '22 17:10

Paul Sasik