EXCEPT (MINUS) for differences in SQL - Time & Space Complexity
Start learning this pattern below
Jump into concepts and practice - no test required
When we use EXCEPT or MINUS in SQL, we want to find rows in one table that are not in another. Understanding how long this takes helps us know how it will perform as data grows.
We ask: How does the time to find differences change when tables get bigger?
Analyze the time complexity of the following code snippet.
SELECT employee_id, name
FROM employees
EXCEPT
SELECT employee_id, name
FROM retired_employees;
This query finds employees who are currently working but not retired by removing retired employees from the full employee list.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Comparing each row in the first table against rows in the second to find differences.
- How many times: For each row in the first table, it checks against rows in the second table.
As the number of employees and retired employees grows, the comparisons increase.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 100 comparisons |
| 100 | About 10,000 comparisons |
| 1000 | About 1,000,000 comparisons |
Pattern observation: The number of operations grows quickly, roughly multiplying as both tables get bigger.
Time Complexity: O(n * m)
This means the time to find differences grows roughly by multiplying the size of the first table by the size of the second.
[X] Wrong: "The EXCEPT operation only looks at one table, so it runs in linear time."
[OK] Correct: EXCEPT compares rows between two tables, so it depends on both sizes, not just one.
Understanding how set difference operations scale helps you explain query performance clearly and shows you can think about data size impact in real projects.
"What if we added an index on the columns used in EXCEPT? How would the time complexity change?"
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
