What is the difference between the following two commands?
SELECT * FROM table WHERE id IN (id1, id2, ..., idn)
and
SELECT * FROM table WHERE id = id1 OR id = id2 OR ... OR id = idn
Which one is faster? And will it be different if id is another type?
They are semantically identical.
INis just a shorthand for a string of equality statements like in your second example. Performance should also be identical.The type shouldn’t matter, it will always evaluate to a string of equalities.
There is a difference when you are using
NOT INand data that can beNULL, though – aNULLwill not evaluate false for aNOT INcomparison, so you may get rows you didn’t expect in the result set.As an example:
SELECT 'Passed!' WHERE NULL NOT IN ('foo', 'bar')The above query will not return a row even though at face value
NULLis neither'foo'or'bar'– this is becauseNULLis an unknown state, and SQL cannot say with certainty that the unknown value is NOT one of theINlisted values.