- Categories:
[ NOT ] IN¶
Tests whether its argument is or is not one of the members of an explicit list or the result of a subquery.
Note
In subquery form, IN is equivalent to = ANY and NOT IN is equivalent to <> ALL.
Tip
You can use the search optimization service to improve the performance of queries that call this function. For details, see Search optimization service.
Syntax¶
To compare individual values:
To compare row constructors (parenthesized lists of values):
To compare a value to the values returned by a subquery:
Parameters¶
valueThe value for which to search.
value_A,value_BThe elements of a row constructor for which to search.
Ensure that each value on the right of IN (for example,
(value3, value4)) has the same number of elements as the value on the left of IN (for example,(value_A, value_B)).value_#A value to which
valueshould be compared.If the values to compare to are row constructors, then each
value_#is an individual element of a row constructor.subqueryA subquery that returns a list of values to which
valuecan be compared.
Usage notes¶
As in most contexts, NULL is not equal to NULL. If
valueis NULL, then the return value of the function is NULL, whether or not the list or subquery contains NULL. See Using NULL.Syntactically, IN is treated as an operator rather than a function. This example shows the difference between using IN as an operator and calling
f()as a function:You can’t use function syntax with IN. For example, you can’t rewrite the preceding example as:
IN is also considered a subquery operator.
In a query that uses IN, you can expand an array into a list of individual values by using the spread operator (
**). For more information and examples, see Expansion operators.
Collation details¶
Arguments with collation specifications currently aren’t supported.
Examples¶
The following examples use the IN function.
Using IN with simple literals¶
The following examples show how to use IN and NOT IN with simple literals:
Using IN with a subquery¶
These example shows how to use IN in a subquery.
Using IN with a table¶
These examples show how to use IN with a table. The statement below creates the table used in the examples.
This example shows how to use IN with a single column of a table:
This example shows how to use IN with multiple columns of a table:
This example shows how to use IN with a subquery that reads multiple columns of a table:
Using NULL¶
Remember that NULL != NULL. IN and NOT IN lists that contain comparisons with NULL (including equality conditions) might produce unexpected results because NULL represents an unknown value. Comparisons with NULL do not return TRUE or FALSE; they return NULL. See also Ternary logic.
For example, the following query returns NULL, not TRUE, because SQL cannot determine whether NULL equals any value, including another NULL.
Note that if you change the query to select 1, not NULL, it returns TRUE:
In this case, the result is TRUE because 1 does have a match in the IN list. The fact that NULL also exists
in the IN list doesn’t affect the result.
Similarly, NOT IN comparisons with NULL also return NULL if any value in the list is NULL.
The same behavior is true for the following query, where the set of values 4, 5, NULL does not match either 4, 5, NULL or 7, 8, 9:
The following example shows the same behavior with NULL comparisions but uses a subquery to define the IN list values that are compared: