EXCEPT equivalent in MySQL - Time & Space Complexity
We want to understand how the time needed to find differences between two sets of data grows as the data gets bigger.
Specifically, how does the cost change when using an EXCEPT equivalent in MySQL?
Analyze the time complexity of the following code snippet.
SELECT column1 FROM tableA
WHERE column1 NOT IN (SELECT column1 FROM tableB);
This query finds all values in tableA that are not present in tableB, similar to EXCEPT.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: For each row in
tableA, check if its value exists intableB. - How many times: This check happens once for every row in
tableA.
As the number of rows in tableA grows, the number of checks grows too.
| Input Size (rows in tableA) | Approx. Operations |
|---|---|
| 10 | 10 checks against tableB |
| 100 | 100 checks against tableB |
| 1000 | 1000 checks against tableB |
Pattern observation: The number of operations grows directly with the size of tableA.
Time Complexity: O(n * m)
This means the time grows roughly by multiplying the size of tableA (n) by the size of tableB (m), because each row in tableA is checked against all rows in tableB.
[X] Wrong: "The query only looks at tableA, so time depends only on its size."
[OK] Correct: Each row in tableA must be compared against tableB, so the size of tableB also affects the time.
Understanding how queries compare data sets helps you explain performance and choose better solutions in real projects.
"What if we add an index on tableB.column1? How would the time complexity change?"
Practice
EXCEPT operation do?Solution
Step 1: Understand the purpose of EXCEPT
The EXCEPT operation returns rows that appear in the first query but do not appear in the second query.Step 2: Compare with other set operations
Unlike INTERSECT which returns common rows, EXCEPT excludes rows found in the second set.Final Answer:
Returns rows from the first query that are not in the second query -> Option AQuick Check:
EXCEPT = unique rows in first set [OK]
- Confusing EXCEPT with INTERSECT
- Thinking EXCEPT returns all rows combined
- Assuming EXCEPT deletes data
EXCEPT using NOT EXISTS?Solution
Step 1: Identify how NOT EXISTS works
NOT EXISTS checks if a row in table1 has no matching row in table2 based on the condition.Step 2: Match syntax to EXCEPT behavior
The query selects rows from table1 where no matching id exists in table2, simulating EXCEPT.Final Answer:
SELECT * FROM table1 WHERE NOT EXISTS (SELECT * FROM table2 WHERE table2.id = table1.id); -> Option AQuick Check:
NOT EXISTS filters out matching rows [OK]
- Using EXCEPT directly in MySQL (unsupported)
- Confusing JOIN with NOT EXISTS logic
- Using IN instead of NOT EXISTS
table1(id): 1, 2, 3, 4table2(id): 3, 4, 5What is the result of this query?
SELECT id FROM table1 WHERE NOT EXISTS (SELECT 1 FROM table2 WHERE table2.id = table1.id);
Solution
Step 1: Identify ids in table1 not in table2
table1 has 1,2,3,4; table2 has 3,4,5. IDs 1 and 2 are missing in table2.Step 2: Understand NOT EXISTS filtering
The query returns ids from table1 where no matching id exists in table2, so 1 and 2 are returned.Final Answer:
[1, 2] -> Option BQuick Check:
table1 minus table2 = [1, 2] [OK]
- Including ids present in both tables
- Confusing NOT EXISTS with EXISTS
- Returning ids from table2 instead
SELECT * FROM table1 LEFT JOIN table2 ON table1.id = table2.id WHERE table2.id = NULL;
Solution
Step 1: Understand NULL comparison in SQL
In SQL, NULL cannot be compared using '='; use IS NULL instead.Step 2: Identify the mistake in WHERE clause
The conditiontable2.id = NULLis always false; it should betable2.id IS NULLto find unmatched rows.Final Answer:
Using '=' to compare with NULL instead of IS NULL -> Option CQuick Check:
NULL comparison requires IS NULL [OK]
- Using '=' instead of IS NULL
- Confusing JOIN types
- Omitting ON clause in JOIN
EXCEPT in MySQL?Solution
Step 1: Identify the time frames and logic
We want customers who ordered in May but not in June.Step 2: Use NOT EXISTS to exclude June customers
The query selects May customers and excludes those with June orders using NOT EXISTS subquery.Step 3: Check other options
SELECT customer_id FROM orders WHERE order_date BETWEEN '2024-06-01' AND '2024-06-30' EXCEPT SELECT customer_id FROM orders WHERE order_date BETWEEN '2024-05-01' AND '2024-05-31'; uses EXCEPT which MySQL does not support; C returns all customers in both months; D returns June customers only.Final Answer:
SELECT customer_id FROM orders WHERE order_date BETWEEN '2024-05-01' AND '2024-05-31' AND NOT EXISTS (SELECT 1 FROM orders o2 WHERE o2.customer_id = orders.customer_id AND o2.order_date BETWEEN '2024-06-01' AND '2024-06-30'); -> Option DQuick Check:
NOT EXISTS excludes June orders [OK]
- Using EXCEPT directly in MySQL
- Selecting all orders without filtering
- Confusing LEFT JOIN logic
Ready to prove it?
Pass the test and this pattern moves unknown
