PostgreSQL IN
Summary: in this tutorial, you will learn how to use the PostgreSQL IN operator to check if a value matches any value in a list.
Introduction to PostgreSQL IN operator
The IN
operator allows you to check whether a value matches any value in a list of values.
Here’s the basic syntax of the IN
operator:
The IN
operator returns true if the value
is equal to any value in the list such as value1
and value2
.
The list of values can be a list of literal values including numbers and strings.
In addition to literal values, the IN
operator also accepts a list of values returned from a query. You’ll learn more about how to use the IN
operator with a query in the subquery tutorial.
Functionally, the IN
operator is equivalent to combining multiple boolean expressions with the OR operators:
PostgreSQL IN operator examples
We’ll use the film
table from the sample database:
1) Using the PostgreSQL IN operator with a list of numbers
The following example uses the IN
operator to retrieve information about the film with id 1, 2, and 3:
Output:
The following statement uses the equal (=
) and OR
operators instead of the IN
operator, which is equivalent to the query above:
The query that uses the IN
operator is shorter and more readable than the query that uses equal (=
) and OR
operators.
Additionally, PostgreSQL executes the query with the IN
operator much faster than the same query that uses a list of OR
operators.
2) Using the PostgreSQL IN operator with a list of strings
We’ll use the actor
table from the sample database:
The following example uses the IN
operator to find the actors who have the last name in the list 'Allen'
, 'Chase'
, and 'Davis'
:
Output:
3) Using the PostgreSQL IN operator with a list of dates
The following statement uses the IN operator to find payments whose payment dates are in a list of dates: 2007-02-15
and 2007-02-16
:
Output:
In this example, the payment_date
column has the type timestamp
that consists of both date and time parts.
To match the values in the payment_date
column with a list of dates, you need to cast them to date values that have the date part only.
To do that you use the ::
cast operator:
For example, if the timestamp value is 2007-02-15 22:25:46.996577
, the cast operator will convert it to 2007-02-15
.
PostgreSQL NOT IN operator
To negate the IN
operator, you use the NOT IN
operator. Here’s the basic syntax of the NOT IN
operator:
The NOT IN
operator returns true
if the value
is not equal to any value in the list such as value1
and value2
; otherwise, the NOT IN
operator returns false
.
The NOT IN
operator is equivalent to a combination of multiple boolean expressions with the AND operators:
PostgreSQL NOT IN operator example
The following example uses the NOT IN
operator to retrieve films whose id is not 1, 2, or 3:
Output:
The following query retrieves the same set of data but uses the not-equal (<>
) and AND
operators:
Summary
- Use the
IN
operator to check if a value matches any value in a list of values. - Use the
NOT
operator to negate theIN
operator.