Introduction
We use EXCEPT (or MINUS) to find items in one list that are not in another. It helps us see differences between two sets of data.
Jump into concepts and practice - no test required
We use EXCEPT (or MINUS) to find items in one list that are not in another. It helps us see differences between two sets of data.
SELECT column_list FROM table1 EXCEPT SELECT column_list FROM table2;
SELECT name FROM employees EXCEPT SELECT name FROM managers;
SELECT product_id FROM sales_2023 MINUS SELECT product_id FROM sales_2022;
SELECT email FROM newsletter_subscribers EXCEPT SELECT email FROM unsubscribed;
This example finds fruits that appeared in 2023 but not in 2022.
CREATE TABLE fruits2023 (name VARCHAR(20)); CREATE TABLE fruits2022 (name VARCHAR(20)); INSERT INTO fruits2023 (name) VALUES ('apple'), ('banana'), ('cherry'); INSERT INTO fruits2022 (name) VALUES ('banana'), ('date'); SELECT name FROM fruits2023 EXCEPT SELECT name FROM fruits2022;
Both queries must select the same number of columns with compatible data types.
EXCEPT removes duplicates from the result by default.
Order of queries matters: EXCEPT returns rows in the first query that are missing in the second.
EXCEPT (or MINUS) helps find differences between two sets of data.
It returns rows from the first query that do not appear in the second.
Useful for comparing lists and spotting unique items.
EXCEPT operator do?table1 not in table2 using EXCEPT?SELECT ... FROM ... EXCEPT SELECT ... FROM ....table1:table2:SELECT id FROM table1 EXCEPT SELECT id FROM table2;SELECT name FROM employees EXCEPT name FROM managers;name FROM managers, causing syntax error.orders_2023 and orders_2024, both with columns order_id and customer_id.orders_2023 EXCEPT orders_2024 fits.