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
EmpID
Name
DeptID
Salary
1
Alice
10
60000
2
Bob
20
55000
3
Carol
10
70000
4
David
30
40000
5
Eve
20
75000
TempEmployee
EmpID
Name
DeptID
Salary
6
Frank
20
50000
7
Grace
10
72000
Department
DeptID
DeptName
Location
10
HR
Delhi
20
IT
Mumbai
30
Finance
Bangalore
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.
πName,Salary(Employee)
Name
Salary
Alice
60000
Bob
55000
Carol
70000
David
40000
Eve
75000
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.
Rename changes the name of a relation or its attributes, allowing reusability or self-joins.
Only mention attributes are kept; unmentioned attribute is dropped.
ρnewEmployee(Name,Salary)(Employee)degree(ρnewEmployee(Name,Salary)(Employee))=degree(Employee)∣ρnewEmployee(Name,Salary)(Employee)∣=∣Employee∣(EmpID,Name,DeptID,Salary)(EmpID,Name,Salary)
Join (⋈)
Join Type
Symbol / Expression
Purpose / Result
Equivalent 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