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 match is as below:
Syntax: value NOT IN (value1, value2, …)
The syntax for using NOT IN operator to return the matching values(except for the specified values) in contrast with the SELECT statement is as below:
Syntax: value NOT IN (SELECT value FROM tbl_name);
Now, let’s look into a few examples.
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);
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);
- PostgreSQL - NOT LIKE operator
- PostgreSQL - NOT BETWEEN operator
- PostgreSQL - Connect To PostgreSQL Database Server in Python
- PostgreSQL - IN operator
- PostgreSQL - ILIKE operator
- PostgreSQL - IS NULL operator
- PostgreSQL - LIKE operator
- PostgreSQL - BETWEEN operator
- PostgreSQL - UNION operator
- PostgreSQL - INTERSECT Operator
- PostgreSQL - EXCEPT Operator
- PostgreSQL - ANY Operator
- PostgreSQL - SOME Operator
- PostgreSQL - ALL Operator
- PostgreSQL - EXISTS Operator
- PostgreSQL - NOT NULL Constraint
- What is PostgreSQL - Introduction
- Install PostgreSQL on Windows
- Install PostgreSQL on Mac
- PostgreSQL - Loading a Database
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.