-4

Possible Duplicate:
MYSQL OR vs IN performance

I was wondering whats the difference between:

SELECT * FROM table WHERE cat = 'cat1' OR cat = 'cat2' OR cat = 'cat3'

And:

SELECT * FROM table WHERE cat in ('cat1', 'cat2', 'cat3')

Is there any difference? As I tried they both give out same result.

Community
  • 1
  • 1
ChrisMe
  • 55
  • 5

3 Answers3

2

They are identical the IN() is just a short-hand version using listing out all of the OR statements.

Taryn
  • 242,637
  • 56
  • 362
  • 405
1

IN() just makes for a much shorter, and easier to read, syntax especially when you have a lot of OR clauses.

John Conde
  • 217,595
  • 99
  • 455
  • 496
0

There is one more thing that you can do quite easily with IN but you can't with =.

SELECT * 
FROM `table`
WHERE `column` IN (
    SELECT col_name
    FROM `table2` -- or the same table
    WHERE `some_column` = 5
)

So basically you search in a subset of another table, which sometimes comes in handy.

Real life usage:

  • You have a list of administrable user types (each with its own permissions) in another table, and you want to enforce that the user type actually exists.
Vlad Preda
  • 9,780
  • 7
  • 36
  • 63