Relational Algebra – Set Difference (−) Operation in DBMS

Relational Algebra Operations – Set Difference (−)

The Set Difference operation in Relational Algebra returns all tuples that are present in the first relation (R) but not in the second relation (S). It helps to find data that exists in one table but not in another — just like MINUS or EXCEPT in SQL.


🔹 Definition

Notation:

R − S

Here, R and S are two union-compatible relations (same attributes and domains). The result contains tuples that appear in R but not in S.


🔹 Union Compatibility Rules

  • Both relations must have the same number of attributes.
  • Corresponding attributes must have the same domain (data type).
  • Attribute names should be identical or have the same meaning.

🔹 Example (Bangladeshi University Dataset)

Relation 1: Student_CSE

Student_IDNameDistrict
101Rafi AhmedDhaka
102Tanvir HasanRajshahi
103Imran HossainChattogram
104Sumaiya AkterDhaka

Relation 2: Student_EEE

Student_IDNameDistrict
103Imran HossainChattogram
105Faisal KarimDhaka
106Shanta RahmanKhulna

🟩 Example 1: Simple Set Difference

Operation:

Student_CSE − Student_EEE

Result:

Student_IDNameDistrict
101Rafi AhmedDhaka
102Tanvir HasanRajshahi
104Sumaiya AkterDhaka

Explanation: These are the students who are enrolled in the CSE department but not in the EEE department.


🟩 Example 2: Reversing the Operation

Operation:

Student_EEE − Student_CSE

Result:

Student_IDNameDistrict
105Faisal KarimDhaka
106Shanta RahmanKhulna

Explanation: This result shows students who belong to EEE but not to CSE.


🟩 Example 3: Combined with Selection

Operation:

σDistrict='Dhaka'(Student_CSE − Student_EEE)

Result:

Student_IDNameDistrict
101Rafi AhmedDhaka
104Sumaiya AkterDhaka

Explanation: This query finds students from Dhaka who study in CSE but not in EEE.


🔹 Properties of Set Difference

  • Not Commutative: R − S ≠ S − R
  • Non-Duplicative: Duplicate tuples are automatically removed.
  • Requires Union Compatibility: Relations must have the same attributes and domains.

🔹 SQL Equivalent

SELECT * FROM Student_CSE
EXCEPT
SELECT * FROM Student_EEE;

or in some databases:

SELECT * FROM Student_CSE
MINUS
SELECT * FROM Student_EEE;

🔹 Real-Life Use (Bangladesh Example)

  • Finding students enrolled in one department but not another (e.g., CSE but not EEE).
  • Finding employees who work in Dhaka branch but not in Chattogram branch.
  • Finding suppliers who serve one region but not another (e.g., Khulna but not Rajshahi).

✅ Summary Table

FeatureDescription
Operator− (Set Difference)
PurposeFind tuples present in one relation but not in another
InputTwo union-compatible relations
OutputTuples from R that are not in S
SQL EquivalentEXCEPT or MINUS

relational algebra set difference operation, difference example dbms, minus operation relational algebra, set difference with bangladeshi data, cse eee student difference, relational algebra tutorial bangladesh, dbms minus example

কোন মন্তব্য নেই:

একটি মন্তব্য পোস্ট করুন