Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a difference between '=' and In?

Tags:

sql

sql-server

Is there any differences (perf) writting this request:

Select * from T where PK = 1

or this

Select * from T where PK in (1)

I believe not but i realy don't know how to dispay an execution plan that should assert my feeling.

Thx in advance

like image 364
Pitming_Reloaded Avatar asked Jan 05 '11 10:01

Pitming_Reloaded


People also ask

What is the difference between '=' and in C?

First of all = is a assignment operator and == is a comparison operator. = operator is used to assign value to a variable and == operator is used to compare two variable or constants.

What is the difference between '/' and in Python?

In Python programming, you can perform division in two ways. The first one is Float Division("/") and the second is Integer Division("//") or Floor Division.

What is the difference between '=' and?

The “=” is an assignment operator is used to assign the value on the right to the variable on the left. The '==' operator checks whether the two given operands are equal or not. If so, it returns true.


2 Answers

The IN expression:

Column in (Val1,Val2,Val3)

Is internally rewritten into something very similar to:

(Column = Val1 or Column = Val2 or Column = Val3)

You can tell this because, if you supply the name of a non-existent column, the number of errors reported (Invalid Column name 'Blah') is equal to the number of values in the IN list. Of course, such re-writing only occurs for lists of literal values. Subqueries (as @oezi says) are handled differently.


Of course, this particular optimization isn't documented, and it is always preferable to write the clearest code possible.

I'm not sure if there's an upper limit where it will not perform this expansion - it will certainly do it up to 100 values in the IN list (and I can't be bothered to type in more).

like image 96
Damien_The_Unbeliever Avatar answered Oct 10 '22 03:10

Damien_The_Unbeliever


in your case: no difference

in general: you could write a subselect or inside of the braces, and = will fail if that subselect returns more than 1 row. also, you could write (1,2) (wich would be like a subselect that returns 2 rows), where = isn't possible, too.

like image 20
oezi Avatar answered Oct 10 '22 02:10

oezi