Page Content

Tutorials

What Are The Except Operator in PostgreSQL With Example

Except Operator in PostgreSQL

PostgreSQL’s EXCEPT operator blends two SELECT queries by returning just the distinct rows from the first query but not the second. Its main function is calculating the “difference” between query results. EXCEPT works when both SELECT queries yield the same amount of columns and have compatible data types. EXCEPT removes duplicate records from its output by default, like SELECT DISTINCT. If ALL is provided (i.e., EXCEPT ALL), duplicate entries from the first query that are not detected in the second query will be preserved.

The EXCEPT operator is evaluated after WHERE clause filtering, GROUP BY and HAVING clauses, and DISTINCT (if supplied) to eliminate duplicate rows. PostgreSQL considers chained set operators left to right unless brackets specify otherwise. The SQL standard defines EXCEPT, while Oracle uses MINUS, and PostgreSQL’s ALL term for EXCEPT is an extension. It can replace NOT IN or NOT EXISTS constructs, which might slow PostgreSQL’s indexes, for tasks like discovering table differences.

Except Operator in PostgreSQL
Except Operator in PostgreSQL

Core Functionality and Syntax

Two SELECT statements make up the fundamental syntax for employing the EXCEPT operator:

query1 EXCEPT [ALL] query2 [5, 6]

Where the two queries are legitimate SELECT statements.

Both of the SELECT queries need to be “union compatible” in order for EXCEPT to work. Accordingly, they need to meet two requirements:

  • The quantity of columns they return must be equal.
  • Complementary data types must be present in their respective columns. It is always the first subquery that provides the output names for the columns in the final result.

Handling of Duplicates

The EXCEPT operator automatically eliminates duplicate data from its output. SELECT DISTINCT functions similarly to this behaviour. Query 1 rows that occur twice but not in query 2 will only display once in the EXCEPT result. A row that occurs in both queries1 and 2 will be removed.

The ALL keyword preserves duplicate rows: EXCEPT ALL. If ALL is given, EXCEPT will return all of the rows from query 1 that do not exactly match in query 2, preserving the original query 1 count. If query 2 contains one matched row and query 1 contains three identical rows, then EXCEPT ALL would return two of query 1’s identical rows.

Placement and Order of Evaluation

The form of the SELECT statement depends heavily on the EXCEPT clause. In the PostgreSQL query’s logical processing sequence, set operations such as EXCEPT are used at the end of the process, following:

  • Input tables provide the rows.
  • There is filtering via the WHERE clause.
  • Combining and combining (if there are GROUP BY and HAVING clauses).
  • Duplicate rows are removed if the select list contains the DISTINCT option.

Because PostgreSQL optimises this process, the logical output is the same regardless of execution sequence. Union, intersect, and except are evaluated left to right in SELECT queries unless otherwise stated in parenthesis. ORDER BY and LIMIT can only sort EXCEPT combo output by column names or numbers.

Use Cases and Examples

The following tasks benefit greatly from the use of the EXCEPT operator:

Finding differences between tables: Chaining EXCEPT ALL with UNION ALL finds entries in one table but not another, or vice versa. To find rows specific to table an or table b. In table a, this query would display rows that are present but not in table b, and vice versa.

Simulating outer joins (historical/specific scenarios): For historical or particular cases, simulating outer joins. EXCEPT (typically in conjunction with UNION ALL) can be used to mimic the behaviour of outer joins in previous PostgreSQL versions (like as 7.0) that did not natively allow outer joins or for particular reasoning. This example would include every row from tab1, displaying NULL in the absence of a match and matching tab2.col2 values when they exist.

Identifying records that don’t meet a certain condition via a subquery: In place of NOT IN or NOT EXISTS clauses, this might be used. To locate seats on a flight without matching boarding passes, for instance, one method use NOT EXISTS; however, EXCEPT (as a “set subtraction” operation) may also be used. The NOT IN construct can occasionally result in performance problems since PostgreSQL might not be able to evaluate such queries effectively using indexes. Using EXCEPT or LEFT JOIN / NOT EXISTS to rewrite such queries might occasionally result in better performance.

Code Example:

CREATE TABLE seats(seat_no INT);
CREATE TABLE boarding_passes(seat_no INT);
INSERT INTO seats VALUES (1), (2), (3), (4);
INSERT INTO boarding_passes VALUES (2), (4);
SELECT seat_no FROM seats
EXCEPT
SELECT seat_no FROM boarding_passes;

Output:

CREATE TABLE
CREATE TABLE
INSERT 0 4
INSERT 0 2
 seat_no 
---------
       3
       1
(2 rows)

PostgreSQL Specifics and Compatibility

Although it includes certain features and additions, PostgreSQL’s implementation of the EXCEPT operator mostly follows the SQL standard. Other relational database management systems, like Oracle, may use other terminology, such as MINUS instead of EXCEPT, even though EXCEPT is a common set operation. The ALL keyword is a noteworthy addition to the standard in PostgreSQL. When declaring EXCEPT ALL, users can keep duplicate entries from the first query that are not present in the second query, while the default EXCEPT behaviour automatically removes duplicates.

Both SELECT queries must be “union compatible” that is, they must produce an equal number of columns with matching compatible data types in order for EXCEPT to work properly. The first subquery is always the output column names. EXCEPT is logically used late in the execution flow of a query, following the removal of DISTINCT rows, WHERE clauses, and GROUP BY and HAVING clauses. Unless brackets specify a different sequence, multiple set operators are evaluated from left to right.

Moreover, the outcome of an EXCEPT operation may be subject to ORDER BY and LIMIT clauses. Key features of PostgreSQL’s architecture include its extensibility and flexibility, which enable the creation of custom data types, functions, and operators. In some situations, the EXCEPT operator can be a more effective option than NOT IN or NOT EXISTS clauses, which occasionally have trouble using their indexes efficiently.

EXCEPT’s handling of conditional clauses and set operations (UNION, INTERSECT) is crucial for CQ builders to comprehend. Develop the same query as many times as possible (e.g., using EXCEPT instead of NOT IN or LEFT JOIN) and compare execution plans (using EXPLAIN) to ascertain the ideal result and performance consequences.

Kowsalya
Kowsalya
Hi, I'm Kowsalya a B.Com graduate and currently working as an Author at Govindhtech Solutions. I'm deeply passionate about publishing the latest tech news and tutorials that bringing insightful updates to readers. I enjoy creating step-by-step guides and making complex topics easier to understand for everyone.
Index