Skip to content
Contents/CH 11
Relational Model/Intermediate · 14 min

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

  1. 01State the union-compatibility requirement.
  2. 02Apply union, intersection, and difference.
  3. 03Know the difference between set and bag semantics.
  4. 04Recognize when a set operation is invalid.
01

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.

chapter-11.db
A(name, age) and B(student_name, years) are compatible
A(name, age) and B(name) are not compatible
02

Union, 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.

chapter-11.db
A = {1,2,3}
B = {3,4}

A ∪ B = {1,2,3,4}
A ∩ B = {3}
A - B = {1,2}
03

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.

Worked example3 steps

Who took C1 or C2?

Take π_student_id(σ_course_id = C1(Enroll)) and the same expression for C2.

01

Union the two relations.

Every student who took either course appears once.

02

Intersect the two relations.

Only students who took both courses remain.

03

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.

0 / 2 answered
01

What is required for union compatibility?

02

Which operation returns tuples in A but not in B?

DB//COSMOS

A field manual for database systems: model the domain, normalize the facts, query the relations, and protect the result.

Edition 2026 · 32 chaptersTopic catalogInterview prep