EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 360+ Courses All in One Bundle
  • Login

PostgreSQL NOT IN

By Priya PedamkarPriya Pedamkar

Home » Data Science » Data Science Tutorials » PostgreSQL Tutorial » PostgreSQL NOT IN

PostgreSQL NOT IN

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:

Start Your Free Data Science Course

Hadoop, Data Science, Statistics & others

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);

PostgreSQL NOT IN-1.1

  • 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;

PostgreSQL NOT IN-1.2

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);

PostgreSQL NOT IN-1.3...

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);

Output-1.3

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;

Output-1.4

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 –

  1. PostgreSQL Datetime
  2. PostgreSQL Log Queries
  3. PostgreSQL Wal
  4. PostgreSQL Compare Strings

All in One Data Science Bundle (360+ Courses, 50+ projects)

360+ Online Courses

50+ projects

1500+ Hours

Verifiable Certificates

Lifetime Access

Learn More

0 Shares
Share
Tweet
Share
Primary Sidebar
PostgreSQL Tutorial
  • Advanced
    • PostgreSQL Schema
    • Postgres List Schemas
    • PostgreSQL VARCHAR
    • Array in PostgreSQL
    • PostgreSQL DDL
    • PostgreSQL List Users
    • Postgres Default User
    • Postgres add user
    • PostgreSQL log_statement
    • PostgreSQL String Functions
    • PostgreSQL Compare Strings
    • PostgreSQL Text Search
    • PostgreSQL TEXT
    • PostgreSQL String Array
    • PostgreSQL Constraints
    • PostgreSQL UNIQUE Constraint
    • PostgreSQL INTERSECT
    • PostgreSQL Like
    • Cursors in PostgreSQL
    • PostgreSQL UNION ALL
    • Indexes in PostgreSQL
    • PostgreSQL Index Types
    • PostgreSQL REINDEX
    • PostgreSQL UNIQUE Index
    • PostgreSQL Clustered Index
    • PostgreSQL DROP INDEX
    • PostgreSQL DISTINCT
    • PostgreSQL FETCH
    • PostgreSQL RAISE EXCEPTION
    • PostgreSQL Auto Increment
    • Sequence in PostgreSQL
    • Wildcards in PostgreSQL
    • PostgreSQL Subquery
    • PostgreSQL Alias
    • PostgreSQL LIMIT
    • PostgreSQL Limit Offset
    • PostgreSQL LAG()
    • PostgreSQL Table
    • Postgres Show Tables
    • PostgreSQL Describe Table
    • PostgreSQL Lock Table
    • PostgreSQL ALTER TABLE
    • Postgres Rename Table
    • Postgres DROP Table
    • PostgreSQL Functions
    • PostgreSQL Math Functions
    • PostgreSQL Window Functions
    • Aggregate Functions in PostgreSQL
    • PostgreSQL Primary Key
    • Foreign Key in PostgreSQL
    • PostgreSQL Procedures
    • PostgreSQL Stored Procedures
    • PostgreSQL Views
    • PostgreSQL Materialized Views
    • Postgres Create View
    • PostgreSQL Triggers
    • PostgreSQL DROP TRIGGER
    • PostgreSQL Date Functions
    • PostgreSQL TO_DATE()
    • PostgreSQL Timestamp
    • PostgreSQL CURRENT_TIMESTAMP()
    • PostgreSQL Notify
    • PostgreSQL RANK()
    • PostgreSQL Select
    • PostgreSQL Average
    • PostgreSQL DATE_PART()
    • PostgreSQL EXECUTE
    • PostgreSQL COALESCE
    • PostgreSQL EXTRACT()
    • PostgreSQL Sort
    • PostgreSQL TO_CHAR
    • PostgreSQL Interval
    • PostgreSQL Number Types
    • PostgreSQL ROW_NUMBER
    • Alter Column in PostgreSQL
    • PostgreSQL Identity Column
    • PostgreSQL SPLIT_PART()
    • PostgreSQL CONCAT()
    • PostgreSQL replace
    • PostgreSQL TRIM()
    • PostgreSQL MAX
    • PostgreSQL DELETE
    • PostgreSQL Float
    • PostgreSQL OID
    • PostgreSQL log
    • PostgreSQL REGEXP_MATCHES()
    • PostgreSQL MD5 
    • PostgreSQL NOW()
    • PostgreSQL RANDOM
    • PostgreSQL round
    • PostgreSQL Trunc()
    • PostgreSQL TIME
    • PostgreSQL IS NULL
    • PostgreSQL CURRENT_TIME
    • PostgreSQL MOD()
    • Postgresql Count
    • PostgreSQL Datetime
    • PostgreSQL MIN()
    • PostgreSQL age()
    • PostgreSQL enum
    • PostgreSQL OR
    • PostgreSQL Wal
    • PostgreSQL NOT IN
    • PostgreSQL SET
    • PostgreSQL Current Date
    • PostgreSQL Compare Date
    • PostgreSQL SERIAL
    • PostgreSQL Database
    • PostgreSQL Clone Database
    • PostgreSQL Copy Database
    • PostgreSQL Restore Database
    • PostgreSQL DROP DATABASE
    • PostgreSQL ALTER DATABASE
    • Postgres DROP Database
    • Postgres Dump Database
    • PostgreSQL OFFSET
    • PostgreSQL GRANT
    • PostgreSQL COMMIT
    • PostgreSQL ROLLUP
    • PostgreSQL JSON
    • EXPLAIN ANALYZE in PostgreSQL
    • PostgreSQL Temporary Table
    • PostgreSQL cluster
    • PostgreSQL Replication
    • PostgreSQL Logical Replication
    • PostgreSQL flush privileges
    • PostgreSQL Tablespaces
    • CAST in PostgreSQL
    • PostgreSQL CTE
    • hstore in PostgreSQL
    • PostgreSQL DECODE()
    • PostgreSQL Vacuum
    • PostgreSQL EXCLUDE
    • Postgres Change Password
    • Postgres Delete Cascade
    • PostgreSQL EXCEPT
    • PostgreSQL Roles
    • PostgreSQL Link
    • PostgreSQL Partition
    • PostgreSQL column does not exist
    • PostgreSQL Log Queries
    • PostgreSQL escape single quote
    • PostgreSQL Query Optimization
    • PostgreSQL Character Varying
    • PostgreSQL Transaction
    • PostgreSQL Extensions
    • PostgreSQL Import CSV
    • PostgreSQL Client
    • PostgreSQL caching
    • PostgreSQL JDBC Driver
    • PostgreSQL Interview Questions
  • Basic
    • What is PostgreSQL
    • PostgreSQL Features
    • How to Install PostgreSQL
    • PostgreSQL Versions
    • PostgreSQL Architecture
    • PostgreSQL GUI
    • PostgreSQL Variables
    • PostgreSQL Data Types
    • PostgreSQL NOT NULL
    • PostgreSQL Integer
    • PostgreSQL Boolean
    • PostgreSQL NULLIF
    • PostgreSQL Administration
    • PostgreSQL Commands
    • PostgreSQL Operators
    • PostgreSQL IN Operator
  • Control Statement
    • PostgreSQL IF Statement
    • PostgreSQL if else
    • PostgreSQL CASE Statement
    • PostgreSQL LOOP
    • PostgreSQL For Loop
    • PostgreSQL While Loop
  • Joins
    • Joins in PostgreSQL
    • PostgreSQL Inner Join
    • PostgreSQL Outer Join
    • LEFT OUTER JOIN in PostgreSQL
    • PostgreSQL FULL OUTER JOIN
    • PostgreSQL LEFT JOIN
    • PostgreSQL Full Join
    • PostgreSQL Cross Join
    • PostgreSQL NATURAL JOIN
    • PostgreSQL UPDATE JOIN
  • Queries
    • PostgreSQL Queries
    • PostgreSQL WHERE Clause
    • PostgreSQL WITH Clause
    • PostgreSQL ORDER BY
    • PostgreSQL ORDER BY Random
    • PostgreSQL GROUP BY
    • PostgreSQL group_concat
    • PostgreSQL HAVING
    • PostgreSQL Recursive Query
Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Database Management
  • Machine Learning
  • All Tutorials
Certification Courses
  • All Courses
  • Data Science Course - All in One Bundle
  • Machine Learning Course
  • Hadoop Certification Training
  • Cloud Computing Training Course
  • R Programming Course
  • AWS Training Course
  • SAS Training Course

© 2020 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA
Free Data Science Course

Hadoop, Data Science, Statistics & others

*Please provide your correct email id. Login details for this Free course will be emailed to you
Book Your One Instructor : One Learner Free Class

Let’s Get Started

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA Login

Forgot Password?

EDUCBA
Free Data Science Course

Hadoop, Data Science, Statistics & others

*Please provide your correct email id. Login details for this Free course will be emailed to you

Special Offer - All in One Data Science Bundle (360+ Courses, 50+ projects) Learn More