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_ID | Name | District |
|---|---|---|
| 101 | Rafi Ahmed | Dhaka |
| 102 | Tanvir Hasan | Rajshahi |
| 103 | Imran Hossain | Chattogram |
| 104 | Sumaiya Akter | Dhaka |
Relation 2: Student_EEE
| Student_ID | Name | District |
|---|---|---|
| 103 | Imran Hossain | Chattogram |
| 105 | Faisal Karim | Dhaka |
| 106 | Shanta Rahman | Khulna |
🟩 Example 1: Simple Set Difference
Operation:
Student_CSE − Student_EEE
Result:
| Student_ID | Name | District |
|---|---|---|
| 101 | Rafi Ahmed | Dhaka |
| 102 | Tanvir Hasan | Rajshahi |
| 104 | Sumaiya Akter | Dhaka |
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_ID | Name | District |
|---|---|---|
| 105 | Faisal Karim | Dhaka |
| 106 | Shanta Rahman | Khulna |
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_ID | Name | District |
|---|---|---|
| 101 | Rafi Ahmed | Dhaka |
| 104 | Sumaiya Akter | Dhaka |
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
| Feature | Description |
|---|---|
| Operator | − (Set Difference) |
| Purpose | Find tuples present in one relation but not in another |
| Input | Two union-compatible relations |
| Output | Tuples from R that are not in S |
| SQL Equivalent | EXCEPT 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
কোন মন্তব্য নেই:
একটি মন্তব্য পোস্ট করুন