Sub topic 4
Union, intersection and difference
This chapter assumes you know basic set operations and can read a relation as a set of tuples, as covered in the chapters on the relational model and on selection and projection. Here you will learn the three set operators of relational algebra, the condition that makes them legal, and how to bound the size of their results. These bounds appear often in GATE questions.
Relations as sets
In relational algebra a relation is a set of tuples. It has no duplicates and no order. Union, intersection and difference therefore behave exactly as they do on mathematical sets. SQL tables are different, because they can hold duplicate rows. That difference is covered near the end of the chapter.
Union compatibility
Two relations R and S are union compatible when both of these hold:
- They have the same number of attributes.
- The attribute at each position has the same domain (type) in both relations. For example, the first attributes of R and S are both integers.
Attribute names need not match. By the usual convention, the result takes its attribute names from the first operand. Some textbooks require identical names. If you need to line names up, use rename (ρ), which a later chapter covers.
Matching is by position, not by name. Take Student(sid, sname) and Course(cid, cname). They are union compatible if sid and cid are both integers and sname and cname are both strings. The union would still make no semantic sense, but the operator allows it.
The three operators
- Union, R ∪ S contains every tuple that is in R, in S, or in both. A tuple present in both appears once.
- Intersection, R ∩ S contains the tuples present in both R and S.
- Difference, R − S contains the tuples in R that are not in S.
Difference is the only one of the three that is not commutative. In general R − S ≠ S − R.
Intersection is derived
Intersection adds no power to the algebra. It can be written with difference alone:
R ∩ S = R − (R − S)R − S removes the tuples that are in R and also in S. Subtracting that result from R leaves exactly the tuples of R that are in S. Union and difference, together with selection, projection, product and rename, form the basic operator set.
Worked example
Use the Enrolled(sid, cid) relation from the running schema. Let the tuples be (1,10), (2,10), (3,10), (2,20), (3,20) and (4,20).
Define two single-column relations:
A = π_sid(σ_cid = 10(Enrolled)) = {1, 2, 3}
B = π_sid(σ_cid = 20(Enrolled)) = {2, 3, 4}| Expression | Meaning | Result |
|---|---|---|
| A ∪ B | Enrolled in course 10 or 20 | {1, 2, 3, 4} |
| A ∩ B | Enrolled in both courses | {2, 3} |
| A − B | In course 10 but not 20 | {1} |
| B − A | In course 20 but not 10 | {4} |
Projection comes before the set operator here. If you applied the operator to whole Enrolled tuples, no two tuples from different courses would ever match. A tuple (2,10) differs from (2,20).
Counting result sizes
GATE questions often give only the sizes of the relations, say |R| = m and |S| = n, and ask for the minimum or maximum result size. Assume R and S are union compatible.
| Expression | Minimum | Maximum |
|---|---|---|
| R ∪ S | max(m, n) | m + n |
| R ∩ S | 0 | min(m, n) |
| R − S | max(0, m − n) | m |
The extremes arise as follows:
- The union is largest when R and S share no tuples. It is smallest when the smaller relation is contained in the larger one.
- The intersection is empty when the relations are disjoint. It is largest when the smaller relation is a subset of the larger one.
- The difference R − S loses at most n tuples, so it keeps at least m − n. It loses nothing if the two relations are disjoint.
Set operators in SQL
SQL provides UNION, INTERSECT and EXCEPT. The SQL set operations chapter covers them fully. The important point here is that they match columns by position, as relational algebra does. The example below creates the Enrolled table and runs the difference A − B in PostgreSQL syntax.
CREATE TABLE Enrolled (sid INT, cid INT);
INSERT INTO Enrolled VALUES (1,10),(2,10),(3,10),(2,20),(3,20),(4,20);
SELECT sid FROM Enrolled WHERE cid = 10
EXCEPT
SELECT sid FROM Enrolled WHERE cid = 20;
-- returns one row: 1UNION, INTERSECT and EXCEPT remove duplicates, which matches set semantics. The variants UNION ALL, INTERSECT ALL and EXCEPT ALL keep duplicates. Oracle spells EXCEPT as MINUS.
Common errors
- Assuming a set operator is legal because the column names match. The number of attributes and the domains at each position decide it.
- Treating difference as symmetric. R − S and S − R are different relations.
- Forgetting to project before comparing. The operators compare whole tuples, so extra columns can make tuples differ.
- Using the minimum |R ∪ S| = m + n. That value is the maximum.
Exercise
R has 5 tuples and S has 8 tuples, and they are union compatible. Give the minimum and maximum number of tuples in R ∪ S, R ∩ S, R − S and S − R.
Show answer
R ∪ S: minimum 8 (R contained in S), maximum 13 (disjoint). R ∩ S: minimum 0, maximum 5. R − S: minimum 0 (R contained in S), maximum 5 (disjoint). S − R: minimum 8 − 5 = 3, maximum 8.
Exercise
Write R ∩ S using only the difference operator. Then use your expression to evaluate it for R = {1, 2, 3} and S = {2, 3, 4}.
Show answer
R ∩ S = R − (R − S). Here R − S = {1}, and R − {1} = {2, 3}, which equals R ∩ S.