Relation Algebra

Relational algebra is a theoretical model for querying which is composed of “What to do” and “How to do” on structured data. It is composed of mathematical expressions for representing data operations and is fundamentally based on set theory.

Operators, Operands, and Their Types

The operands are the relations (tables) of the database, and the operators are predefined functions that perform actions on those relations. Each operator takes one or more relations as input and produces a new relation as output maintaining closure of the algebra.

SAMPLE DATA FOR THE EXAMPLE

Employee
EmpIDNameDeptIDSalary
1Alice1060000
2Bob2055000
3Carol1070000
4David3040000
5Eve2075000
TempEmployee
EmpIDNameDeptIDSalary
6Frank2050000
7Grace1072000
Department
DeptIDDeptNameLocation
10HRDelhi
20ITMumbai
30FinanceBangalore

Projection (π)

Projection selects specific attributes (columns) from a relation, removing duplicates by default. In order to project two relation as one they have to be performed through joins, product or union.

NameSalary
Alice60000
Bob55000
Carol70000
David40000
Eve75000

Selection (σ)

Selection retrieves tuples (rows) from a relation that satisfy a given predicate (condition). In order to pass two tables they must have a relation.

EmpIDNameDeptIDSalary
3Carol1070000
5Eve2075000

Union (∪)

Union gives the combined tuples of the tables.

EmpIDNameDeptIDSalary
1Alice1060000
2Bob2055000
3Carol1070000
4David3040000
5Eve2075000
6Frank2050000
7Grace1072000

Cross Product (×)

Cartesian or Cross Product combines every tuple of one relation with every tuple of another.

EmpIDNameEmp.DeptIDSalaryDept.DeptIDDeptNameLocation
1Alice106000010HRDelhi
2Bob205500020ITMumbai
3Charlie107000010HRDelhi
4David305000030MarketingBangalore
5Eve208000020ITMumbai

Set Difference (−)

Set Difference returns tuples that are in one relation but not in another.

EmpIDNameDeptIDSalary

Intersection (∩)

EmpIDNameDeptIDSalary

Rename (ρ)

Rename changes the name of a relation or its attributes, allowing reusability or self-joins. Only mention attributes are kept; unmentioned attribute is dropped.

Join (⋈)

Join TypeSymbol / ExpressionPurpose / ResultEquivalent Basic Operation
Theta Join (θ-Join)( R \bowtie_{\theta} S )Combines tuples of R and S that satisfy a general condition (e.g. R.A > S.B)( \sigma_{\theta}(R \times S) )
Equi Join( R \bowtie_{R.A = S.B} S )Combines tuples using equality condition only( \sigma_{R.A = S.B}(R \times S) )
Natural Join( R \bowtie S )Joins automatically on all common attributes and removes duplicates( \pi_{\text{all attrs except duplicate}}(\sigma_{\text{R.common = S.common}}(R \times S)) )
Left Outer Join( R ⟕ S )Keeps all tuples of R, adds S where condition matches, fills unmatched S with NULL( (R \bowtie S) \cup (R - \pi_R(R \bowtie S)) )
Right Outer Join( R ⟖ S )Keeps all tuples of S, adds R where condition matches, fills unmatched R with NULL( (R \bowtie S) \cup (S - \pi_S(R \bowtie S)) )
Full Outer Join( R ⟗ S )Keeps all tuples from both sides, unmatched ones padded with NULL( (R ⟕ S) \cup (R ⟖ S) )
Left Semi Join( R ⋉ S )Returns only tuples from R that have a match in S( \pi_R(R \bowtie S) )
Right Semi Join( R ⋊ S )Returns only tuples from S that have a match in R( \pi_S(R \bowtie S) )
Anti Join( R ▷ S )Returns tuples from R that do not match any tuple in S( R - (R ⋉ S) )

Division (÷)

For division between R1 AND R2 every attribute of R2 the divisor must be present in the R1 , matter fact the R2 must be a subset of R1.

R1

St_NameC_Name
TomDBMS
JohnDS
TomDS
TomCN
JohnDBMS
AmyCN
AmyDBMS
AmyDS
R2
C_Name
DBMS
DS
CN
RESULT
St_Name
Tom
Amy