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
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.
SELECT column1, column2, ... FROM table1 WHERE condition EXCEPT SELECT column1, column2, ... FROM table2 WHERE condition;
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.
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.
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.
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.
Using EXCEPT to find the difference between two queries in PostgreSQL:
EXCEPT
returns distinct rows from the first query that are not present in the second query.SELECT column1, column2 FROM table1 EXCEPT SELECT column1, column2 FROM table2;
Comparing result sets with EXCEPT in PostgreSQL:
EXCEPT
to compare result sets and identify differences between two queries.SELECT column1, column2 FROM table1 EXCEPT SELECT column1, column2 FROM table2;
Handling NULL values with EXCEPT in PostgreSQL:
EXCEPT
treats NULL values as equal. To handle NULLs differently, use additional filters or consider using IS NULL
in the queries.SELECT column1, column2 FROM table1 WHERE column1 IS NOT NULL EXCEPT SELECT column1, column2 FROM table2 WHERE column1 IS NOT NULL;
Combining EXCEPT with other set operators in PostgreSQL:
EXCEPT
with other set operators like UNION
and INTERSECT
to perform complex set operations.(SELECT column1, column2 FROM table1) EXCEPT (SELECT column1, column2 FROM table2 INTERSECT SELECT column1, column2 FROM table3);
Using EXCEPT with multiple tables in PostgreSQL:
EXCEPT
to find the differences between them.SELECT column1, column2 FROM table1 EXCEPT SELECT column1, column2 FROM table2 EXCEPT SELECT column1, column2 FROM table3;
Applying filters with WHERE clause in EXCEPT queries in PostgreSQL:
WHERE
clause to apply filters in the queries before using EXCEPT
to find specific differences.SELECT column1, column2 FROM table1 WHERE condition EXCEPT SELECT column1, column2 FROM table2 WHERE condition;
Alternatives to EXCEPT operator in PostgreSQL:
NOT EXISTS
, LEFT JOIN
, and NOT IN
. Choose the appropriate method based on your specific use case.SELECT column1, column2 FROM table1 WHERE NOT EXISTS (SELECT 1 FROM table2 WHERE table1.column1 = table2.column1);