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

Updated 29 Sep 20266 min read

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

Union-compatible relations

Keep tuples present in either input

m + n - k

INTERSECT

Union-compatible relations

Keep tuples present in both inputs

k

R - S

Union-compatible relations

Keep tuples in R but not S

m - k

R x S

Any two relations

Pair every R tuple with every S tuple

m x n

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.

Venn diagram of the MORNING and EVENING relations, with the union, intersection and both difference results listed below.

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 - EVENING has 3 - 2 = 1 tuple.

  • 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:

  1. (101, Asha, L1, 101)

  2. (101, Asha, L2, 204)

  3. (102, Bharat, L1, 101)

  4. (102, Bharat, L2, 204)

  5. (103, Charu, L1, 101)

  6. (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.

Grid pairing three STUDENT rows with two LAB rows to give the six tuples of STUDENT x LAB, each of degree four.

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

  1. Checking degree alone.R(Sid, Name) and MARKS(Sid, Score) both have degree 2, but text Name and numeric Score are incompatible. Rename changes a label, not a domain.

  2. Reversing difference.MORNING - EVENING = {(101, Asha)}, but EVENING - MORNING = {(104, Dev)}.

  3. Adding product sizes. Three students and two labs produce 3 x 2 = 6, not 5. The result has six tuples and four attributes.

  4. Importing SQL bag behaviour. Pure relations contain no duplicate tuples. SQL UNION removes duplicate result rows, while UNION ALL retains them.

  5. 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 is 7 + 5 - 3 = 9, |R - S| = 7 - 3 = 4, and |R x S| = 7 x 5 = 35.

  • In the enrolment trace, the product has 9 pairs, equality keeps 3, and the natural-join result has degree 3 after the repeated Sid is 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.