September 3rd, 2008

IN (Matching to a List)

Posted by admin in D. WHERE Clause

The IN operator is used to find if a given value matches a value in a specified list.

SELECT column1, column2, ...
  FROM table1, table2, ...
 WHERE testcolumn [NOT] IN (value1, value2, ...;

> IN works with character strings, numbers, and datetimes. The testcolumn can be an expression.
The list value must be the same comparable data type as the testcolumn.
> The IN list is contained in parentheses and contains one or more values separated by commas.
The values in the list can be in any order.
> You can combine IN conditions with other conditions using AND and OR.
> NOT IN is used to find values that are not in the list values.
> String comparisons may or may not be case sensitive depending on your RDBMS.
> The IN operator is shorter than using many OR conditions and runs faster. For example:

WHERE testcolumn IN            or  WHERE (testcolumn = value1)
      (value1, value2, value3)        OR (testcolumn = value2)
                                      OR (testcolumn = value3)

For speed, list the values most likely to find a match first.

Comments are closed.

Sorry, the comment form is closed at this time.