Post

03. Relation Algebra

03. Relation Algebra

Relation Algebra

Employees

EmpIDNameAgeDeptID
1Alice30D1
2Bob25D2
3Charlie35D1

Departments

DeptIDDeptNameLocation
D1HRNew York
D2ITSan Francisco

Relational Algebra Operations

1. Selection (σ) (Duplicated Data should be removed)

Select tuples that satisfy a condition.

Example: Select employees older than 30.
Expression: σAge > 30(Employees)
Result:

EmpIDNameAgeDeptID
3Charlie35D1

2. Projection (π) (Duplicated Data should be removed)

Extract specific columns (attributes) from a relation.

Example: Project only the Name and Age attributes.
Expression: πName, Age(Employees)
Result:

NameAge
Alice30
Bob25
Charlie35

3. Union (∪)

Combine tuples from two union-compatible relations.

First, form two subsets of Employees:

  • R1: Employees in department D1
  • R1 = σDeptID = ‘D1’(Employees)
  • R1 Result:
EmpIDNameAgeDeptID
1Alice30D1
3Charlie35D1
  • R2: Employees younger than 30
  • R2 = σAge < 30(Employees)
  • R2 Result:
EmpIDNameAgeDeptID
2Bob25D2

Union Expression: R1 ∪ R2 Result:

EmpIDNameAgeDeptID
1Alice30D1
3Charlie35D1
2Bob25D2

4. Set Difference (−)

Return tuples in one relation that are not in the other.

Example: Employees in D1 that are not younger than 30 (using the same subsets).
Expression: R1 − R2
Result:

EmpIDNameAgeDeptID
1Alice30D1
3Charlie35D1

5. Set Intersection (∩)

Return tuples common to both relations.

Example: With our chosen subsets, there is no common tuple since no employee is both in D1 and younger than 30.
Expression: R1 ∩ R2
Result:
Empty relation
(If overlapping conditions were chosen, common tuples would be returned.)


6. Cartesian Product (×)

Combine every tuple of one relation with every tuple of another.

Expression: Employees × Departments
Result:
This produces 3 × 2 = 6 tuples. For example, one of the tuples is:

7. Natural Join (⨝)

Combine tuples from two relations based on common attribute(s).

natural-join.png

Expression: Employees ⨝ Departments
Result:
The join is done on the common attribute DeptID. The result is:

EmpIDNameAgeDeptIDDeptNameLocation
1Alice30D1HRNew York
3Charlie35D1HRNew York
2Bob25D2ITSan Francisco

8. Rename (ρ)

Change the name of a relation or its attributes. rename-operator.png Example: Rename Employees to Emp and change attribute DeptID to Department.
Expression: ρEmp(EID, Name, Age, Department)(Employees)
Result:
The resulting relation Emp has attributes renamed as specified:

EIDNameAgeDepartment
1Alice30D1
2Bob25D2
3Charlie35D1

This post is licensed under CC BY 4.0 by the author.