Union, intersection, difference in DBMS Theory - Time & Space Complexity
When working with sets of data in databases, operations like union, intersection, and difference combine or compare rows from tables.
We want to understand how the time to perform these operations grows as the size of the data increases.
Analyze the time complexity of the following SQL queries using UNION, INTERSECT, and EXCEPT.
SELECT * FROM TableA
UNION
SELECT * FROM TableB;
SELECT * FROM TableA
INTERSECT
SELECT * FROM TableB;
SELECT * FROM TableA
EXCEPT
SELECT * FROM TableB;
These queries combine or compare two tables to produce a result set with unique rows.
Each operation involves scanning rows and comparing them.
- Primary operation: Comparing rows from both tables to find matches or differences.
- How many times: Each row in one table is compared against rows in the other table.
As the number of rows in each table grows, the number of comparisons grows roughly with the product of their sizes.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 100 comparisons |
| 100 | About 10,000 comparisons |
| 1000 | About 1,000,000 comparisons |
Pattern observation: Doubling the size of both tables roughly quadruples the work needed.
Time Complexity: O(n * m)
This means the time grows roughly with the number of rows in the first table times the number in the second table.
[X] Wrong: "These operations only scan one table, so time grows linearly with one table's size."
[OK] Correct: The operations compare rows between two tables, so both sizes affect the total work.
Understanding how set operations scale helps you reason about database query performance and write efficient queries in real projects.
"What if one table is much smaller than the other? How would that affect the time complexity?"
Practice
union operation in databases?Solution
Step 1: Understand the union operation
The union operation combines all unique records from two sets or tables, removing duplicates.Step 2: Compare with other operations
Intersection finds common records, difference finds records in one set but not the other, and deletion removes records.Final Answer:
Combines all unique records from two tables -> Option BQuick Check:
Union = Combine unique records [OK]
- Confusing union with intersection
- Thinking union includes duplicates
- Mixing difference with union
Solution
Step 1: Identify the keyword for common records
The SQL keywordINTERSECTreturns only the records common to both tables.Step 2: Differentiate from other keywords
UNIONcombines unique records,DIFFERENCEis not a standard SQL keyword, andJOINcombines rows based on a condition.Final Answer:
INTERSECT -> Option DQuick Check:
Common records = INTERSECT [OK]
- Using UNION instead of INTERSECT
- Confusing JOIN with INTERSECT
- Assuming DIFFERENCE is a valid SQL keyword
A INTERSECT B?Solution
Step 1: Identify common elements in A and B
The common elements between A and B are 3 and 4.Step 2: Understand INTERSECT operation
INTERSECT returns only the common elements, so the result is {3, 4}.Final Answer:
{3, 4} -> Option AQuick Check:
Intersection = common elements [OK]
- Listing all elements instead of common ones
- Confusing intersection with union
- Including elements only in one set
SELECT * FROM Table1
EXCEPT
SELECT * FROM Table2;What is the purpose of the
EXCEPT keyword?Solution
Step 1: Understand the EXCEPT keyword
EXCEPT returns records from the first query that are not present in the second query.Step 2: Apply to given tables
So, it returns records in Table1 but not in Table2.Final Answer:
Returns records in Table1 but not in Table2 -> Option AQuick Check:
Difference = records in first not in second [OK]
- Confusing EXCEPT with UNION
- Thinking EXCEPT returns common records
- Mixing order of tables in EXCEPT
Employees_A with IDs {101, 102, 103, 104} and Employees_B with IDs {103, 104, 105, 106}. You want to find IDs that are in either table but not in both. Which SQL operation gives this result?
Solution
Step 1: Understand the requirement
We want IDs in either table but not in both, which is called symmetric difference.Step 2: Identify SQL approach
SQL does not have a direct symmetric difference keyword, but it can be done by combining UNION and EXCEPT operations.Step 3: Explain why other options are incorrect
UNION returns all unique IDs including common ones, INTERSECT returns only common IDs, FULL OUTER JOIN with WHERE can be complex but not direct symmetric difference.Final Answer:
SYMMETRIC DIFFERENCE using UNION and EXCEPT -> Option CQuick Check:
Symmetric difference = union minus intersection [OK]
- Using UNION alone includes common IDs
- Using INTERSECT returns only common IDs
- Assuming FULL OUTER JOIN alone solves it
Ready to prove it?
Pass the test and this pattern moves unknown
