Relational Algebra - Complete Reference

Relational algebra is a procedural query language that defines operations to manipulate relations (tables) in a relational database. It provides a mathematical foundation for database operations and serves as the theoretical basis for SQL.

1. Unary Operations

1.1 Selection (σ - Sigma)

Symbol: σcondition(R)

Purpose: Selects tuples (rows) that satisfy a given condition

Syntax: σcondition(Relation)

Conditions:

  • Uses comparison operators: =, ≠, <, >, ≤, ≥
  • Logical operators: ∧ (AND), ∨ (OR), ¬ (NOT)
  • Can combine multiple conditions

Example Tables:

Original Employee Table:

EIDNameAgeDeptSalary
101John28CS55000
102Jane24IT45000
103Bob35CS65000
104Alice22HR40000

Selection Examples:

  1. σage > 25(Employee) - Select employees older than 25:

    EIDNameAgeDeptSalary
    101John28CS55000
    103Bob35CS65000
  2. σdept = ‘CS’ ∧ salary > 50000(Employee) - Select CS employees with salary > 50000:

    EIDNameAgeDeptSalary
    101John28CS55000
    103Bob35CS65000
  3. σname = ‘John’ ∨ name = ‘Jane’(Employee) - Select employees named John or Jane:

    EIDNameAgeDeptSalary
    101John28CS55000
    102Jane24IT45000

1.2 Projection (Π - Pi)

Symbol: Πattributes(R)

Purpose: Selects specific columns from a relation and eliminates duplicates

Syntax: ΠA₁, A₂, …, Aₙ(Relation)

Conditions:

  • Selected attributes must exist in the relation
  • Automatically removes duplicate tuples
  • Order of attributes in result follows the order specified

Projection Examples:

Original Employee Table:

EIDNameAgeDeptSalary
101John28CS55000
102Jane24IT45000
103Bob35CS65000
104Alice22HR40000
  1. Πname, age(Employee) - Project name and age columns:

    NameAge
    John28
    Jane24
    Bob35
    Alice22
  2. Πdept, salary(Employee) - Project department and salary:

    DeptSalary
    CS55000
    IT45000
    CS65000
    HR40000
  3. Πdept(Employee) - Project only department (duplicates removed):

    Dept
    CS
    IT
    HR

1.3 Rename (ρ - Rho)

Symbol: ρnew_name/old_name(R) or ρS(A₁, A₂, …, Aₙ)(R)

Purpose: Renames relations and/or attributes

Syntax Options: ρnew_attr/old_attr(R) # Rename attribute ρS(R) # Rename relation to S ρS(A₁, A₂, …, Aₙ)(R) # Rename relation and all attributes

Rename Examples:

Original Employee Table:

EIDNameAgeDept
101John28CS
102Jane24IT
  1. ρemp_name/name(Employee) - Rename ’name’ attribute to ’emp_name’:

    EIDemp_nameAgeDept
    101John28CS
    102Jane24IT
  2. ρStaff(Employee) - Rename Employee relation to Staff: Staff Table:

    EIDNameAgeDept
    101John28CS
    102Jane24IT
  3. ρE(eid, ename, age, department)(Employee) - Rename relation and all attributes: E Table:

    eidenameagedepartment
    101John28CS
    102Jane24IT

2. Combining Operations

OperationExampleExplanation
Selection + ProjectionΠname, salarydept = ‘CS’(Employee))First select CS employees, then project name and salary

3. Set Theory Operations

3.1 Union (∪)

Symbol: R ∪ S

Purpose: Combines tuples from two relations, eliminating duplicates

Conditions (Union Compatibility):

  • Both relations must have the same number of attributes
  • Corresponding attributes must have compatible data types
  • Attribute names should be the same (or renamed to match)

Union Example:

Students Table:

NameAge
John20
Jane22
Bob21

Faculty Table:

NameAge
Dr. Smith45
Dr. Brown50
Jane22

Students ∪ Faculty (Union Result):

NameAge
John20
Jane22
Bob21
Dr. Smith45
Dr. Brown50

Note: Jane appears only once (duplicates eliminated)


3.2 Intersection (∩)

Symbol: R ∩ S

Purpose: Returns tuples common to both relations

Conditions (Union Compatibility):

  • Both relations must have the same number of attributes
  • Corresponding attributes must have compatible data types

Intersection Example:

Current Students Table:

NameYear
John2023
Jane2024
Bob2025

Alumni Table:

NameYear
Jane2024
Alice2022
Tom2023

Current Students ∩ Alumni (Intersection Result):

NameYear
Jane2024

Note: Only Jane with Year 2024 exists in both tables


3.3 Set Difference (−)

Symbol: R − S

Purpose: Returns tuples in R but not in S

Conditions (Union Compatibility):

  • Both relations must have the same number of attributes
  • Corresponding attributes must have compatible data types

Set Difference Example:

All Students Table:

NameID
John101
Jane102
Bob103
Alice104

Graduated Students Table:

NameID
John101
Alice104

All Students − Graduated Students (Difference Result):

NameID
Jane102
Bob103

Note: Students who have NOT graduated


3.4 Cartesian Product (×)

Symbol: R × S

Purpose: Combines every tuple from R with every tuple from S

Conditions:

  • When relations have common attribute names, use RelationName.attribute notation
  • If no common attributes, proceed normally

Cartesian Product Examples:

Case 1: No Common Attributes

Employee Table:

NameDept
JohnCS
JaneIT

Project Table:

PIDBudget
P110000
P215000

Employee × Project:

NameDeptPIDBudget
JohnCSP110000
JohnCSP215000
JaneITP110000
JaneITP215000

Case 2: Common Attributes (Name)

Student Table:

NameAge
John20
Jane22

Teacher Table:

NameSubject
JohnMath
AlicePhysics

Student × Teacher (using RelationName.attribute):

Student.NameAgeTeacher.NameSubject
John20JohnMath
John20AlicePhysics
Jane22JohnMath
Jane22AlicePhysics

Note: Common attribute ‘Name’ requires RelationName.attribute notation


4. Join Operations

4.1 Inner Joins

4.1.1 Natural Join (⋈)

Symbol: R ⋈ S

Purpose: Joins relations on common attributes with same names

Conditions:

  • Relations must have at least one attribute with the same name
  • Common attributes must have compatible data types
  • Automatically eliminates duplicate columns

Natural Join Example:

Employee Table:

EIDNameDID
1John10
2Jane20
3Bob10
4Alice30

Department Table:

DIDDName
10CS
20IT
40HR

Employee ⋈ Department (Natural Join Result):

EIDNameDIDDName
1John10CS
2Jane20IT
3Bob10CS

Note: Common attribute DID appears only once (combined/eliminated). Alice (DID=30) and HR (DID=40) not included as no match.


4.1.2 Theta Join (θ-Join)

Symbol: R ⋈θ S where θ is a condition

Purpose: Joins relations based on any comparison condition

Conditions:

  • Can use any comparison operator: =, ≠, <, >, ≤, ≥
  • Can combine multiple conditions with ∧, ∨
  • Does not eliminate duplicate columns (unlike natural join)
  • Uses RelationName.attribute when common attributes exist

Theta Join Examples:

Case 1: Different Attribute Names

Employee Table:

EIDSalary
150000
270000
365000

Manager Table:

MIDMaxSal
10160000
10280000

Employee ⋈Salary < MaxSal Manager:

EIDSalaryMIDMaxSal
15000010160000
15000010280000
27000010280000
36500010280000

Case 2: Common Attribute Names

Employee Table:

EIDNameSalary
1John50000
2Jane70000

Manager Table:

MIDNameSalary
101Bob60000
102Alice80000

Employee ⋈Employee.Salary < Manager.Salary Manager:

EIDEmployee.NameEmployee.SalaryMIDManager.NameManager.Salary
1John50000101Bob60000
1John50000102Alice80000
2Jane70000102Alice80000

Note: RelationName.attribute used for common attributes (Name, Salary). All columns retained.


4.1.3 Equi-Join

Symbol: R ⋈A = B S

Purpose: Special case of theta join using only equality conditions

Conditions:

  • Uses only equality (=) operator
  • Does not eliminate duplicate columns (unlike natural join)
  • Attributes being compared can have different names
  • Uses RelationName.attribute when common attributes exist

Equi-Join Examples:

Case 1: Different Attribute Names

Employee Table:

EIDDeptID
110
220
310

Department Table:

IDName
10CS
20IT
30HR

Employee ⋈DeptID = ID Department:

EIDDeptIDIDName
11010CS
22020IT
31010CS

Case 2: Common Attribute Names

Student Table:

IDNameAge
1John20
2Jane22

Course Table:

IDNameCredits
101Math3
102Physics4

Student ⋈Student.ID = Course.ID Course:

Student.IDStudent.NameAgeCourse.IDCourse.NameCredits
(No matches since IDs don’t overlap)

Better Example - Student ⋈Student.Name = Course.Name Course: Assuming Course has instructor names matching student names

Note: RelationName.attribute notation required for common attributes. Both duplicate columns retained.

Difference from Natural Join:

  • Equi-join keeps duplicate columns
  • Natural join eliminates duplicate columns
  • Equi-join can join on different attribute names

4.2 Outer Joins

Purpose: Include unmatched tuples from one or both relations, filling with NULLs

Important: When relations have common attributes (other than join attributes), use RelationName.attribute notation for all joins except Natural Join.

Sample Tables for All Examples:

Employee Table:

EIDNameDID
1John10
2Jane20
3Bob30

Department Table:

DIDDName
10CS
20IT
40HR

Outer Join Results Comparison:

Join TypeSymbolResult Explanation
Left OuterR ⟕ SAll from left + NULLs for unmatched
Right OuterR ⟖ SAll from right + NULLs for unmatched
Full OuterR ⟗ SAll from both + NULLs for unmatched

Results (Natural Join Based - Common DID Combined):

Employee ⟕ Department (Left)Employee ⟖ Department (Right)Employee ⟗ Department (Full)
EID | Name | DID | DNameEID | Name | DID | DNameEID | Name | DID | DName
1 | John | 10 | CS1 | John | 10 | CS1 | John | 10 | CS
2 | Jane | 20 | IT2 | Jane | 20 | IT2 | Jane | 20 | IT
3 | Bob | 30 | NULLNULL | NULL | 40 | HR3 | Bob | 30 | NULL
NULL | NULL | 40 | HR

Note: Based on Natural Join (common DID combined). For other outer joins, use RelationName.attribute notation.

SQL Equivalents: LEFT JOIN, RIGHT JOIN, FULL OUTER JOIN


5. Additional Relational Operations

OperationSymbol/SyntaxPurposeExample
AssignmentTemp ← ExpressionStore intermediate resultsTemp ← σ<sub>age > 25</sub>(Employee)
Generalized ProjectionΠF₁, F₂, …, Fₙ(R)Include computed fieldsΠ<sub>name, salary*0.1 as tax</sub>(Employee)

Features of Generalized Projection:

  • Arithmetic operations: +, -, ×, ÷
  • String operations: concatenation
  • Functions: mathematical, date functions

5.3 Aggregate Functions and Grouping

5.3.1 Basic Aggregation

Symbol: GF₁, F₂, …, Fₙ(R)

Aggregate Functions:

  • SUM: Total of numeric values
  • AVG: Average of numeric values
  • COUNT: Number of tuples
  • MAX: Maximum value
  • MIN: Minimum value

Aggregate Function Examples:

Employee Table:

EIDNameDeptSalary
1JohnCS55000
2JaneIT45000
3BobCS65000
4AliceIT50000
5TomHR40000

Basic Aggregation Examples:

  1. GCOUNT(*)(Employee) - Count all employees:

    COUNT
    5
  2. GAVG(salary)(Employee) - Average salary:

    AVG_Salary
    51000
  3. GMAX(salary), MIN(salary)(Employee) - Max and min salary:

    MAX_SalaryMIN_Salary
    6500040000

5.4 Division (÷)

Symbol: R ÷ S

Purpose: Finds tuples in R that are related to ALL tuples in S

Conditions:

  • Attributes of S must be a subset of attributes of R
  • Complex operation often used for “for all” queries

Division Example - “Find students enrolled in ALL courses”:

Enrollment Table (R):

SIDCID
S1C1
S1C2
S1C3
S2C1
S2C2
S3C1

AllCourses Table (S):

CID
C1
C2
C3

Enrollment ÷ AllCourses (Division Result):

SID
S1

Note: Only S1 is enrolled in ALL courses (C1, C2, C3). S2 is missing C3, S3 is missing C2 and C3.

Step-by-step Process:

  1. S1 has {C1, C2, C3} - contains all courses ✓
  2. S2 has {C1, C2} - missing C3 ✗
  3. S3 has {C1} - missing C2, C3 ✗

Generalized Projection

Generalized projection allows for more complex expressions in the projection operation, including arithmetic operations, string concatenation, and function applications. Symbol: ΠF₁, F₂, …, Fₙ(R) Purpose: Projects specific attributes and allows for computed fields

Table Example:

EIDNameSalary
1John55000
2Jane45000
3Bob65000

Example:

Πname, salary * 0.1(Employee)

NameSalary * 0.1
John5500
Jane4500
Bob6500

6. Database Manipulation Operations

Database manipulation operations in relational algebra are used to modify the contents of relations. These operations correspond to SQL’s INSERT, UPDATE, and DELETE operations.

6.1 Insertion (∪ with new tuples)

Purpose: Add new tuples to a relation

Syntax: R ← R ∪ {new tuples}

Conditions:

  • New tuples must have same schema as existing relation
  • Duplicate tuples are automatically eliminated (set property)

Insertion Examples:

Original Employee Table:

EIDNameDeptSalary
1JohnCS55000
2JaneIT45000

Insert Single Tuple:

Employee ← Employee ∪ {(3, 'Bob', 'HR', 50000)}

Result:

EIDNameDeptSalary
1JohnCS55000
2JaneIT45000
3BobHR50000

Insert Multiple Tuples:

Employee ← Employee ∪ {(4, 'Alice', 'CS', 60000), (5, 'Tom', 'IT', 48000)}

Insert from Another Relation:

Employee ← Employee ∪ ΠEID, Name, Dept, Salary(NewHires)

6.2 Deletion (− Difference)

Purpose: Remove tuples that satisfy certain conditions

Syntax: R ← R − σcondition(R)

Conditions:

  • Uses selection to identify tuples to delete
  • Remaining tuples form the new relation

Deletion Examples:

Original Employee Table:

EIDNameDeptSalary
1JohnCS55000
2JaneIT45000
3BobHR50000
4AliceCS60000

Delete by Condition:

Employee ← Employee − σDept = 'HR'(Employee)

Result:

EIDNameDeptSalary
1JohnCS55000
2JaneIT45000
4AliceCS60000

Delete Multiple Conditions:

Employee ← Employee − σSalary < 50000 ∨ Dept = 'IT'(Employee)

Delete All Tuples:

Employee ← Employee − Employee  (Results in empty relation)

6.3 Update (Combination of Delete and Insert)

Purpose: Modify attribute values of existing tuples

Approach: Update = Delete old tuples + Insert modified tuples

Syntax:

R ← (R − σcondition(R)) ∪ Πmodified_attributescondition(R))

Update Examples:

Original Employee Table:

EIDNameDeptSalary
1JohnCS55000
2JaneIT45000
3BobHR50000

Update Single Attribute: Increase salary by 10% for CS employees

Employee ← (Employee − σDept = 'CS'(Employee)) ∪ 
           ΠEID, Name, Dept, Salary*1.1Dept = 'CS'(Employee))

Result:

EIDNameDeptSalary
1JohnCS60500
2JaneIT45000
3BobHR50000

Update Multiple Attributes: Change Bob’s department to IT and increase salary

Employee ← (Employee − σName = 'Bob'(Employee)) ∪ 
           {(3, 'Bob', 'IT', 55000)}

Update with Complex Conditions: Give 5% raise to employees earning less than 50000

LowSalary ← σSalary < 50000(Employee)
Employee ← (Employee − LowSalary) ∪ 
           ΠEID, Name, Dept, Salary*1.05(LowSalary)

6.4 Transaction Operations

Purpose: Ensure database consistency during multiple operations

ACID Properties in Relational Algebra:

PropertyDescriptionRelational Algebra Implementation
AtomicityAll or nothingGroup operations using assignment
ConsistencyValid state to valid stateUse constraints in conditions
IsolationConcurrent transactions don’t interfereSequential operation execution
DurabilityChanges persistAssignment makes changes permanent

Transaction Example: Transfer employee from one department to another with salary adjustment

-- Step 1: Verify employee exists
Temp1 ← σEID = 123(Employee)

-- Step 2: Remove from current department
Employee ← Employee − Temp1

-- Step 3: Add to new department with updated info
Employee ← Employee ∪ {(123, 'John', 'NewDept', NewSalary)}

6.5 Bulk Operations

Purpose: Perform operations on multiple tuples efficiently

Bulk Insert:

Employee ← Employee ∪ σSalary > 60000(Contractors)

Bulk Update:

HighEarners ← σSalary > 70000(Employee)
Employee ← (Employee − HighEarners) ∪ 
           ΠEID, Name, Dept, Salary*0.95(HighEarners)

Bulk Delete:

Employee ← Employee − σDept ∈ {'HR', 'Admin'}(Employee)

6.6 Constraints and Validation

Purpose: Ensure data integrity during manipulation operations

Primary Key Constraint:

-- Before insertion, check for duplicates
NewEmployee ← {(NewEID, NewName, NewDept, NewSalary)}
Duplicate ← ΠEID(Employee) ∩ ΠEID(NewEmployee)

-- Insert only if no duplicate
Employee ← Employee ∪ (NewEmployee − (NewEmployee ⋈ Duplicate))

Foreign Key Constraint:

-- Ensure department exists before assigning employee
ValidDepts ← ΠDeptName(Department)
NewEmployee ← {(EID, Name, DeptName, Salary)}

-- Insert only if department is valid
Employee ← Employee ∪ (NewEmployee ⋈ ValidDepts)

Check Constraints:

-- Ensure salary is positive
ValidSalary ← σSalary > 0(NewEmployee)
Employee ← Employee ∪ ValidSalary

7. Database Manipulation vs Query Operations

AspectQuery OperationsManipulation Operations
PurposeRetrieve dataModify data
ResultNew relation (temporary)Modified relation (permanent)
Examplesσ, Π, ⋈, ∪, ∩Insert, Update, Delete
Side EffectsNo change to original dataChanges original data
ReversibilityAlways reversibleMay not be reversible
SQL EquivalentSELECTINSERT, UPDATE, DELETE

Combined Example - Employee Promotion System:

-- Query: Find eligible employees
Eligible ← σYears > 2 ∧ Performance = 'Excellent'(Employee)

-- Manipulation: Promote eligible employees
Promoted ← ΠEID, Name, Dept, Salary*1.15(Eligible)
Employee ← (Employee − Eligible) ∪ Promoted

-- Query: Verify promotion results
NewHighEarners ← σSalary > 70000(Employee)