Sub topic 9
Writing queries in relational algebra
This chapter assumes you know the operators from the earlier chapters: selection (σ), projection (π), the set operations, product with rename (ρ), joins and division. It teaches you to turn an English question into an expression. The method is to recognise which pattern the question matches, then assemble that pattern from the operators.
All examples use this schema: Student(sid, sname, age), Course(cid, cname), Enrolled(sid, cid).
Sample instance:
| Relation | Tuples |
|---|---|
| Student | (1, Asha, 21), (2, Ben, 19), (3, Chen, 22), (4, Dev, 20) |
| Course | (10, DBMS), (20, OS) |
| Enrolled | (1,10), (1,20), (2,10), (3,20) |
A method for any query
- Decide what the output columns are. This is usually the last projection.
- Decide which relations hold the needed attributes.
- Identify the pattern: a plain filter, a join, a self-join ("at least two"), a difference ("none", "but not"), an intersection ("both"), or division ("every").
- Build inside out, then check the attribute names at each step.
Pattern 1: filter and join
Names of students enrolled in course 10:
π_sname (Student ⋈ σ_cid = 10 (Enrolled))Filter early, join next, project last. Student and Enrolled share only sid, so the natural join matches on exactly the attribute you want. Check this each time. If two relations share an attribute you did not mean to match, the natural join silently adds a condition. Rename the attribute first.
Pattern 2: self-join for "at least two"
Students enrolled in at least two different courses:
π_E1.sid ( σ_(E1.sid = E2.sid ∧ E1.cid ≠ E2.cid) ( ρ_E1(Enrolled) × ρ_E2(Enrolled) ) )Selection looks at one tuple at a time. To compare two enrollments of the same student, you need both in one tuple, so you take a product of two renamed copies. Without ρ the two copies have identical attribute names and the expression is ambiguous. On the sample instance the result is {1}.
"At least three" needs three copies and pairwise ≠ conditions. "Exactly one" is "at least one" minus "at least two".
Pattern 3: difference for "no" and "but not"
Students with no enrollment:
π_sid (Student) − π_sid (Enrolled)The result is {4}. Always subtract projected keys, then join back to Student if you need names:
π_sname ( Student ⋈ ( π_sid (Student) − π_sid (Enrolled) ) )Students in course 10 but not in course 20:
π_sid (σ_cid = 10 (Enrolled)) − π_sid (σ_cid = 20 (Enrolled))The result is {2}.
Pattern 4: intersection for "both"
Students enrolled in both course 10 and course 20:
π_sid (σ_cid = 10 (Enrolled)) ∩ π_sid (σ_cid = 20 (Enrolled))The result is {1}.
Pattern 5: division for "every"
Students enrolled in every course:
Enrolled ÷ π_cid (Course)Enrolled has exactly the attributes (sid, cid), and the divisor has (cid), so the quotient has attribute sid. The result is {1}. Student 1 is paired with both 10 and 20.
If you need names, join afterwards:
π_sname ( Student ⋈ ( Enrolled ÷ π_cid (Course) ) )Students who are not enrolled in every course are the complement within Student:
π_sid (Student) − ( Enrolled ÷ π_cid (Course) )This gives {2, 3, 4}. Student 4 appears because the complement is taken from all students, not only those in Enrolled.
Division edge cases
- If the divisor is empty, every sid that appears in Enrolled qualifies, by the definition
π_A(R) − π_A((π_A(R) × S) − R). Students with no enrollments are still excluded, because they are not inπ_sid(Enrolled). - If the dividend has extra attributes, project them away first. Division needs the divisor's attributes to be a subset of the dividend's.
- "Only" queries (students who took only courses from some set) are not division. They usually use difference: students with some enrollment outside the set are removed from students with any enrollment.
Combining patterns
Names of students older than 20 who are enrolled in every course:
π_sname ( σ_age > 20 (Student) ⋈ ( Enrolled ÷ π_cid (Course) ) )Names of students enrolled in no course that has "DBMS" as its name:
π_sname ( Student ⋈
( π_sid (Student) − π_sid ( Enrolled ⋈ σ_cname = 'DBMS' (Course) ) ) )The inner join finds enrollments in DBMS, the projection keeps student ids, and the difference removes those students.
Common errors
| Error | Why it fails | Fix |
|---|---|---|
| Putting a multi-row condition in one σ | σ sees one tuple at a time | Self-join or intersection |
| Self-join without ρ | Attribute names collide | Rename each copy |
| Projecting away a needed attribute too early | Later join or selection cannot find it | Project last |
| Using "not equal" in a join for "not enrolled" | It finds students with some other enrollment, not none | Use set difference |
| Joining by accident on a shared attribute | Natural join matches all common names | Rename or use a theta join |
Checking your answer
Run each candidate expression on a small instance like the one above. Include a student with no rows in Enrolled, since that case separates most correct answers from wrong ones. Check the final attribute names against the question as well.
Exercise
Write a relational algebra expression for the sids of students enrolled in course 20 but not in course 10, then evaluate it on the sample instance. Also write an expression for the names of students enrolled in no course other than course 10, where such students must have at least one enrollment.
Show answer
First: πsid(σcid = 20 (Enrolled)) − πsid(σcid = 10 (Enrolled)). Course 20 has students {1, 3}, course 10 has {1, 2}, so the result is {3}. Second: let X = πsid(σcid = 10 (Enrolled)) be students in course 10, and Y = πsid(σcid ≠ 10 (Enrolled)) be students with some other enrollment. The expression is π_sname(Student ⋈ (X − Y)). Here X = {1, 2} and Y = {1, 3}, so X − Y = {2} and the answer is Ben. The set X guarantees at least one enrollment, and subtracting Y removes anyone with an enrollment outside course 10.
Exercise
Which student ids does Enrolled ÷ π_cid(Course) return if Course is empty, using the instance above for Enrolled?
Show answer
Every sid in πsid(Enrolled), which is {1, 2, 3}. The definition πA(R) − πA((πA(R) × S) − R) with S empty gives the product as empty, so nothing is subtracted. Student 4 is not returned because it has no tuple in Enrolled.