EDUCBA Logo

EDUCBA

MENUMENU
  • Explore
    • EDUCBA Pro
    • PRO Bundles
    • All Courses
    • All Specializations
  • Blog
  • Enterprise
  • Free Courses
  • All Courses
  • All Specializations
  • Log in
  • Sign Up
Home Data Science Data Science Tutorials PostgreSQL Tutorial PostgreSQL EXCEPT
 

PostgreSQL EXCEPT

Sohel Sayyad
Article bySohel Sayyad
EDUCBA
Reviewed byRavi Rathore

Updated May 18, 2023

PostgreSQL EXCEPT

 

 

Introduction to PostgreSQL EXCEPT

The PostgreSQL EXCEPT operator is used with two SELECT statements. Let’s consider as a first SELECT statement and second SELECT statement, in order to return all of the records from the first SELECT statement which are not returned from the second SELECT statement, we generally use the PostgreSQL EXCEPT operator. The SELECT statement defined with the PostgreSQL EXCEPT operator defines a dataset. It retrieves all records from the result of the first SELECT statement’s dataset and then removes all of the records of the second SELECT statement’s dataset from the result of the first SELECT statement’s dataset.

Watch our Demo Courses and Videos

Valuation, Hadoop, Excel, Mobile Apps, Web Development & many more.

Syntax:

SELECT column_name_1, column_name_2, ... column_name_n
FROM table_1
[WHERE conditions_table1]
EXCEPT
SELECT column_name_1, column_name_2, ... column_name_n
FROM table_2
[WHERE conditions_table2];

Explanation:

  • column names: We have to define the columns which we want to compare in the first and second SELECT statement. It is not required for both SELECT statements to have the same fields, only we need to make sure that they are having similar data-type.
  • table1 and table2: We have defined the name of the table from which we want to retrieve the records. It is mandatory to define at least one table in the FROM clause.
  • WHERE conditions: This is an optional clause which can be used to filter the records of the tables based on the conditions met.

How does EXCEPT Operator work in PostgreSQL?

postgreSQL EXCEPT 1

The PostgreSQL EXCEPT operator works like it will return all of the records from the light brown area. In the above diagram, we can see that records which exist in the left side area (the light brown area) and do not exist in the right side area (records2).

In order to combine the PostgreSQL SELECT statements with the help of the PostgreSQL EXCEPT operator, we need to consider the following rules:

  • Both of the SELECT statements should have the same number of columns.
  • Also, both of the SELECT statements should have the same order for columns.
  • The data types of the columns defined in the first and second SELECT statement should be the same.

Examples

Given below are the examples mentioned:

We will create two tables of name ‘student’ and ‘teacher’ by using the PostgreSQL CREATE TABLE statement.

Code:

create table student
(
stud_id serial PRIMARY KEY,
stud_fname VARCHAR(80) NOT NULL,
stud_lname VARCHAR(80) NOT NULL
);

Code:

create table teacher
(
teach_id serial PRIMARY KEY,
teach_fname VARCHAR(80) NOT NULL,
teach_lname VARCHAR(80) NOT NULL
);

Now, we will insert some data into the ‘student’ table by using the PostgreSQL INSERT INTO statement as follows:

Code:

INSERT INTO student(stud_fname, stud_lname)
VALUES
('Smith','Johnson'),
('Williams','Jones'),
('Harper','James'),
('Jack','Liam'),
('Harry','Mason'),
('Jacob','Oscar'),
('Michael','Charlie'),
('William','Joe'),
('Oliver','John');
select * from student;

Output:

postgreSQL EXCEPT 2

Now, we will insert some data into the ‘teacher’ table by using the INSERT INTO statement as follows:

Code:

INSERT INTO teacher(teach_fname, teach_lname)
VALUES
('William','Joe'),
('Oliver','John'),
('Jack','Richard'),
('Harry','Joseph'),
('George','Thomas'),
('Brown','Charles');
select * from teacher;

Output:

postgreSQL EXCEPT3

Example #1

The PostgreSQL EXCEPT operator with a single column.

To return only one column which is having the same data-type.

Code:

SELECT stud_id
FROM student
EXCEPT
SELECT teach_id
FROM teacher;

Output:

postgreSQL EXCEPT 1

In the above statement, it will return all of the stud_ids from the student table by skipping the stud_ids which are matching with the teach_id from the teacher table. Which means if stud_id is the same as teach_id, then it will not get considered in the result set.

Example #2

The PostgreSQL EXCEPT operator with multiple columns.

To return multiple columns which are having the same data-type.

Code:

SELECT stud_id, stud_fname, stud_lname
FROM student
WHERE stud_id <= 5
EXCEPT
SELECT teach_id, teach_fname, teach_lname
FROM teacher
WHERE teach_id < 3;

Output:

with Multiple columns

In the above statement, the operator will return all of the stud_id, stud_fname, and stud_lname columns from the student table by skipping the stud_id, stud_fname, and stud_lname columns, which are matching with the teach_id, teach_fname, teach_lname columns from the teacher table. Which means if stud_id, stud_fname, and stud_lname are the same as teach_id, teach_fname, and teach_lname, then it will not get considered in the result set.

Example #3

The PostgreSQL EXCEPT operator using order by.

To return multiple columns which are having the same data-type along with order by clause.

Code:

SELECT stud_id, stud_fname, stud_lname
FROM student
WHERE stud_id <= 5
EXCEPT
SELECT teach_id, teach_fname, teach_lname
FROM teacher
WHERE teach_id < 3
ORDER BY 3;

Output:

using ORDER BY

In the above example, we have used the number 3 in the order by clause which means it will order the result set by the stud_lname | teach_lname column.

Recommended Articles

We hope that this EDUCBA information on “PostgreSQL EXCEPT” was beneficial to you. You can view EDUCBA’s recommended articles for more information.

  1. PostgreSQL IF Statement
  2. FULL OUTER JOIN PostgreSQL
  3. PostgreSQL NATURAL JOIN
  4. PostgreSQL round
Primary Sidebar
Footer
Follow us!
  • EDUCBA FacebookEDUCBA TwitterEDUCBA LinkedINEDUCBA Instagram
  • EDUCBA YoutubeEDUCBA CourseraEDUCBA Udemy
APPS
EDUCBA Android AppEDUCBA iOS App
Blog
  • Blog
  • Free Tutorials
  • About us
  • Contact us
  • Log in
Courses
  • Enterprise Solutions
  • Free Courses
  • Explore Programs
  • All Courses
  • All in One Bundles
  • Sign up
Email
  • [email protected]

ISO 10004:2018 & ISO 9001:2015 Certified

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

EDUCBA

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

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

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

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more

EDUCBA
Free Data Science Course

Hadoop, Data Science, Statistics & others

By continuing above step, you agree to our Terms of Use and Privacy Policy.
*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA Login

Forgot Password?

🚀 Limited Time Offer! - 🎁 ENROLL NOW