Preamble
The PostgreSQL EXCEPT statement is used to return all lines in the first SELECT statement that are not returned by the second SELECT statement.
Each SELECT operator will define the data set.
EXCEPT will extract all records from the first dataset and then delete all records from the second dataset from the results.
EXCEPT query

Explanation: The “EXCEPT” query will return the records in the shaded blue area. These are records that exist in the SELECT 1 data set, not in the SELECT 2 data set.
Each SELECT operator in an “EXCEPT” query must have the same number of fields in the result sets with the same data types.
The syntax for EXCEPT in PostgreSQL
SELECT expression1_id, expression2_id, ... expression_n_id
FROM tables
[WHERE conditions]
EXCEPT
SELECT expression1_id, expression2_id, ... expression_n_id
FROM tabs
[WHERE conds];
Parameters and arguments of the statement
- expressions – Columns or calculations that you want to compare between two SELECT operators. They do not necessarily have to be the same fields in each SELECT statement, but the corresponding columns must be of the same data type.
- Tables – The tables from which you want to get the records. At least one table must be specified in the FROM operator.
- WHERE conds – Optional. The conditions to be met for the records to be selected.
Note:
- Both SELECT operators must have the same number of expressions.
- The corresponding columns in each of the SELECT operators must have similar data types.
- The EXCEPT operator shall return all records from the first SELECT operator that is not in the second SELECT operator.
- The EXCEPT operator in PostgreSQL is equivalent to the MINUS operator in Oracle.
Example of an EXCEPT operator with one expression
Let’s consider an example of an EXCEPT operator in PostgreSQL, which returns a single field with the same data type.
For example:
SELECT category_id
FROM products
EXCEPT
SELECT category_id
FROM inventory;
In this EXCEPT example, all category_id values that are in the products table are returned instead of the inventory table. This means that if the category_id value existed in the products table as well as the inventory table, the category_id value will not be displayed in the EXCEPT query results.
Example of an operator with multiple expressions
Next, let’s look at an example of an EXCEPT query in PostgreSQL that returns more than one column.
For example:
SELECT contact_id,
last_name,
first_name
FROM contacts
WHERE last_name = 'Ivanov'
EXCEPT
SELECT customer_id,
last_name,
first_name
FROM customers
WHERE customer_id < 99;
In this example, the EXCEPT query will return records from the contacts table with contact_id, last_name, and first_name a value that does not match the customer_id, last_name, and first_name in the customer’s table.
Example of an operator using ORDER BY
How to use the ORDER BY statement in an EXCEPT query in PostgreSQL
For example:
SELECT supplier_id,
supplier_name
FROM suppliers
WHERE supplier_id >= 59
EXCEPT
SELECT company_id,
company_name
FROM companies
WHERE state = 'Nevada'
ORDER BY 2;
In this EXCEPT example, since column names in two SELECT operators are different, it is more advantageous to refer to columns in ORDER BY operator by their position in the resulting set. In this example, we have sorted the results by supplier_name / company_name in ascending order as ORDER BY 2.
The fields supplier_name / company_name are at position #2 in the resulting set.
SQL UNION, INTERSECT, EXCEPT
About Enteros
Enteros offers a patented database performance management SaaS platform. It proactively identifies root causes of complex business-impacting database scalability and performance issues across a growing number of clouds, RDBMS, NoSQL, and machine learning database platforms.
The views expressed on this blog are those of the author and do not necessarily reflect the opinions of Enteros Inc. This blog may contain links to the content of third-party sites. By providing such links, Enteros Inc. does not adopt, guarantee, approve, or endorse the information, views, or products available on such sites.
Are you interested in writing for Enteros’ Blog? Please send us a pitch!
RELATED POSTS
Driving Financial RevOps Efficiency with Enteros: AIOps-Powered Database Performance for Smarter Decision-Making
- 27 October 2025
- Database Performance Management
Introduction In today’s competitive financial ecosystem, where every second and every data point matters, achieving operational agility and revenue precision has become a top priority. Financial institutions — from banks and insurance companies to fintech innovators — are seeking more intelligent ways to manage their data-driven operations. The convergence of Revenue Operations (RevOps), database performance … Continue reading “Driving Financial RevOps Efficiency with Enteros: AIOps-Powered Database Performance for Smarter Decision-Making”
Revolutionizing Healthcare Efficiency with Enteros: SaaS Database Optimization, Generative AI, and Cloud FinOps Synergy
Introduction The healthcare sector has entered a new digital era driven by innovation, patient data analytics, and intelligent automation. From hospital management systems and patient monitoring to clinical research and predictive diagnostics, the industry’s growing dependence on cloud-based data systems has introduced both immense opportunities and operational challenges. As healthcare organizations scale their operations, managing … Continue reading “Revolutionizing Healthcare Efficiency with Enteros: SaaS Database Optimization, Generative AI, and Cloud FinOps Synergy”
⚡ The Hidden Blackout: How Data Lag Triggered a Grid Failure (and What It Taught Us About Energy Resilience)
📍 The Incident In late 2024, a regional utility company in Northern Europe experienced an unexpected power disruption.Not because of a storm.Not because of an overload.But because two monitoring systems were five seconds out of sync. Those five seconds cost the company 19 million euros in operational losses and emergency response. When analysts reconstructed the … Continue reading “⚡ The Hidden Blackout: How Data Lag Triggered a Grid Failure (and What It Taught Us About Energy Resilience)”
How Enteros is Revolutionizing Real Estate RevOps Through Generative AI and Smarter Database Performance
- 26 October 2025
- Database Performance Management
Introduction The real estate sector is no longer confined to property listings and offline transactions—it’s a complex, data-driven industry powered by technology, analytics, and automation. From property valuation and customer engagement to financial forecasting and deal execution, data plays a pivotal role in driving operational efficiency and revenue growth. However, managing this massive influx of … Continue reading “How Enteros is Revolutionizing Real Estate RevOps Through Generative AI and Smarter Database Performance”