10 - Relational Algebra
1624 words
8 min read
Visual companion
Python
Type and operator map
Python Week 1: the first filter for runtime behavior
View
Revision summary
What this note is really saying
Short form
# 10 - Relational Algebra ## 🎯 Learning Objectives After reading this topic, you will be able to: - Write relational algebra expressions for database queries - Use σ (select), π (project), ρ (rename), ∪ (union), − (difference), ∩ (intersection), × (Cartesian product) - Write join expressions (natural join, theta jo...

10 - Relational Algebra
🎯 Learning Objectives
After reading this topic, you will be able to:
- Write relational algebra expressions for database queries
- Use σ (select), π (project), ρ (rename), ∪ (union), − (difference), ∩ (intersection), × (Cartesian product)
- Write join expressions (natural join, theta join, outer join)
- Use the division operator (÷) for "all" queries
- Translate between relational algebra and SQL
📋 Prerequisites
- 03 - Relational Model — Relations, tuples, attributes, keys
- Set theory from BSMA1001 (Maths 1) — Sets, subsets, set operations
📖 Core Content
10.1 Intuition: The Mathematical Foundation of SQL
Relational algebra is the theoretical foundation of SQL. Every SQL query can be translated to a relational algebra expression. While SQL is declarative (WHAT), relational algebra is procedural (HOW).
Think of it as a recipe:
- Take the
instructorrelation - Select rows where
salary > 80000 - Project only
nameanddept_nameIn relational algebra: πname,deptname(σsalary>80000(instructor))
Why This Matters: Understanding relational algebra helps you:
- Write complex SQL queries correctly
- Understand what the query optimizer does
- Reason about query equivalence and optimization
10.2 The Basic Operations
Select: σ (Sigma)
Selects rows based on a condition:
Returns all instructors in Comp. Sci.
Returns Finance instructors earning > 80000.
| Symbol | Meaning |
|---|---|
| σc(R) | Select rows from R satisfying condition c |
| ∧ | AND |
| ∨ | OR |
| ¬ | NOT |
Project: π (Pi)
Selects columns (attributes):
Returns only the name and salary columns.
Returns department names (WITHOUT duplicates — unlike SQL's default behavior).
Rename: ρ (Rho)
Renames relations and/or attributes:
Returns the result of expression E with the name X and attributes renamed to A1,A2,…,An.
Renames
instructor to emp and its columns to ID, n, d, s.10.3 Set Operations
All set operations require union compatibility: same number of columns, same data types.
Union: ∪
r∪s={t∣t∈r or t∈s} πcourseid(teachesFall)∪πcourseid(teachesSpring)Courses taught in Fall OR Spring.
Difference: − (or ∖)
r−s={t∣t∈r and t∈/s} πcourseid(teachesFall)−πcourseid(teachesSpring)Courses taught in Fall but NOT in Spring.
Intersection: ∩
r∩s={t∣t∈r and t∈s}Can be expressed using difference: r∩s=r−(r−s)
Courses taught in BOTH Fall and Spring.
10.4 Cartesian Product: ×
Every combination of tuples from both relations:
If instructor has 12 tuples and teaches has 15 tuples:
This is equivalent to a JOIN. In practice, Cartesian product is almost always followed by a select condition.
10.5 Join Operations
Theta Join: ⋈θ
R⋈θS=σθ(R×S) instructor⋈instructor.ID=teaches.IDteachesNatural Join: ⋈
Joins on all common attributes (removes duplicate columns):
Joins on
ID (the common attribute). Equivalent to:Outer Join
Preserves non-matching rows (like SQL's LEFT/RIGHT/FULL OUTER JOIN):
- R⟕S — Left outer join
- R⟖S — Right outer join
- R⟗S — Full outer join
10.6 Division: ÷
The division operator answers "all" queries: "Find students who have taken ALL CS courses."
Where attributes(R) ⊇ attributes(S). Result has attributes(R) - attributes(S).
Intuition: For a tuple t to appear in the result, t must appear in R combined with every tuple in S.
(Diagram)
Example:
| student_id | course_id |
|---|---|
| 1 | CS101 |
| 1 | CS201 |
| 2 | CS101 |
| 2 | CS201 |
| 3 | CS101 |
S = {CS101, CS201}
R÷S = {1, 2} — students 1 and 2 took BOTH CS101 and CS201. Student 3 only took CS101.
10.7 Equivalence Rules
Important equivalences that the optimizer uses:
- Cascade selection: σθ1∧θ2(E)=σθ1(σθ2(E))
- Commuting selection: σθ1(σθ2(E))=σθ2(σθ1(E))
- Cascade projection: πL1(πL2(E))=πL1(E)
- Selection before join: σθ(E1×E2)=E1⋈θE2
- Commuting join: E1⋈θE2=E2⋈θE1
- Associating join: (E1⋈E2)⋈E3=E1⋈(E2⋈E3)
10.8 Worked Examples
Example 1: Simple query
Find the names of all instructors in the Physics department.
Example 2: Join query
Find the names of all instructors who teach a course.
Example 3: Division
Find the IDs of students who have taken ALL courses offered in Fall 2017.
Let R=πID,courseid(takes) Let S=πcourseid(σsemester=′Fall′∧year=2017(teaches))
Answer: R÷S
10.9 Relational Algebra Summary Table
| Operation | Symbol | SQL Equivalent | Purpose |
|---|---|---|---|
| Select | σ | WHERE | Filter rows |
| Project | π | SELECT | Choose columns |
| Rename | ρ | AS | Rename relation/attributes |
| Union | ∪ | UNION | Rows in either |
| Difference | − | EXCEPT | Rows in first but not second |
| Intersection | ∩ | INTERSECT | Rows in both |
| Cartesian Product | × | CROSS JOIN | All combinations |
| Natural Join | ⋈ | NATURAL JOIN | Join on common attributes |
| Theta Join | ⋈θ | JOIN ON condition | Join on arbitrary condition |
| Division | ÷ | NOT EXISTS + EXCEPT | "All" queries |
⚠️ Common Pitfalls
Pitfall 1: Confusing σ (select) with SQL SELECT
The Mistake: Thinking σ is the same as SQL's SELECT.
Why It's Wrong: σ selects rows (filtering), while SQL's SELECT picks columns. The relational algebra equivalent of SQL's SELECT is π (project).
| Concept | Relational Algebra | SQL |
|---|---|---|
| Row filtering | σ | WHERE |
| Column picking | π | SELECT |
| Renaming | ρ | AS |
Pitfall 2: Forgetting Set Operations Need Union Compatibility
The Mistake: Applying ∪, ∩, or - to relations with different schemas.
Why It's Wrong: Set operations require the same number of attributes with compatible domains. R(A, B) ∪ S(C, D, E) is invalid.
Fix: Use project to match schemas: πA,B(R)∪πC,D(S)
Pitfall 3: Thinking Natural Join is Always Equijoin
The Mistake: Assuming natural join always joins on foreign key = primary key.
Why It's Wrong: Natural join joins on ALL common attributes. If two tables accidentally share a column name (e.g., both have
name), the join uses that too.
Fix: Use theta join (⋈θ) to be explicit about join conditions.📝 Practice Questions
Q1. Write a relational algebra expression to find the names of all instructors who earn more than 90000.
Answerπname(σsalary>90000(instructor))Step 1: σsalary>90000(instructor) — filter rows with salary > 90000 Step 2: πname — keep only the name column
Q2. Express the following SQL query in relational algebra: SELECT name FROM instructor WHERE dept_name = 'Music';
Answerπname(σdeptname=′Music′(instructor))
Q3. Write a relational algebra expression to find course IDs taught in Fall 2017 but not in Spring 2018.
Answerπcourseid(σsemester=′Fall′∧year=2017(teaches))−πcourseid(σsemester=′Spring′∧year=2018(teaches))
Q4. What is the division operator? Give an intuitive explanation.
AnswerThe division operator (R÷S) answers "for ALL" queries. For a tuple t to be in the result, t (combined with other attributes) must have a match with every tuple in S.Example: "Find students who have taken ALL courses offered by the CS department." Use division:
- R = (student_id, course_id) — all enrollment records
- S = (course_id) — all CS courses
- R ÷ S = students who took every CS course
Q5. Show that intersection can be expressed using difference. R∩S=?
AnswerR∩S=R−(R−S)Proof:
- R−S = tuples in R but not S
- R−(R−S) = tuples in R minus those not in S = tuples in both R and S = R∩S
Q6. Write a relational algebra expression to find the names of instructors and the course IDs they teach.
Answerπname,courseid(instructor⋈teaches)This natural joins instructor and teaches on ID, then projects name and course_id.
Q7. What is the difference between natural join (⋈) and theta join (⋈θ)?
Answer
- Natural join (⋈): Automatically joins on ALL common attribute names. Duplicate columns are removed from the result.
- Theta join (⋈θ): Joins based on an explicit condition θ. Can use any comparison operator (=, <, >, etc.). May have duplicate columns.
Natural join is a special case of theta join where θ is equality on all common attributes.
Q8. Translate: πname(σdeptname=′CS′(instructor⋈σyear=2018(teaches))) to English.
Answer"Find the names of all instructors in the Comp. Sci. department who taught a course in the year 2018."Step by step:
- σyear=2018(teaches) — find teaching assignments from 2018
- instructor⋈ (step 1) — join with instructor to get instructor details
- σdeptname=′CS′ — keep only Comp. Sci. instructors
- πname — return their names
🔗 Cross-References
- Next Topic: 11 - Relational Calculus
- Previous Topic: 09 - SQL Functions & Triggers
- Related: 12 - ER Model (conceptual design before algebra)
- Related: 31 - Query Processing (relational algebra in optimization)
- Textbook: Silberschatz, Korth, Sudarshan — Chapter 2 (Relational Model), Chapter 6 (Formal Relational Query Languages) Join Discord Previous09 - SQL Functions & TriggersNext11 - Relational Calculus