PostgreSQL Tutorial

Data Types

Querying & Filtering Data

Managing Tables

Modifying Data

Conditionals

Control Flow

Transactions & Constraints

Working with JOINS & Schemas

Roles & Permissions

Working with Sets

Subquery & CTEs

User-defined Functions

Important In-Built Functions

PostgreSQL PL/pgSQL

Variables & Constants

Stored Procedures

Working with Triggers

Working with Views & Indexes

Errors & Exception Handling

PostgreSQL - EXCEPT Operator

The EXCEPT operator in PostgreSQL is used to return all the rows in the first result set that are not present in the second result set. Both SQL statements within the EXCEPT must retrieve the same number of columns, and corresponding columns must have compatible data types.

The EXCEPT operator works similarly to the UNION or INTERSECT operators, but it returns only the rows that are unique to the first result set.

Syntax:

SELECT column1, column2, ...
FROM table1
WHERE condition
EXCEPT
SELECT column1, column2, ...
FROM table2
WHERE condition;

Example:

Consider the following data:

Table: students

| id | name  |
|----|-------|
| 1  | Alice |
| 2  | Bob   |
| 3  | Carol |

Table: graduated_students

| id | name  |
|----|-------|
| 2  | Bob   |
| 3  | Carol |

To find out the students who haven't graduated yet:

SELECT id, name
FROM students
EXCEPT
SELECT id, name
FROM graduated_students;

This would return:

| id | name  |
|----|-------|
| 1  | Alice |

Alice is the only student from the students table who hasn't graduated yet.

Points to Remember:

  1. The EXCEPT operator removes duplicates. If there are identical rows in the first result set, the EXCEPT operation will return them as a single row.

  2. Column order matters. If the first result set has columns A and B, and the second result set has columns B and A, you'll get an error.

  3. To maintain the original order of rows from the first result set after the EXCEPT operation, you can use the ORDER BY clause at the end.

In summary, the EXCEPT operator is useful when you want to retrieve rows from one query that don't exist in another. Always ensure that the structures of the compared result sets match in terms of the number of columns and their data types.

  1. Using EXCEPT to find the difference between two queries in PostgreSQL:

    • Description: EXCEPT returns distinct rows from the first query that are not present in the second query.
    • Code:
      SELECT column1, column2 FROM table1
      EXCEPT
      SELECT column1, column2 FROM table2;
      
  2. Comparing result sets with EXCEPT in PostgreSQL:

    • Description: Use EXCEPT to compare result sets and identify differences between two queries.
    • Code:
      SELECT column1, column2 FROM table1
      EXCEPT
      SELECT column1, column2 FROM table2;
      
  3. Handling NULL values with EXCEPT in PostgreSQL:

    • Description: EXCEPT treats NULL values as equal. To handle NULLs differently, use additional filters or consider using IS NULL in the queries.
    • Code:
      SELECT column1, column2 FROM table1 WHERE column1 IS NOT NULL
      EXCEPT
      SELECT column1, column2 FROM table2 WHERE column1 IS NOT NULL;
      
  4. Combining EXCEPT with other set operators in PostgreSQL:

    • Description: Combine EXCEPT with other set operators like UNION and INTERSECT to perform complex set operations.
    • Code:
      (SELECT column1, column2 FROM table1)
      EXCEPT
      (SELECT column1, column2 FROM table2
       INTERSECT
       SELECT column1, column2 FROM table3);
      
  5. Using EXCEPT with multiple tables in PostgreSQL:

    • Description: Compare the results of multiple queries using EXCEPT to find the differences between them.
    • Code:
      SELECT column1, column2 FROM table1
      EXCEPT
      SELECT column1, column2 FROM table2
      EXCEPT
      SELECT column1, column2 FROM table3;
      
  6. Applying filters with WHERE clause in EXCEPT queries in PostgreSQL:

    • Description: Use the WHERE clause to apply filters in the queries before using EXCEPT to find specific differences.
    • Code:
      SELECT column1, column2 FROM table1 WHERE condition
      EXCEPT
      SELECT column1, column2 FROM table2 WHERE condition;
      
  7. Alternatives to EXCEPT operator in PostgreSQL:

    • Description: Alternatives include NOT EXISTS, LEFT JOIN, and NOT IN. Choose the appropriate method based on your specific use case.
    • Code (using NOT EXISTS):
      SELECT column1, column2 FROM table1
      WHERE NOT EXISTS (SELECT 1 FROM table2 WHERE table1.column1 = table2.column1);