Sub topic 5
Cartesian product and rename
This chapter assumes you know selection and projection, and the set operations union, intersection and difference. It covers the Cartesian product (×) and the rename operator (ρ). Together they let relational algebra combine tables and compare a table with itself. You need both before joins make sense.
The Cartesian product
For relations R and S, the product R × S pairs every tuple of R with every tuple of S.
- Schema: the attributes of R followed by the attributes of S. The degree (number of columns) is the sum of the two degrees.
- Cardinality: |R × S| = |R| · |S|, because the product contains every pairing exactly once.
- Empty operand: if either relation is empty, the product is empty.
- Duplicates: the product of two sets is a set, so no duplicates arise in the algebra. SQL works with bags (tables that may hold duplicate rows), so its row counts can differ.
Take Student(sid, sname, age) with 4 tuples and Enrolled(sid, cid) with 6 tuples.
Student × Enrolled
degree = 3 + 2 = 5
cardinality = 4 × 6 = 24
attributes : Student.sid, sname, age, Enrolled.sid, cidBoth operands have an attribute named sid. A relation cannot have two attributes with the same name, so the standard convention is to qualify them as Student.sid and Enrolled.sid. This is a naming problem, not a data problem.
Product plus selection
A bare product is rarely useful, because most of its tuples pair unrelated rows. The usual pattern is a product followed by a selection that keeps only the meaningful pairs.
Names of students enrolled in course 10:
π_sname ( σ_{Student.sid = Enrolled.sid ∧ cid = 10} (Student × Enrolled) )The selection condition has two parts. The first, Student.sid = Enrolled.sid, is the join condition. The second, cid = 10, is an ordinary filter. The pattern σθ(R × S) is exactly the theta join R ⋈θ S, which the next chapter defines. In SQL, the comma form is also a product.
SELECT DISTINCT s.sname
FROM Student s, Enrolled e
WHERE s.sid = e.sid AND e.cid = 10;Without the WHERE clause, this query returns all 24 pairings.
Rename
The rename operator changes the name of a relation, its attributes, or both. Two common notations are:
ρ_{S1}(Student) -- relation renamed to S1
ρ_{S1(id, nm, ag)}(Student) -- relation S1 with attributes id, nm, ag
ρ_{(id, nm, ag)}(Student) -- attributes onlyRename has two uses.
- Resolve name clashes. Renaming Enrolled's attributes to (esid, ecid) removes the need for qualified names.
- Compare a relation with itself. If you write Student × Student, the result has duplicate attribute names and you cannot tell the two copies apart. Renaming each copy gives them distinct names.
Self-join: the oldest student
Self-join queries are the standard reason to learn rename. To find the oldest student, first find the students who are not oldest. A student is not oldest if some other student is strictly older. Then subtract those students from all students.
NotOldest = π_{S2.sid} ( σ_{S1.age > S2.age} ( ρ_{S1}(Student) × ρ_{S2}(Student) ) )
Oldest = π_sid(Student) − NotOldestS2 plays the role of the student who has someone older. The difference in the last step is needed because relational algebra has no "maximum" operator in its basic form. Any "most" or "least" query uses this pattern: find everyone who loses a comparison, then remove them.
The SQL version uses table aliases, which play the role of ρ.
SELECT sid
FROM Student
EXCEPT
SELECT s2.sid
FROM Student s1, Student s2
WHERE s1.age > s2.age;If two students tie for the oldest age, neither is in NotOldest because neither is strictly older than the other. Both therefore appear in the answer.
Counting results
GATE questions often ask for the minimum or maximum number of tuples in an expression involving a product. Reason from the cardinality rule.
| Expression | Minimum | Maximum | Column | Column | Column | Column |
|---|---|---|---|---|---|---|
| R × S with \ | R\ | = m, \ | S\ | = n | m·n | m·n |
| σ_θ(R × S) | 0 | m·n | ||||
| σ_{R.a = S.a}(R × S), where a is a key of S | 0 | m | ||||
| σ_{R.a = S.a}(R × S), where a is a key of both | 0 | min(m, n) |
The third row holds when R.a is a foreign key into S.a. Each tuple of R then matches exactly one tuple of S, so the result has exactly m tuples, not just at most m. Without the foreign key constraint, a tuple of R may match nothing, which gives the minimum of 0.
Common errors
- Forgetting the join condition.
FROM Student, Enrolledwith no matching condition returns every pairing. - Unqualified attribute names after a product when both operands have the attribute. Qualify them or rename.
- Self-join without rename. The two copies cannot be told apart.
- Using ≥ instead of > in "not oldest". With ≥, every student is older than or equal to themselves, so nobody is left in the difference.
- Treating the SQL row count as the algebra count. Without DISTINCT, SQL keeps duplicates that the algebra would have merged.
Exercise
Relation R has 5 tuples and 3 attributes, and S has 7 tuples and 2 attributes. What are the degree and cardinality of R × S? What are the minimum and maximum cardinality of σ_{R.a = S.b}(R × S)?
Show answer
The degree is 3 + 2 = 5 and the cardinality is 5 × 7 = 35. The selection keeps only the pairs that satisfy the condition. In the worst case every pair satisfies it, so the maximum is 35. In the best case none do, so the minimum is 0. If you add the information that S.b is a key, each tuple of R matches at most one tuple of S, which lowers the maximum to 5.
Exercise
Using Student(sid, sname, age), write a relational algebra expression for the sids of students who are not the youngest. Explain why rename is required.
Show answer
π{S1.sid}( σ{S1.age > S2.age}( ρ{S1}(Student) × ρ{S2}(Student) ) ). A student is not the youngest if some student S2 is strictly younger. Rename is required because the product of Student with itself would have duplicate attribute names, and the condition must refer to the two copies separately.