Relational Algebra: Cartesian Product & Joins
A join is a Cartesian product followed by a filter. That one sentence explains both why joins are powerful and why they can explode in size.
In plain English
A join matches two lists by a shared ID, like pairing students with their course cards.
The one idea
A join is a Cartesian product followed by a predicate; the predicate decides which combinations survive.
Normalization splits facts into separate relations, so joins are how the original entity is reconstructed. Join order and indexes decide whether that is fast or catastrophic.
By the end
- 01Compute a Cartesian product and its cardinality.
- 02Apply theta, equi, and natural joins.
- 03Know which columns a natural join keeps.
- 04Explain why join order matters for performance.
Cartesian product
The Cartesian product A × B pairs every tuple of A with every tuple of B. If A has m tuples and B has n, the result has m × n tuples and degree(A) + degree(B) attributes. It is rarely useful alone, but it is the foundation of joins.
A = {a1, a2}, B = {b1, b2}
A × B = {(a1,b1),(a1,b2),(a2,b1),(a2,b2)}
cardinality = 2 × 2 = 4Theta and equi joins
A theta join A ⋈θ B is σθ(A × B): pair the tuples, then keep the pairs that satisfy θ. An equi join uses equality, such as Student.id = Enroll.student_id. The join condition can compare columns from either relation.
Student(id, name)
Enroll(student_id, course)
Student ⋈ Student.id = Enroll.student_id EnrollNatural join
A natural join matches tuples on all attributes with the same name and keeps one copy of each shared column. It is convenient when schemas are designed with consistent names, but dangerous when a shared name is accidental. Explicit join conditions are safer in production queries.
Student(id, name)
Enroll(id, course)
Student ⋈ Enroll /* matches on id, keeps one id */Students and their enrollments
Student has 3 rows and Enroll has 3 rows. The Cartesian product has 9 combinations.
Form Student × Enroll.
Nine candidate rows, most with mismatched student ids.
Keep rows where Student.id = Enroll.student_id.
Three matching rows remain: Ada twice and Grace once.
Use a left outer join instead.
Linus remains with null course and grade, because he has no enrollment.
ConclusionAn inner join drops unmatched rows. An outer join preserves them with nulls. The choice is a business rule, not a formatting preference.
Instrument
Watch it happen.
Step through one operation at a time
Start with both relations
A join pairs rows from two relations and keeps the pairs that satisfy the join condition.
| id | name |
|---|---|
| 1 | Ada |
| 2 | Grace |
| 3 | Linus |
| student_id | course_id | grade |
|---|---|---|
| 1 | C1 | A |
| 1 | C2 | B |
| 2 | C1 | A |
The trade-off
No free correctness.
Gain
Reconstruct entities without duplicating facts in storage.
Cost
Joins consume memory and I/O; a bad join order can multiply intermediate rows.
Verdict
Normalize for integrity, then index the join keys and verify the plan.
Recap
What to keep.
Takeaways
- +Cartesian product pairs every tuple with every tuple.
- +A theta join is a Cartesian product plus a predicate.
- +An equi join uses equality in the predicate.
- +A natural join matches all shared attribute names.
- +Join order affects cost even when the result is the same.
Traps
- !Forgetting the join predicate and accidentally creating a Cartesian product.
- !Assuming a natural join is always safe.
- !Ignoring the cardinality explosion of a product.
Focus
- →Compute the cardinality of a Cartesian product.
- →Write a theta join for two small relations.
- →Explain the difference between equi and natural join.
Check yourself
Before you turn the page.
If A has 3 tuples and B has 4, how many tuples does A × B have?
What does a natural join match on?