The Wayback Machine - https://web.archive.org/web/20240528214440/https://www.geeksforgeeks.org/postgresql-in-operator/
Open In App

PostgreSQL – IN operator

Last Updated : 28 Aug, 2020
Improve
Improve
Like Article
Like
Save
Share
Report

The PostgreSQL IN operator is used with the WHERE clause to check against a list of values.

The syntax for using IN operator with the WHERE clause to check against a list of values which returns a boolean value depending upon the match is as below:

Syntax: value IN (value1, value2, …)

The syntax for using IN operator to return the matching values in contrast with the SELECT statement is as below:

Syntax: value IN (SELECT value FROM tbl_name);

For the sake of this article we will be using the sample DVD rental database, which is explained here and can be downloaded by clicking on this link.

Now, let’s look into a few examples.
Example 1:
Here we will make a query for the rental information of customer id 10 and 12, using the WHERE clause and IN operator.

SELECT
 customer_id,
    rental_id,
    return_date
FROM
    rental
WHERE
    customer_id IN (10, 12)
ORDER BY
    return_date DESC;

Output:

Example 2:
Here we will make a query for a list of customer id of customers that has rental’s return date on 2005-05-27.

SELECT
    first_name,
    last_name
FROM
    customer
WHERE
    customer_id IN (
        SELECT
            customer_id
        FROM
            rental
        WHERE
            CAST (return_date AS DATE) = '2005-05-27'
    );

Output:


Previous Article
Next Article

Similar Reads

PostgreSQL - Connect To PostgreSQL Database Server in Python
The psycopg database adapter is used to connect with PostgreSQL database server through python. Installing psycopg: First, use the following command line from the terminal: pip install psycopg If you have downloaded the source package into your computer, you can use the setup.py as follows: python setup.py build sudo python setup.py installCreate a
4 min read
PostgreSQL - Export PostgreSQL Table to CSV file
In this article we will discuss the process of exporting a PostgreSQL Table to a CSV file. Here we will see how to export on the server and also on the client machine. For Server-Side Export: Use the below syntax to copy a PostgreSQL table from the server itself: Syntax: COPY Table_Name TO 'Path/filename.csv' CSV HEADER; Note: If you have permissio
2 min read
PostgreSQL - Installing PostgreSQL Without Admin Rights on Windows
If you are a part of a corporation, it is highly unlikely that you have the admin privileges to install any external software. But the curious souls that all software developers are, in this article, we will see the detailed process of installation of PostgreSQL without having administrator rights on our Windows machine. Installation: Follow the be
4 min read
PostgreSQL - NOT IN operator
The PostgreSQL NOT IN operator works exactly opposite to that of the IN operator. It is used to filter query results excluding the specified values from the list of values. The syntax for using NOT IN operator with the WHERE clause to check against a list of values (except for the specified values) which returns a boolean value depending upon the m
2 min read
PostgreSQL - NOT LIKE operator
The PostgreSQL NOT LIKE works exactly opposite to that of the LIKE operator. It is used to data using pattern matching techniques that explicitly excludes the mentioned pattern from the query result set.Its result include strings that are case-sensitive and doesn't follow the mentioned pattern. It is important to know that PostgreSQL provides with
2 min read
PostgreSQL - IS NULL operator
The PostgreSQL IS NULL operator is used to check if a value is NULL or not. In the world of database Null either means data is missing or not applicable. Hence, it cannot be compared with any integer or string as this kind of comparison always leads to a Null meaning an unknown result. Syntax: Value IS NULL; For the sake of this article we will be
1 min read
PostgreSQL - LIKE operator
The PostgreSQL LIKE operator is used query data using pattern matching techniques. Its result include strings that are case-sensitive and follow the mentioned pattern. It is important to know that PostgreSQL provides with 2 special wildcard characters for the purpose of patterns matching as below: Percent ( %) for matching any sequence of character
2 min read
PostgreSQL - BETWEEN operator
PostgreSQL BETWEEN operator is used to match a value against a range of values. Syntax: value BETWEEN low AND high; Or, Syntax: value >= low and value; The BETWEEN operator is used generally with WHERE clause with association with SELECT, INSERT, UPDATE or DELETE statement. For the sake of this article we will be using the sample DVD rental databas
1 min read
PostgreSQL - UNION operator
The PostgreSQL UNION operator is used to combine result sets of multiple queries into a single set of result. It is used to combine result sets of two or more SELECT statements into a single result set. Syntax: SELECT column_1, column_2 FROM table_name_1 UNION SELECT column_1, column_2 FROM table_name_2; The below rules need to be followed while us
2 min read
PostgreSQL - INTERSECT Operator
PostgreSQL has an INTERSECT operator that is used to combine two or more result sets returned by the SELECT statement and provide with the common data among the tables into a single result set. Syntax: SELECT column_list FROM A INTERSECT SELECT column_list FROM B; The below rules must be followed while using the INTERSECT operator with the SELECT s
2 min read