Set Operations and Cartesian Product in Relational Algebra: Worked Examples and Exam Traps
Learn when relational set operations are legal, calculate exact union and difference results, enumerate a Cartesian product, and trace how a join filters it.
KnowledgeGate Team
Exam prep & CS education

Union, intersection and difference look like familiar set theory until relation schemas decide whether an operation is legal. Cartesian product follows a different rule, and even a few input rows can become many output rows. Union, intersection, and difference operate on two compatible class lists; a 3 x 2 product pairs every tuple; and a join filters a second product.
Set Operations in Relational Algebra Start with Relation Semantics
A relation is a set of distinct tuples. Row order has no meaning, including the convenient order used in worked tables.
Union, intersection and difference require union-compatible inputs: the same degree and compatible corresponding domains. Rename can align names, but cannot turn an integer domain into an unrelated text domain.
Cartesian product needs no union compatibility. It pairs every left tuple with every right tuple. Result degree is the sum of input degrees, while cardinality is the product of input cardinalities. Repeated attribute names need qualification or renaming.
Operator | Input requirement | Tuple rule | Output size cue |
|---|---|---|---|
| Union-compatible relations | Keep tuples present in either input |
|
| Union-compatible relations | Keep tuples present in both inputs |
|
| Union-compatible relations | Keep tuples in |
|
| Any two relations | Pair every |
|
Use CS Fundamentals for the broader DBMS route. Relational Algebra for GATE develops operator-wide tuple bounds and equivalence drills. MORNING and EVENING expose exact set membership; STUDENT x LAB enumerates every pair before a join predicate removes failures.
Set Operations Worked Example with Union-Compatible Relations
Take these two relations:
MORNING(Sid, Name) = {(101, Asha), (102, Bharat), (103, Charu)}
EVENING(Sid, Name) = {(102, Bharat), (103, Charu), (104, Dev)}
Both have degree 2. Their corresponding domains are integer Sid and text Name, so all three set operations are legal.
MORNING UNION EVENING = {(101, Asha), (102, Bharat), (103, Charu), (104, Dev)}MORNING INTERSECT EVENING = {(102, Bharat), (103, Charu)}MORNING - EVENING = {(101, Asha)}EVENING - MORNING = {(104, Dev)}(MORNING - EVENING) UNION (EVENING - MORNING) = {(101, Asha), (104, Dev)}
Tuple 101 is left-only, 104 is right-only, and tuples 102 and 103 occur in both. The common tuples enter the intersection and appear once in the union. Union and intersection are unchanged when operands swap. Difference is not.

Cardinality Formulas and Set Identities Without Guesswork
Here, |MORNING| = 3, |EVENING| = 3 and |MORNING INTERSECT EVENING| = 2. Therefore:
Union size is
3 + 3 - 2 = 4.MORNING - EVENINGhas3 - 2 = 1tuple.Symmetric-difference size is
3 + 3 - 2(2) = 2.
For finite union-compatible relations, let |R| = m, |S| = n and |R INTERSECT S| = k. Then union has m + n - k tuples, R - S has m - k, S - R has n - k, and the symmetric difference has m + n - 2k. The bounds are 0 <= k <= min(m,n) and max(m,n) <= |R UNION S| <= m + n.
For a fresh drill, use m = 5, n = 4, k = 2. Union has 5 + 4 - 2 = 7; R - S has 5 - 2 = 3; S - R has 4 - 2 = 2; the symmetric difference has 5 + 4 - 4 = 5; and R x S has exactly 5 x 4 = 20 tuples.
Also, (R - S) INTERSECT (S - R) is always empty. Any tuple in R - S is not in S, while any tuple in S - R is not in R, so no tuple can belong to both. Their union is the symmetric difference and need not be empty.
Cartesian Product Worked Example with All Six Tuples
Use STUDENT(Sid, Name) = {(101, Asha), (102, Bharat), (103, Charu)} and LAB(LabId, Room) = {(L1, 101), (L2, 204)}. Qualify the result schema as (STUDENT.Sid, STUDENT.Name, LAB.LabId, LAB.Room) so student Sid 101 is not confused with room 101.
STUDENT x LAB contains:
(101, Asha, L1, 101)(101, Asha, L2, 204)(102, Bharat, L1, 101)(102, Bharat, L2, 204)(103, Charu, L1, 101)(103, Charu, L2, 204)
A relation has no tuple-ordering promise. Cardinality is 3 x 2 = 6, while degree is 2 + 2 = 4. STUDENT x EMPTY_LAB has zero tuples because 3 x 0 = 0, although its intended schema still contains the combined attributes.
LAB x STUDENT also has six tuples, but reverses the attribute order and tuple construction. It becomes equivalent only after appropriate renaming and reordering, not through simple textual equality.

Cartesian Product Becomes a Join After Filtering
Keep STUDENT and define ENROLMENT(Sid, Course) = {(101, DBMS), (101, CN), (103, OS)}. The raw product has 3 x 3 = 9 four-attribute tuples.
The theta join is STUDENT JOIN_(STUDENT.Sid = ENROLMENT.Sid) ENROLMENT = SELECT_(STUDENT.Sid = ENROLMENT.Sid)(STUDENT x ENROLMENT). The equality test keeps exactly (101, Asha, 101, DBMS), (101, Asha, 101, CN) and (103, Charu, 103, OS). The other six pairs fail.
After a natural join keeps one shared Sid, the result is {(101, Asha, DBMS), (101, Asha, CN), (103, Charu, OS)}. This is safe here because Sid is the only common attribute. A natural join matches every identically named attribute.
The SQL Queries and Joins in DBMS guide connects product to CROSS JOIN and filtered product to INNER JOIN ... ON. Pure relational algebra uses set semantics, while SQL commonly preserves duplicates unless DISTINCT or a set operator changes that behaviour.
Set Operations and Cartesian Product Traps
Checking degree alone.
R(Sid, Name)andMARKS(Sid, Score)both have degree2, but textNameand numericScoreare incompatible. Rename changes a label, not a domain.Reversing difference.
MORNING - EVENING = {(101, Asha)}, butEVENING - MORNING = {(104, Dev)}.Adding product sizes. Three students and two labs produce
3 x 2 = 6, not5. The result has six tuples and four attributes.Importing SQL bag behaviour. Pure relations contain no duplicate tuples. SQL
UNIONremoves duplicate result rows, whileUNION ALLretains them.Calling every filtered product natural. A theta join uses any stated predicate. A natural join equates all same-named attributes and removes repeated copies of those join attributes. Write the schemas first.
How Exams Test Set Operations and Cartesian Product
Questions ask you to check union compatibility, compute a result relation, find cardinality or degree, judge an identity, or recognise selection over product as a join. Use one routine: write both schemas, check the operator rule, mark common tuples or form every pair, apply the predicate, and count last.
Three checkpoints make the routine concrete:
The symmetric difference of the class lists is
{(101, Asha), (104, Dev)}.If
|R| = 7,|S| = 5, and|R INTERSECT S| = 3, then union size is7 + 5 - 3 = 9,|R - S| = 7 - 3 = 4, and|R x S| = 7 x 5 = 35.In the enrolment trace, the product has
9pairs, equality keeps3, and the natural-join result has degree3after the repeatedSidis merged.
After you can reconstruct these results by hand, use the GATE Test Series for timed subject practice.
Short Version and the Next DBMS Step
Union, intersection and difference require union-compatible relations. Union keeps either-side tuples, intersection keeps common tuples, and difference keeps left-only tuples. Cartesian product pairs every left tuple with every right tuple.
The class-list example gives union 4, intersection 2, and one tuple in each directed difference. The student-lab product gives 3 x 2 = 6 tuples of degree 4.
Now reconstruct all five class-list results and all six product tuples without looking, then explain why the enrolment join keeps three of nine pairs. Zero to Hero provides a structured core-CS route, while the GATE Test Series adds subject practice.
Keep learning

Data Mining and Data Warehouse Explained: Concepts, OLAP and Worked Examples
Follow six orders from operational sources into a warehouse, calculate OLAP totals, and test an association rule on five shopping baskets.

DBMS Interview Questions: Keys, Normalisation and Transactions Through One Worked Schema
Build defensible DBMS interview answers by tracing keys, normal forms, ACID and isolation through one university-enrolment database.

Super Key, Candidate Key and Primary Key: A Worked DBMS Example
Use one ENROLMENT relation to derive two candidate keys, enumerate every super key, select a primary key and translate the result into SQL.

SQL Subqueries Tutorial: Scalar, IN, EXISTS, and Correlated Examples
Learn SQL subqueries from one small employee database. Trace each inner result, compare the main query forms, handle NULL safely, and check your understanding with four exercises.