Relational Algebra: Set Operations
Set operations combine whole relations. The catch is union compatibility: the two inputs must have the same shape.
In plain English
Combine two name lists: union is everyone, intersection is people on both lists, and difference is people only on the first list.
The one idea
Union, intersection, and difference operate on whole tuples and require union-compatible relations.
They are the clean way to combine result sets with the same shape, and they expose when two queries are not actually comparable.
By the end
- 01State the union-compatibility requirement.
- 02Apply union, intersection, and difference.
- 03Know the difference between set and bag semantics.
- 04Recognize when a set operation is invalid.
Union compatibility
Two relations are union-compatible when they have the same number of attributes and corresponding attributes have compatible domains. Names need not match, but the positions must line up. Without compatibility, union, intersection, and difference are undefined.
A(name, age) and B(student_name, years) are compatible
A(name, age) and B(name) are not compatibleUnion, intersection, difference
Union returns tuples in either relation. Intersection returns tuples in both. Difference returns tuples in the first but not the second. All three eliminate duplicates under set semantics.
A = {1,2,3}
B = {3,4}
A ∪ B = {1,2,3,4}
A ∩ B = {3}
A - B = {1,2}Set versus bag semantics
The relational model uses sets, so duplicates disappear. SQL tables are bags by default: UNION ALL keeps duplicates, UNION removes them. Knowing which semantics you are using is essential when counting rows or comparing query results.
Who took C1 or C2?
Take π_student_id(σ_course_id = C1(Enroll)) and the same expression for C2.
Union the two relations.
Every student who took either course appears once.
Intersect the two relations.
Only students who took both courses remain.
Subtract the C2 set from the C1 set.
Students who took C1 but not C2 remain.
ConclusionSet operations are only valid when both inputs have the same number and compatible types of attributes. SQL's UNION, INTERSECT, and EXCEPT follow the same rule.
The trade-off
No free correctness.
Gain
Composable answers without repeating the same predicate in multiple queries.
Cost
Duplicate elimination needs sorting or hashing, and SQL multiset semantics may surprise you.
Verdict
Use set operations for shape-compatible results; use joins when the rows must be combined column-wise.
Recap
What to keep.
Takeaways
- +Set operations require union-compatible relations.
- +Union combines, intersection keeps common tuples, difference subtracts.
- +Set semantics eliminate duplicates.
- +SQL distinguishes UNION from UNION ALL.
- +Compatibility is about shape and domains, not column names.
Traps
- !Applying union to relations with different degrees.
- !Assuming SQL UNION ALL removes duplicates.
- !Ignoring domain compatibility even when positions match.
Focus
- →Test two relations for union compatibility.
- →Compute union, intersection, and difference on small sets.
- →Explain set versus bag semantics.
Check yourself
Before you turn the page.
What is required for union compatibility?
Which operation returns tuples in A but not in B?