Somnath Muluk Somnath Muluk - 5 months ago 9
MySQL Question

Performance differences between equal (=) and IN with one value

How does SQL engines differ when we use equal sign and IN operator have same value? Does execution time changes?

1st one using equality check operator

WHERE column_value = 'All'


2nd one using OR operator and single value

WHERE column_value IN ('All')


Does SQL engine changes
IN
to
=
if only one value is there?

Is there any difference for same in Mysql and PostgresSQL?

Answer

There is no difference between those two statements, and the optimiser will transform the IN to the = when IN have just one element in it.

Though when you have a question like this, just run both statements, run their execution plan and see the differences. Here - you won't find any.

After a big search online, I found a document on SQL to support this(I assume it applies to all DBMS):

If there is only one value inside the parenthesis, this commend is equivalent to

WHERE "column_name" = 'value1

Here is the link to the document