Definition of PostgreSQL NOT IN
PostgreSQL NOT IN condition is the combination of NOT and IN condition, NOT IN condition in PostgreSQL will return the values which were not found in the specified column from which column we have searching. We can use the NOT IN condition with Subquery in PostgreSQL, we can find the result excluding values which was we have used in select query. We have also used the NOT operator with IN condition to retrieve the same result from the table.
Syntax:
Below is the syntax of the NOT IN operator in PostgreSQL.
select column_name1, column_name2, column_name3, …, column_nameN from name_of_table Where column_name NOT IN(value1, value2, value3, …, valueN);
Parameter:
Below is the parameter description syntax of the NOT IN operator in PostgreSQL.
- Select: We have select single or multiple columns while using the NOT IN condition in PostgreSQL. Using select statements we have to retrieve results by using the NOT IN condition in PostgreSQL.
- From: This keyword is used to select the table name to use the NOT IN operator in PostgreSQL. We have used from keyword to specify the column name in PostgreSQL.
- Table name: We can define the table name from which we have retrieving data by using the NOT IN condition in PostgreSQL. We have used any table to retrieve data using NOT IN condition.
- Where condition: This condition is used to specify the column condition using the NOT IN condition in PostgreSQL.
- NOT IN: This condition in PostgreSQL is defined as retrieve value from the table by using the NOT IN condition in PostgreSQL.
- Value 1 to value N: We have excluded this value from the output to display the specified result by using the NOT IN condition.
How NOT IN Condition Work in PostgreSQL?
- Below is the working of the NOT IN condition in PostgreSQL. We have to use the NOT IN condition basically we have to exclude the specified value result from the output.
- If we want to exclude specified values from the column output then we have used the NOT IN condition in PostgreSQL.
- If suppose we have to exclude some student roll no from student table then we have used NOT IN condition in PostgreSQL.
- To execute the NOT condition with the select statement in PostgreSQL we need to have select privileges on the table or we need to have super user privileges to execute the NOT IN condition statement.
- Below is the example of NOT IN condition with a select statement require select privileges on a table or super user privileges to execute the NOT IN condition with the select statement in PostgreSQL.
psql -U db_test -d testing
select * from stud1 where id NOT IN (1, 2, 3, 4, 5);
psql -U postgres -d testing
select * from stud1 where id NOT IN (1, 2, 3, 4, 5);
- In the above first example, we have used the user as db_test, this user doesn’t have privileges of select student table or super user so, it will issue an error while executing the select statement.
- In the second example, we have select the stud1 table rows using the username as Postgres, after using this user we have to select the data from the stud1 table.
- NOT IN condition is basically used to retrieve data from the table by excluding specified values from the column.
- We can also use the NOT IN condition with not equal and the AND operator in PostgreSQL. We can also write the NOT IN query by using the not equal and the AND operator.
- Using not equal and the AND operator returns the same output as a return by the NOT IN condition in PostgreSQL.
Examples of PostgreSQL NOT IN
- Below is an example of NOT IN condition in PostgreSQL. We have using stud1 table to describe the example of the NOT IN condition in PostgreSQL.
- Below is the table and data description of stud1 table.
\d+ stud1;
select * from stud1;
1. NOT IN condition by selecting single column
- In the below example, we have used a single column with NOT in condition. We have selected id column from the stud1 table.
select id from stud1 where id NOT IN (1, 2, 3, 4, 5);
2. NOT IN condition by selecting all column
- In the below example, we have used all columns with NOT in condition. We have selected all columns from the stud1 table.
select * from stud1 where id NOT IN (4, 5);
3. NOT IN condition by using not equal and the AND operator
- In the below example, we have used the NOT IN condition by using not equal and the AND operator. We have selected all columns from the stud1 table.
select * from stud1 where id <> 12 AND id <> 10 AND id <> 10;
Advantages
Some of the advantages are:
- Below are the advantages of the NOT IN condition in PostgreSQL.
- NOT IN condition is exclude the specific value from a table.
- We can use equal to and the AND operator to use NOT IN condition.
- We can select single as well as multiple columns with NOT IN condition.
- We can also use NOT IN condition with Subquery.
Recommended Articles
This is a guide to PostgreSQL NOT IN. Here we also discuss the definition and how not in condition work in postgresql? along with different examples and its code implementation. You may also have a look at the following articles to learn more –