Skip to content
Contents/CH 12
Relational Model/Advanced · 16 min

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

  1. 01Compute a Cartesian product and its cardinality.
  2. 02Apply theta, equi, and natural joins.
  3. 03Know which columns a natural join keeps.
  4. 04Explain why join order matters for performance.
01

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.

chapter-12.db
A = {a1, a2}, B = {b1, b2}
A × B = {(a1,b1),(a1,b2),(a2,b1),(a2,b2)}
cardinality = 2 × 2 = 4
02

Theta 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.

chapter-12.db
Student(id, name)
Enroll(student_id, course)

Student ⋈ Student.id = Enroll.student_id Enroll
03

Natural 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.

chapter-12.db
Student(id, name)
Enroll(id, course)

Student ⋈ Enroll  /* matches on id, keeps one id */
Worked example3 steps

Students and their enrollments

Student has 3 rows and Enroll has 3 rows. The Cartesian product has 9 combinations.

01

Form Student × Enroll.

Nine candidate rows, most with mismatched student ids.

02

Keep rows where Student.id = Enroll.student_id.

Three matching rows remain: Ada twice and Grace once.

03

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

Step player01 / 03
01

Start with both relations

A join pairs rows from two relations and keeps the pairs that satisfy the join condition.

Student
idname
1Ada
2Grace
3Linus
Enroll
student_idcourse_idgrade
1C1A
1C2B
2C1A

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.

0 / 2 answered
01

If A has 3 tuples and B has 4, how many tuples does A × B have?

02

What does a natural join match on?

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