PostgreSQL – NOT IN operator
Last Updated :
23 Oct, 2024
PostgreSQL NOT IN condition is a powerful tool for data retrieval in PostgreSQL by allowing users to filter out specific values from their query results. This condition is particularly useful when we want to exclude a defined set of values from a dataset by making our queries more efficient and targeted.
What is the NOT IN Operator?
- The
NOT IN
operator in PostgreSQL allows us to filter out rows that match a list of specified values.
- When used in a
WHERE
clause, it ensures that the returned results do not include any of the values within the specified list.
Syntax:
value NOT IN (value1, value2, ...)
Explanation: The syntax for using the NOT IN operator to return the matching values(except for the specified values) in contrast with the SELECT statement is as below:
value NOT IN (SELECT value FROM tbl_name)
Examples of PostgreSQL NOT IN operator
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 of the NOT IN operator in PostgreSQL to better understand the concept.
Example 1: Excluding Specific Customer IDs in Rentals
Here we will query for all rentals where the ‘customer_id’ is not 10 or 12, from the ‘rental’ table in our sample database.
SELECT
customer_id,
rental_id,
return_date
FROM
rental
WHERE
customer_id NOT IN (10, 12);
Output:

Explanation: This query will return all rows from the ‘rental'
table where the ‘customer_id'
is neither 10 nor 12, displaying the ‘customer_id'
, ‘rental_id'
, and ‘return_date'
.
Example 2: Excluding Specific Customer IDs in Customers Table
Here we will query for all customers where the ‘customer_id’ is not 10 or 12, from the ‘customer‘ table in our sample database.
SELECT
customer_id,
first_name,
last_name
FROM
customer
WHERE
customer_id NOT IN (10, 12);
Output:

Explanation: This query will return all rows from the customer
table where the ‘customer_id'
is neither 10 nor 12, displaying the ‘customer_id'
, ‘first_name'
, and ‘last_name'
.
Important Points About PostgreSQL NOT IN Operator
Conclusion
Overall, understanding the PostgreSQL NOT IN condition and its corresponding syntax enables users to perform precise data filtering within their queries. By excluding unwanted values, users can streamline their results and enhance data analysis capabilities.
Similar Reads
PostgreSQL - NOT LIKE operator
The PostgreSQL NOT LIKE operator is a powerful tool used in SQL queries to filter out rows that do not match specific patterns. By utilizing this operator, users can eliminate undesired string patterns from their results, enabling more precise data retrieval. It is particularly beneficial in scenari
3 min read
PostgreSQL - IN Operator
The IN operator in PostgreSQL is a powerful and efficient tool used to filter records based on a predefined set of values. When used with the WHERE clause, it simplifies SQL queries and enhances readability, making it a key component of SQL query optimization for data retrieval and database manipula
4 min read
PostgreSQL - NOT BETWEEN operator
PostgreSQL NOT BETWEEN operator is used to match all values against a range of values excluding the values in the mentioned range itself. Syntax: value NOT BETWEEN low AND high; Or, Syntax: value high; The NOT BETWEEN operator is used generally with WHERE clause with association with SELECT, INSERT,
1 min read
PostgreSQL - IS NULL operator
The PostgreSQL IS NULL operator is used to check whether a value is NULL. In the context of databases, NULL indicates that data is either missing or not applicable. Since NULL cannot be compared directly with any integer or string (as such comparisons result in NULL, meaning an unknown result), the
2 min read
PostgreSQL - ANY Operator
The ANY operator in PostgreSQL is a powerful tool for comparing a scalar value against a set of values returned by a subquery. From this article, we can better understand the ANY Operator in PostgreSQL. Syntaxexpression operator ANY(subquery)Rules of ANY Operator The below rules must be followed whi
3 min read
PostgreSQL - EXISTS Operator
The EXISTS operator in PostgreSQL is a powerful SQL feature used to check the existence of rows in a subquery. It is particularly useful when working with correlated subqueries, where the inner query depends on values from the outer query. The EXISTS operator returns true if the subquery returns at
4 min read
SQL NOT IN Operator
The NOT IN operator in SQL is used to exclude a specified set of values in a query, making code more readable and efficient. It is often combined with SELECT, UPDATE, and DELETE statements to filter out rows that match any value in a given list. This operator is a more intuitive alternative to using
4 min read
PostgreSQL - INTERSECT Operator
The PostgreSQL INTERSECT operator is used to combine two or more result sets returned by SELECT statements and return the common data among the tables into a single result set. This is useful for identifying overlapping data between tables. Let us better understand the INTERSECT Operator of PostgreS
3 min read
PostgreSQL - LIKE operator
In PostgreSQL, the LIKE operator is an essential tool for pattern matching in SQL queries. Whether we're dealing with large datasets or searching for specific string patterns, this operator provides a powerful way to filter and retrieve data based on partial matches. By Using wildcard search techniq
5 min read
PostgreSQL - ALL Operator
The PostgreSQL ALL operator is a powerful tool for comparing a value with a list of values returned by a subquery. This operator is essential for filtering and querying data based on comparisons with multiple values, making it a valuable addition to any PostgreSQL user's toolkit. Let us better under
3 min read