Sub topic 7
Division and 'for all' queries
This chapter assumes you know selection, projection, set operations (union, difference) and the Cartesian product from the earlier chapters. It also assumes you can read a basic SQL SELECT. Division is the relational algebra operator for queries of the form "find the X that are related to all Y."
What division computes
Let R have attributes (A, B) and S have attributes (B). Here A and B can each stand for several attributes. The only requirement is that every attribute of S also appears in R.
The result R ÷ S has the attributes of R that are not in S, which is A. It contains each value a such that for every tuple b in S, the pair (a, b) is in R.
Take these instances:
| Enrolled.sid | Enrolled.cid |
|---|---|
| 1 | c1 |
| 1 | c2 |
| 1 | c3 |
| 2 | c1 |
| 2 | c2 |
| 3 | c1 |
| 3 | c2 |
| 3 | c3 |
| 3 | c4 |
The Course table has cid values c1, c2 and c3. Student 2 is missing c3, so only students 1 and 3 qualify. Student 3 is also enrolled in c4, which is not in Course. That extra course does not disqualify student 3. Division only requires that every divisor tuple is matched.
Why division is not a primitive
Division can be built from operators you already know. The idea is to find the students who are disqualified and subtract them.
R ÷ S = π_A(R) − π_A( (π_A(R) × S) − R )- π_A(R) × S lists every pair (student, course) that would have to exist for the student to qualify.
- Subtracting R leaves the required pairs that are missing.
- Projecting onto A gives the students with at least one missing pair. These students are disqualified.
- Subtracting them from π_A(R) leaves the qualifying students.
For the example, the product gives 9 pairs (3 students × 3 courses). Subtracting R leaves only (2, c3). The disqualified set is {2}, and the quotient is {1, 3}.
Writing the expression
Always project R down to exactly the attributes A and B before dividing. The query "students enrolled in every course" is:
π_sid, cid(Enrolled) ÷ π_cid(Course)If R has extra attributes, such as a grade column, the quotient's schema would include them and the result would be wrong. For a restricted divisor, select first. For example, "students enrolled in both DBMS and OS" is:
π_sid, cid(Enrolled) ÷ π_cid(σ_cname = 'DBMS' ∨ cname = 'OS' (Course))Divide when the question says "all", "every" or "both". Use plain join and selection when it says "some" or "at least one."
The same query in tuple calculus
Division corresponds to a universal quantifier. The quantifier ∀ can be rewritten with ∃ and ¬, which is how safe calculus handles it.
{ t | ∃e ∈ Enrolled ( t.sid = e.sid ∧
∀c ∈ Course ∃e2 ∈ Enrolled ( e2.sid = e.sid ∧ e2.cid = c.cid ) ) }The first ∃e ∈ Enrolled limits t to students who appear in Enrolled. This matches π_A(R) in the algebra definition.
The same query in SQL
SQL has no division operator. You express "for all" as "there is no course for which there is no enrollment":
CREATE TABLE Student (sid INT PRIMARY KEY, sname VARCHAR(20));
CREATE TABLE Course (cid VARCHAR(5) PRIMARY KEY, cname VARCHAR(20));
CREATE TABLE Enrolled (sid INT, cid VARCHAR(5), PRIMARY KEY (sid, cid));
INSERT INTO Student VALUES (1,'Asha'),(2,'Ben'),(3,'Chen'),(4,'Dev');
INSERT INTO Course VALUES ('c1','DBMS'),('c2','OS'),('c3','CN');
INSERT INTO Enrolled VALUES (1,'c1'),(1,'c2'),(1,'c3'),
(2,'c1'),(2,'c2'),(3,'c1'),(3,'c2'),(3,'c3'),(3,'c4');
SELECT s.sid
FROM Student s
WHERE NOT EXISTS (
SELECT * FROM Course c
WHERE NOT EXISTS (
SELECT * FROM Enrolled e
WHERE e.sid = s.sid AND e.cid = c.cid
)
);This returns students 1 and 3. A counting version also works:
SELECT e.sid
FROM Enrolled e
WHERE e.cid IN (SELECT cid FROM Course)
GROUP BY e.sid
HAVING COUNT(DISTINCT e.cid) = (SELECT COUNT(*) FROM Course);The IN filter keeps c4 from inflating the count. The counting version is correct only because Enrolled has no duplicate (sid, cid) pairs, and COUNT(DISTINCT ...) also protects against that.
Edge cases
- Empty divisor. If S is empty, every condition "for all b in S" holds trivially. The algebra formula gives π_A(R), because the product is empty and nothing is subtracted.
- Student with no enrollments. Student 4 is not in R, so the algebra result excludes them. The SQL
NOT EXISTSquery scans the Student table. With an empty Course table it would return student 4 as well. The two forms differ in that case. - Extra values in R. Tuples of R whose B value is not in S are ignored, as student 3's c4 shows.
- Duplicates. Algebra uses sets, so duplicates do not arise. SQL needs
DISTINCTif the query can emit repeated rows.
Counting results in GATE questions
Suppose |S| = k > 0. Each tuple in the quotient needs k matching tuples in R. This gives two bounds:
- |R ÷ S| ≤ |π_A(R)|.
- |R ÷ S| ≤ |R| / k, rounded down.
For example, if R has 12 tuples and S has 4, the quotient has at most 3 tuples. The minimum is 0, which happens when no value of A is paired with all of S.
Exercise
R(A, B) = {(a,1), (a,2), (b,1), (c,1), (c,2), (c,3)} and S(B) = {1, 2}. Compute R ÷ S, and state the maximum possible size of R ÷ S when |R| = 12 and |S| = 5.
Show answer
Value a has both 1 and 2, b lacks 2, and c has 1, 2 and 3 (the extra 3 is ignored). So R ÷ S = {a, c}. With |R| = 12 and |S| = 5, each quotient tuple needs 5 tuples in R, so the maximum is floor(12 / 5) = 2.
Exercise
What is R ÷ S when S is empty, and why does the SQL double NOT EXISTS pattern written over a separate Student table differ from this?
Show answer
R ÷ S = π_A(R), because the product with the empty S is empty and nothing is subtracted. The SQL pattern ranges over all rows of Student, so it also returns students that never appear in R, which the algebra expression cannot return.