Menu

Earn Premium with Referrals

Invite your friends and earn Premium rewards through our referral program.

See how it works and start inviting friends.

Relational Algebra
DBMS

Relational Algebra

Master the procedural query language used as the foundation for SQL: Select, Project, Union, and Joins.

Relational Algebra

Relational Algebra is a procedural query language. It tells the system how to get the data. Every SQL query you write is internally converted into Relational Algebra expressions for optimization.


Learning Objectives

After completing this chapter, you will be able to:

  • Understand Relational Algebra as the foundation of SQL.
  • Write expressions using Select (σ), Project (π), and Rename (ρ).
  • Understand Union, Set Difference, and Cartesian Product.
  • Differentiate between types of joins.
  • Convert SQL queries to Relational Algebra.
  • Answer interview questions on Relational Algebra.

Why Relational Algebra Matters

When you run a SQL query:

SELECT Name FROM Students WHERE Age > 20;

The DBMS internally converts it to:

π Name (σ Age > 20 (Students))

This algebraic form allows the query optimizer to find the most efficient execution plan. By understanding Relational Algebra, you understand how the database actually works.


Basic Operators

1. Select (σ) — Row Filtering

Selects rows that satisfy a condition.

Syntax:  σ condition (Relation)
SQL:     WHERE clause
σ Age > 20 (Students)      →  All students older than 20
σ Age > 20 AND City = 'Delhi' (Students)  →  Students in Delhi older than 20
Student_IDNameAgeCity
2Priya22Delhi
3Amit23Mumbai

Select reduces rows (horizontal filtering). σ is the Greek letter Sigma.

2. Project (π) — Column Filtering

Selects specific columns (attributes).

Syntax:  π column1, column2 (Relation)
SQL:     SELECT column1, column2
π Name, Age (Students)  →  Only Name and Age columns
NameAge
Rahul22
Priya23
Amit21

Project reduces columns (vertical filtering). π is the Greek letter Pi.

3. Rename (ρ) — Renaming

Renames a relation or attribute.

Syntax:  ρ NewName (Relation)
         ρ NewName(attr1, attr2) (Relation)
SQL:     AS keyword
ρ S(Std_ID, Std_Name) (Students)  →  Renames table and columns

Set Operations

Relations must be union compatible:

  1. Same number of attributes
  2. Corresponding attributes have same data types

Union (∪)

All rows from both relations (duplicates removed).

Syntax:  R ∪ S
SQL:     SELECT * FROM R UNION SELECT * FROM S

Set Difference (−)

Rows in R but not in S.

Syntax:  R − S
SQL:     SELECT * FROM R EXCEPT SELECT * FROM S

Intersection (∩)

Rows in both R and S.

Syntax:  R ∩ S
SQL:     SELECT * FROM R INTERSECT SELECT * FROM S

Cartesian Product (×)

Every row of R combined with every row of S.

Syntax:  R × S

If R has 100 rows and S has 50 rows, R × S has 5000 rows.

Problem: Most combined rows are meaningless. You need a Join condition to filter.


Join Operations

1. Theta Join (⋈θ)

A Cartesian Product followed by a Select condition.

Syntax:  R ⋈θ S    where θ is any condition
Students ⋈ Students.Student_ID = Enrollments.Student_ID Enrollments

2. Natural Join (⋈)

Automatically joins on common attribute names. No duplicate columns in result.

Syntax:  R ⋈ S

If both Students and Enrollments have Student_ID, natural join matches on it automatically.

3. Outer Joins

Join TypeKeepsMissing values
Left Outer Join (⟕)All rows from left tableNULLs for right table
Right Outer Join (⟖)All rows from right tableNULLs for left table
Full Outer Join (⟗)All rows from both tablesNULLs where no match

Operators Summary Table

OperatorSymbolPurposeSQL Equivalent
SelectσFilter rows (condition)WHERE
ProjectπFilter columnsSELECT columns
RenameρRename table/columnAS
UnionCombine rows (unique)UNION
Set DifferenceRows in R not in SEXCEPT
IntersectionRows in bothINTERSECT
Cartesian Product×Every combinationCROSS JOIN
Natural JoinMatch on same column namesNATURAL JOIN
Theta Join⋈θMatch on conditionJOIN … ON
Division÷Rows matching ALL values(complex)

Example: Converting SQL to Relational Algebra

SQL:

SELECT Students.Name, Courses.Title
FROM Students
JOIN Enrollments ON Students.Student_ID = Enrollments.Student_ID
JOIN Courses ON Enrollments.Course_ID = Courses.Course_ID
WHERE Courses.Department = 'CS';

Relational Algebra:

π Name, Title (
    σ Department = 'CS' (Courses) ⋈ Enrollments ⋈ Students
)

This shows the optimizer can: (1) filter courses to CS first, (2) join with Enrollments, (3) join with Students, (4) project only needed columns. Pushing the selection early reduces intermediate result sizes.


Interview Deep Dive

Q: What is the fundamental difference between Select (σ) and Project (π)?

A: Select (σ) filters rows (horizontal filtering) based on a condition — it reduces the number of records. Project (π) filters columns (vertical filtering) — it reduces the number of attributes. Select produces a subset of rows; Project produces a subset of columns.

Q: What does Union Compatible mean?

A: Two relations are union compatible if: (1) They have the same number of attributes (columns). (2) Corresponding attributes have the same data types. You cannot UNION a Students table with a Products table if they have different structures.

Q: Why do we prefer Joins over Cartesian Products?

A: A Cartesian Product (×) of 100 rows × 100 rows results in 10,000 rows, most of which are garbage (unrelated combinations). A Join combines the Cartesian Product with a selection condition (theta join), giving only the meaningful linked data.

Q: What is the Division operator (÷) used for?

A: Division finds rows in one relation that are associated with ALL rows in another relation. Example: “Find students who have enrolled in ALL courses offered by the CS department.” It’s like a “for all” or “every” query. SQL implements it using double NOT EXISTS or COUNT comparisons.


Key Takeaways

  • Relational Algebra is a procedural language — it specifies HOW to get data (vs SQL which specifies WHAT to get).
  • σ (Select) filters rows; π (Project) filters columns.
  • Union, Intersection, Set Difference require union-compatible relations.
  • Cartesian Product × creates all row combinations; Join filters them.
  • Natural Join matches on common attribute names.
  • Outer Joins preserve unmatched rows with NULLs.
  • The query optimizer converts SQL to Relational Algebra to find the best execution plan.

My Private Notes

Notes are auto-saved locally to this device.