PostgreSQL Tutorial

  • Home
  • Administration
  • Views
  • Triggers
  • Stored Procedures
  • Interfaces
    • PostgreSQL PHP
    • PostgreSQL Python
    • PostgreSQL JDBC
Home / PostgreSQL Tutorial / PostgreSQL EXCEPT Operator

PostgreSQL EXCEPT Operator

Summary: in this tutorial, you will learn how to use the PostgresQL EXCEPT operator to return the rows in the first query that do not appear in the output of the second query.

Introduction to the PostgreSQL EXCEPT operator

Like the UNION and INTERSECT operators, the EXCEPT operator returns rows by comparing the result sets of two or more quires.

The EXCEPT operator returns distinct rows from the first (left) query that are not in the output of the second (right) query. The following illustrates the syntax of the EXCEPT operator.

1
2
3
4
5
6
7
SELECT column_list
FROM A
WHERE condition_a
EXCEPT
SELECT column_list
FROM B
WHERE condition_b;

To combine the queries using the EXCEPT operator, you must obey the following rules:

  • The number of columns and their orders must be the same in the two queries.
  • The data types of the respective columns must be compatible.

The following Venn diagram illustrates the result of the of EXCEPT operator that apply to the A and B tables.

PostgreSQL EXCEPT

PostgreSQL EXCEPT example

Let’s take a look at the film and inventory tables of the sample database.

The following query returns the films in the film table.

1
2
3
4
5
6
7
SELECT
film_id,
title
FROM
film
ORDER BY
title;

PostgreSQL Except - Films from Film Table

The following query returns the films that are in the inventory:

1
2
3
4
5
6
7
SELECT
distinct inventory.film_id,
title
FROM
inventory
INNER JOIN film ON film.film_id = inventory.film_id
ORDER BY title;

PostgreSQL Except - Films from InventoryTable

Both queries return a result set that consists of two columns: film_id and title.

To get the films that are not in the inventory, you use the EXCEPT operator as follows:

1
2
3
4
5
6
7
8
9
10
11
12
13
SELECT
film_id,
title
FROM
film
EXCEPT
SELECT
DISTINCT inventory.film_id,
title
FROM
inventory
INNER JOIN film ON film.film_id = inventory.film_id
ORDER BY title;

PostgreSQL Except Example

Notice that we placed the ORDER BY clause at the end of the statement to sort the films by their titles. If you place the ORDER BY clause in each query, the final result may not be sorted because each query will sort the result set by the title column and after that the EXCEPT operator is applied to the both queries.

In this tutorial, we have shown you how to use the PostgreSQL EXCEPT operator to combine two queries to get the rows in the first query that do not appear in the output of the second query.

Related Tutorials

  • PostgreSQL UNION
  • PostgreSQL INTERSECT Operator
Previous Tutorial: PostgreSQL INTERSECT Operator
Next Tutorial: PostgreSQL Subquery

PostgreSQL Quick Start

  • What is PostgreSQL?
  • Install PostgreSQL
  • Connect to Database
  • Download PostgreSQL Sample Database
  • Load Sample Database
  • Explore Server and Database Objects

PostgreSQL Fundamentals

  • PostgreSQL Select
  • PostgreSQL Order By
  • PostgreSQL Select Distinct
  • PostgreSQL Where
  • PostgreSQL LIMIT
  • PostgreSQL IN
  • PostgreSQL Between
  • PostgreSQL Like
  • PostgreSQL Inner Join
  • PostgreSQL Left Join
  • PostgreSQL Full Outer Join
  • PostgreSQL Cross Join
  • PostgreSQL Natural Join
  • PostgreSQL Group By
  • PostgreSQL Having
  • PostgreSQL Union
  • PostgreSQL Intersect
  • PostgreSQL Except
  • PostgreSQL Subquery
  • PostgreSQL Insert
  • PostgreSQL Update
  • PostgreSQL Delete
  • PostgreSQL Data Types
  • PostgreSQL Create Table
  • PostgreSQL Alter Table
  • PostgreSQL Drop Table
  • PostgreSQL Truncate Table
  • PostgreSQL CHECK Constraint
  • PostgreSQL Not-Null Constraint
  • PostgreSQL Foreign Key
  • PostgreSQL Primary Key
  • PostgreSQL UNIQUE Constraint

About PostgreSQL Tutorial

PostgreSQLTutorial.com is a website dedicated to developers and database administrators who are working on PostgreSQL database management system.

We constantly publish useful PostgreSQL tutorials to keep you up-to-date with the latest PostgreSQL features and technologies. All PostgreSQL tutorials are simple, easy-to-follow and practical.

Recent PostgreSQL Tutorials

  • PostgreSQL Recursive View
  • Learn PostgreSQL Recursive Query By Example
  • Creating Updatable Views Using the WITH CHECK OPTION Clause
  • PostgreSQL Upsert Using INSERT ON CONFLICT statement
  • How to Generate a Random Number in A Range
  • Using PostgreSQL ADD COLUMN to Add One or More Columns To a Table
  • PostgreSQL Character Types: CHAR, VARCHAR, and TEXT
  • Using PostgreSQL SERIAL To Create Auto-increment Column
  • PostgreSQL Boolean Data Type with Practical Examples
  • Understanding PostgreSQL Timestamp Data Types

More Tutorials

  • PostgreSQL PHP
  • PostgreSQL Python
  • PostgreSQL JDBC
  • PostgreSQL Functions
  • PostgreSQL Resources

Site Info

  • Home
  • About Us
  • Contact Us
  • Privacy Policy

Copyright © 2017 by PostgreSQL Tutorial Website. All Rights Reserved.