What if you could spot exactly what changed between two lists in just one simple command?
Why EXCEPT (MINUS) for differences in SQL? - Purpose & Use Cases
Start learning this pattern below
Jump into concepts and practice - no test required
Imagine you have two lists of names on paper, one from last year and one from this year. You want to find out who left the group. You try to compare them by scanning each name one by one.
Checking each name manually is slow and easy to mess up. You might miss some names or check the same name twice. It's hard to keep track and takes a lot of time.
The EXCEPT (or MINUS) command in SQL quickly finds the difference between two lists. It shows you exactly which items are in one list but not the other, without any extra effort.
for name in last_year_list: if name not in this_year_list: print(name)
SELECT name FROM last_year EXCEPT SELECT name FROM this_year;
This lets you instantly see what changed between two sets of data, saving time and avoiding mistakes.
A company wants to find customers who bought products last year but not this year to send them special offers. Using EXCEPT, they get the list instantly.
Manually comparing lists is slow and error-prone.
EXCEPT (MINUS) finds differences quickly and accurately.
It helps track changes between two sets of data easily.
Practice
EXCEPT operator do?Solution
Step 1: Understand the purpose of EXCEPT
The EXCEPT operator compares two query results and returns only those rows that appear in the first query but not in the second.Step 2: Differentiate from other set operations
Unlike INTERSECT which returns common rows, EXCEPT returns unique rows from the first set only.Final Answer:
Returns rows from the first query that are not in the second query -> Option AQuick Check:
EXCEPT = difference = unique first query rows [OK]
- Confusing EXCEPT with INTERSECT
- Thinking EXCEPT deletes rows
- Assuming EXCEPT returns all combined rows
table1 not in table2 using EXCEPT?Solution
Step 1: Recall correct EXCEPT syntax
The EXCEPT operator is used between two SELECT statements:SELECT ... FROM ... EXCEPT SELECT ... FROM ....Step 2: Check each option
SELECT * FROM table1 EXCEPT SELECT * FROM table2; uses correct syntax with two SELECT statements separated by EXCEPT. SELECT * FROM table1 MINUS table2; is incorrect because MINUS requires SELECT before both tables. SELECT * FROM table1 WHERE NOT IN table2; is invalid syntax. SELECT * FROM table1 JOIN table2 EXCEPT; misuses JOIN and EXCEPT.Final Answer:
SELECT * FROM table1 EXCEPT SELECT * FROM table2; -> Option CQuick Check:
Correct EXCEPT syntax = SELECT ... EXCEPT SELECT ... [OK]
- Using EXCEPT without two SELECT statements
- Confusing MINUS syntax with EXCEPT
- Trying to use EXCEPT with JOIN
table1:id
1
2
3
table2:id
2
4
5
What is the result of:
SELECT id FROM table1 EXCEPT SELECT id FROM table2;Solution
Step 1: Identify rows in table1
table1 has ids 1, 2, 3.Step 2: Identify rows in table2
table2 has ids 2, 4, 5.Step 3: Apply EXCEPT logic
EXCEPT returns rows in table1 not in table2, so ids 1 and 3 remain.Final Answer:
[1, 3] -> Option AQuick Check:
table1 - table2 = [1, 3] [OK]
- Including ids from table2
- Returning common ids instead
- Confusing EXCEPT with UNION
SELECT name FROM employees EXCEPT name FROM managers;Solution
Step 1: Check syntax of EXCEPT usage
EXCEPT requires two full SELECT statements. The second query must start with SELECT.Step 2: Identify error in given query
The second part is missing SELECT beforename FROM managers, causing syntax error.Final Answer:
Missing SELECT keyword before second query -> Option BQuick Check:
EXCEPT needs two SELECTs [OK]
- Omitting SELECT in second query
- Using EXCEPT with columns only
- Confusing EXCEPT with JOIN syntax
orders_2023 and orders_2024, both with columns order_id and customer_id.You want to find all orders from 2023 that were NOT repeated in 2024.
Which query correctly finds these unique 2023 orders?
Solution
Step 1: Understand the requirement
We want orders in 2023 that do NOT appear in 2024.Step 2: Choose correct set operation
EXCEPT returns rows in first query not in second, soorders_2023 EXCEPT orders_2024fits.Step 3: Check other options
SELECT order_id, customer_id FROM orders_2024 EXCEPT SELECT order_id, customer_id FROM orders_2023; reverses order, giving 2024 unique orders. SELECT order_id, customer_id FROM orders_2023 INTERSECT SELECT order_id, customer_id FROM orders_2024; returns common orders. SELECT order_id, customer_id FROM orders_2023 UNION SELECT order_id, customer_id FROM orders_2024; combines all orders.Final Answer:
SELECT order_id, customer_id FROM orders_2023 EXCEPT SELECT order_id, customer_id FROM orders_2024; -> Option DQuick Check:
2023 orders minus 2024 orders = unique 2023 orders [OK]
- Swapping table order in EXCEPT
- Using INTERSECT instead of EXCEPT
- Using UNION which combines all rows
