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) s3. Natural Join (r ⋈ s)
A specialized equijoin that automatically:
- Identifies all attributes that have the exact same name in both relations.
- Enforces equality on those shared attributes.
- 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
rhave schema(A, B)and relationshave schema(B). - The division
r ÷ syields a relation with schema(A)containing tuplest[A]such that for every tupleu ∈ s, the concatenated tuple<t[A], u[B]>exists inr.
Formal Definition From Fundamental Operators
r ÷ s = π_(R - S)(r) - π_(R - S)((π_(R - S)(r) × s) - r)Step-by-step Intuition:
π_(R-S)(r): All candidate entities (e.g., all students).π_(R-S)(r) × s: All possible combinations of candidates and required targets (all students paired with every required course).(...) - r: Pairs that were NOT actually completed (disqualifications).π_(R-S)(r) - Disqualified: Candidates who have zero missing courses!
4. The Complete RA ↔ SQL Translation Table
| Relational Algebra Operation | Standard 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 × s | SELECT * FROM r CROSS JOIN s; (or FROM r, s) |
Theta Join r ⋈_θ s | SELECT * FROM r JOIN s ON θ; |
Natural Join r ⋈ s | SELECT * FROM r NATURAL JOIN s; |
Left Outer Join r ⟕ s | SELECT * FROM r LEFT OUTER JOIN s ON ...; |
Right Outer Join r ⟖ s | SELECT * FROM r RIGHT OUTER JOIN s ON ...; |
Full Outer Join r ⟗ s | SELECT * FROM r FULL OUTER JOIN s ON ...; |
Set Union r ∪ s | SELECT * FROM r UNION SELECT * FROM s; |
Set Difference r - s | SELECT * FROM r EXCEPT SELECT * FROM s; |
Set Intersection r ∩ s | SELECT * 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
Answer:
- Minimum: 0 rows (if there are no shared values of B).
- Maximum: 50 rows (if all 10 rows in R and all 5 rows in S have the exact same single value for B, producing a full cross-product for that value:
10 * 5 = 50).
Answer:
Using set difference:
π_id(student) - π_id(takes)Using antijoin:
student ▷ takesAnswer:
SQL does not have a native DIVIDE BY keyword. It implements division in two common ways:
- Double
NOT EXISTS: "Select students where there does NOT exist a course in CS that does NOT exist in their takes list." - Group By with
HAVING COUNT(*):SELECT id FROM takes WHERE course_id IN (SELECT course_id FROM course WHERE dept_name = 'Comp. Sci.') GROUP BY id HAVING COUNT(DISTINCT course_id) = (SELECT COUNT(*) FROM course WHERE dept_name = 'Comp. Sci.');