INTERSECT equivalent in MySQL - Time & Space Complexity
We want to understand how the time needed to find common rows between two tables grows as the tables get bigger.
How does the cost change when we find the intersection of two sets of data?
Analyze the time complexity of this MySQL query that finds common rows using JOIN.
SELECT a.id, a.value
FROM tableA a
INNER JOIN tableB b ON a.id = b.id;
This query returns rows where the id exists in both tableA and tableB.
Look for repeated work done by the database engine.
- Primary operation: Matching each row in tableA with rows in tableB by id.
- How many times: For each row in tableA, the database checks for matching rows in tableB.
As the number of rows in the tables grows, the work to find matches grows too.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 checks |
| 100 | About 100 checks |
| 1000 | About 1000 checks |
Pattern observation: The work grows roughly in direct proportion to the number of rows in the first table.
Time Complexity: O(n)
This means the time to find common rows grows linearly with the size of the input tables, assuming proper indexing.
[X] Wrong: "The database checks every possible pair of rows between the two tables."
[OK] Correct: With proper indexes, the database looks up matches quickly instead of comparing all pairs, so it does not do a full cross-check.
Understanding how joins work and their time cost helps you explain how databases handle common tasks efficiently, a useful skill in many real projects.
"What if we replaced the INNER JOIN with a subquery using EXISTS? How would the time complexity change?"
Practice
table1 and table2 when INTERSECT is not supported?Solution
Step 1: Understand INTERSECT purpose
INTERSECT returns rows common to both queries or tables.Step 2: Use WHERE ... IN to mimic INTERSECT
MySQL lacks INTERSECT, butWHERE column IN (subquery)returns rows intable1that also appear intable2.Final Answer:
UseSELECT * FROM table1 WHERE column IN (SELECT column FROM table2)-> Option CQuick Check:
INTERSECT equivalent = WHERE ... IN [OK]
- Using UNION returns all unique rows, not common ones
- JOIN returns combined rows, not just common rows alone
- EXCEPT is not supported in MySQL and finds differences
id values between tableA and tableB in MySQL?Solution
Step 1: Recognize MySQL lacks INTERSECT and EXCEPT
MySQL does not support INTERSECT or EXCEPT keywords.Step 2: Use WHERE ... IN syntax for intersection
The correct syntax isSELECT id FROM tableA WHERE id IN (SELECT id FROM tableB);to get common ids.Final Answer:
SELECT id FROM tableA WHERE id IN (SELECT id FROM tableB); -> Option BQuick Check:
Correct syntax for INTERSECT equivalent = WHERE ... IN [OK]
- Trying to use INTERSECT or EXCEPT directly in MySQL
- Using UNION which combines all unique rows
- Confusing JOIN with intersection
table1:id
1
2
3
table2:id
2
3
4
What is the output of this query?
SELECT id FROM table1 WHERE id IN (SELECT id FROM table2);
Solution
Step 1: Identify ids in table1
table1 has ids 1, 2, 3.Step 2: Identify ids in table2
table2 has ids 2, 3, 4.Step 3: Find common ids using WHERE ... IN
The query returns ids from table1 that are also in table2: 2 and 3.Final Answer:
[2, 3] -> Option DQuick Check:
Common ids = [2, 3] [OK]
- Including ids only in one table (like 1 or 4)
- Confusing UNION with intersection
- Expecting all ids from table1
user_id values:SELECT user_id FROM users WHERE user_id IN (SELECT user_id FROM orders);
But it returns an error. What is the most likely cause?
Solution
Step 1: Check subquery column count
The subquery insideINmust return exactly one column.Step 2: Identify error cause
If the subquery returns multiple columns, MySQL throws an error.Final Answer:
The subquery returns multiple columns instead of one -> Option AQuick Check:
Subquery must return single column [OK]
- Using multiple columns in subquery for IN
- Assuming INTERSECT exists in MySQL
- Ignoring column existence in tables
product_id values that appear in both sales and returns tables. Both tables have product_id and date columns. You only want products sold and returned on the same date. Which query correctly finds this intersection?Solution
Step 1: Understand requirement for matching product_id and date
We need products sold and returned on the same date, so both columns must match.Step 2: Use tuple comparison in WHERE ... IN
MySQL supports tuple comparisons like(product_id, date) IN (SELECT product_id, date FROM returns)to find exact matches.Step 3: Evaluate other options
SELECT product_id FROM sales WHERE product_id IN (SELECT product_id FROM returns); ignores date, SELECT product_id FROM sales JOIN returns ON sales.product_id = returns.product_id AND sales.date = returns.date; can produce duplicate product_id values if multiple returns match the same product and date, SELECT product_id FROM sales INTERSECT SELECT product_id FROM returns WHERE sales.date = returns.date; uses unsupported INTERSECT with conditions.Final Answer:
SELECT product_id FROM sales WHERE (product_id, date) IN (SELECT product_id, date FROM returns); -> Option AQuick Check:
Tuple IN matches multiple columns [OK]
- Ignoring date column in intersection
- Using INTERSECT which MySQL lacks
- Using JOIN which can produce duplicate product_ids
Ready to prove it?
Pass the test and this pattern moves unknown
