Why set operations are needed in SQL - Performance Analysis
Start learning this pattern below
Jump into concepts and practice - no test required
We want to understand how the time to run set operations in SQL changes as the data grows.
How does combining or comparing tables affect the work the database does?
Analyze the time complexity of this SQL set operation example.
SELECT employee_id FROM employees
UNION
SELECT employee_id FROM contractors;
This query combines employee IDs from two tables, removing duplicates.
Look for repeated work in the query.
- Primary operation: Scanning each table's rows to collect IDs.
- How many times: Once per table, then comparing combined results to remove duplicates.
As the number of rows in each table grows, the work to scan and combine grows too.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 20 scans and comparisons |
| 100 | About 200 scans and comparisons |
| 1000 | About 2000 scans and comparisons |
Pattern observation: The work grows roughly in direct proportion to the total number of rows combined.
Time Complexity: O(n)
This means the time to run the set operation grows linearly with the total number of rows involved.
[X] Wrong: "Set operations are instant no matter how big the tables are."
[OK] Correct: The database must look at every row to combine and remove duplicates, so bigger tables take more time.
Understanding how set operations scale helps you explain database performance clearly and confidently.
"What if we used UNION ALL instead of UNION? How would the time complexity change?"
Practice
UNION and INTERSECT in SQL?Solution
Step 1: Understand the purpose of set operations
Set operations like UNION and INTERSECT are designed to combine or compare rows from multiple tables.Step 2: Identify what set operations do not do
They do not create tables, update, or delete rows; those are different SQL commands.Final Answer:
To combine or compare rows from two or more tables easily -> Option DQuick Check:
Set operations combine or compare data [OK]
- Confusing set operations with data modification commands
- Thinking UNION creates a new permanent table
- Assuming INTERSECT deletes rows
Solution
Step 1: Recall correct UNION syntax
The correct syntax is to write one SELECT query, then UNION, then another SELECT query.Step 2: Check each option for syntax errors
SELECT * FROM table1 UNION SELECT * FROM table2; follows the correct syntax. Options A, B, and D have incorrect keywords or order.Final Answer:
SELECT * FROM table1 UNION SELECT * FROM table2; -> Option CQuick Check:
Correct UNION syntax = SELECT * FROM table1 UNION SELECT * FROM table2; [OK]
- Adding JOIN keyword with UNION
- Using commas instead of UNION
- Placing UNION at the end incorrectly
Table A: {1, 2, 3}Table B: {2, 3, 4}What is the result of
SELECT * FROM A INTERSECT SELECT * FROM B;?Solution
Step 1: Understand INTERSECT operation
INTERSECT returns only rows present in both tables.Step 2: Find common elements in Table A and Table B
Common elements are 2 and 3.Final Answer:
{2, 3} -> Option AQuick Check:
INTERSECT = common rows [OK]
- Confusing INTERSECT with UNION
- Including all rows from both tables
- Mixing up EXCEPT with INTERSECT
SELECT * FROM table1 UNION table2;What is the error and how to fix it?
Solution
Step 1: Identify syntax error in UNION usage
UNION requires two complete SELECT statements, but second query lacks SELECT.Step 2: Correct the query syntax
AddSELECT * FROMbeforetable2to fix the error.Final Answer:
Missing SELECT before table2; fix by adding SELECT * FROM table2 -> Option AQuick Check:
UNION needs two SELECTs [OK]
- Omitting SELECT in second query
- Using UNION with tables directly
- Adding unnecessary parentheses
List A: customers who bought product XList B: customers who bought product YHow do you find customers who bought either product X or Y but not both using set operations?
Solution
Step 1: Understand the problem
We want customers who bought product X or Y but not both (exclusive customers).Step 2: Use EXCEPT and UNION to find exclusive customers
Find customers in A but not in B, and customers in B but not in A, then combine them with UNION.Step 3: Check other options
SELECT * FROM A UNION SELECT * FROM B gives all customers who bought either product (including both). SELECT * FROM A INTERSECT SELECT * FROM B gives only those who bought both. (SELECT * FROM A EXCEPT SELECT * FROM B) UNION (SELECT * FROM A INTERSECT SELECT * FROM B) gives all customers who bought product X.Final Answer:
(SELECT * FROM A EXCEPT SELECT * FROM B) UNION (SELECT * FROM B EXCEPT SELECT * FROM A) -> Option BQuick Check:
Exclusive customers = (A EXCEPT B) UNION (B EXCEPT A) [OK]
- Using UNION alone includes both customers
- Using INTERSECT returns only common customers
- Confusing EXCEPT with INTERSECT
