DBMSCSL303 / MAL505
Module 1: Relational Foundations

Lecture 04: Relational Algebra II & The Join Family

Joins (Theta, Equi, Natural, Outer), Relational Division (÷), Extended RA, and the full RA ↔ SQL translation table

Lecture Source Slide Deck

Original Slides: Lecture 04 (46 slides) · Amit Kumar Dhar (IIT Bhilai)
Download lecture04.pdf

1. The Join Family

Writing Cartesian product followed by selection (r × s followed by σ_θ) is clumsy and generates massive intermediate relation states. The join family formalizes this combination.

1. Theta Join (r ⋈_θ s)

Combines Cartesian product with an explicit selection predicate θ:

r ⋈_θ s = σ_θ(r × s)
  • Predicate θ can involve any comparison operator (=, !=, <, <=, >, >=).

2. Equijoin

A Theta Join where the join predicate consists exclusively of equality conditions (=) across attributes from both tables:

r ⋈_(r.A = s.A AND r.B = s.B) s

3. Natural Join (r ⋈ s)

A specialized equijoin that automatically:

  1. Identifies all attributes that have the exact same name in both relations.
  2. Enforces equality on those shared attributes.
  3. Projects out duplicate column copies, returning only one column for each shared attribute name.
Schema(r ⋈ s) = (R ∪ S)

The Natural Join Trap

If two unrelated tables coincidentally share attribute names (e.g., both student and course have an attribute named name or credits), a natural join will silently filter out rows where student.name != course.name. In production SQL, prefer explicit INNER JOIN ... ON or USING.


2. Outer Joins

Standard (inner) joins discard tuples that have no matching counterpart in the opposing relation. Outer joins preserve dangling tuples by padding missing attributes with NULL.

Relation R             Relation S
+----+-------+        +----+-------+
| id | name  |        | id | grade |
+----+-------+        +----+-------+
| 1  | Alice |        | 1  | A     |
| 2  | Bob   |        | 3  | B     |
+----+-------+        +----+-------+

Inner Join (R ⋈ S):
+----+-------+-------+
| id | name  | grade |
+----+-------+-------+
| 1  | Alice | A     |
+----+-------+-------+

Left Outer Join (R ⟕ S): Bob is preserved!
+----+-------+-------+
| id | name  | grade |
+----+-------+-------+
| 1  | Alice | A     |
| 2  | Bob   | NULL  |
+----+-------+-------+

Full Outer Join (R ⟗ S): Both Alice, Bob, and student 3 preserved!
+----+-------+-------+
| id | name  | grade |
+----+-------+-------+
| 1  | Alice | A     |
| 2  | Bob   | NULL  |
| 3  | NULL  | B     |
+----+-------+-------+

3. Relational Division (÷)

Relational division is the mathematical equivalent of universal quantification ("FOR ALL" queries).

  • Canonical Query Pattern: "Find all students who have taken ALL courses offered by the Computer Science department."
  • Let relation r have schema (A, B) and relation s have schema (B).
  • The division r ÷ s yields a relation with schema (A) containing tuples t[A] such that for every tuple u ∈ s, the concatenated tuple <t[A], u[B]> exists in r.

Formal Definition From Fundamental Operators

r ÷ s = π_(R - S)(r) - π_(R - S)((π_(R - S)(r) × s) - r)

Step-by-step Intuition:

  1. π_(R-S)(r): All candidate entities (e.g., all students).
  2. π_(R-S)(r) × s: All possible combinations of candidates and required targets (all students paired with every required course).
  3. (...) - r: Pairs that were NOT actually completed (disqualifications).
  4. π_(R-S)(r) - Disqualified: Candidates who have zero missing courses!

4. The Complete RA ↔ SQL Translation Table

Relational Algebra OperationStandard SQL Equivalent
Selection σ_p(r)SELECT * FROM r WHERE p;
Projection π_(A1, A2)(r)SELECT DISTINCT A1, A2 FROM r;
Rename ρ_S(r)SELECT * FROM r AS S;
Cartesian Product r × sSELECT * FROM r CROSS JOIN s; (or FROM r, s)
Theta Join r ⋈_θ sSELECT * FROM r JOIN s ON θ;
Natural Join r ⋈ sSELECT * FROM r NATURAL JOIN s;
Left Outer Join r ⟕ sSELECT * FROM r LEFT OUTER JOIN s ON ...;
Right Outer Join r ⟖ sSELECT * FROM r RIGHT OUTER JOIN s ON ...;
Full Outer Join r ⟗ sSELECT * FROM r FULL OUTER JOIN s ON ...;
Set Union r ∪ sSELECT * FROM r UNION SELECT * FROM s;
Set Difference r - sSELECT * FROM r EXCEPT SELECT * FROM s;
Set Intersection r ∩ sSELECT * FROM r INTERSECT SELECT * FROM s;
Aggregation G G_COUNT(A)(r)SELECT G, COUNT(A) FROM r GROUP BY G;

5. Review Exercises & Practice Problems

On this page