Neural Sync Active
10 - Relational Algebra
Registry Synced
10 - Relational Algebra
1624 words
8 min read
Reading compass
Now ยท ๐ฏ Learning Objectives
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,deptnโameโ(ฯ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} ฯcourseiโdโ(teachesFโall)โชฯcourseiโdโ(teachesSโpring)Courses taught in Fall OR Spring.
Difference: โ (or โ)
rโs={tโฃtโrย andย tโ/s} ฯcourseiโdโ(teachesFโall)โฯcourseiโdโ(teachesSโpring)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.IDโteachesNatural 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,courseiโdโ(takes) Let S=ฯcourseiโdโ(ฯ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โ(ฯdeptnโame=โฒMusicโฒโ(instructor))
Q3. Write a relational algebra expression to find course IDs taught in Fall 2017 but not in Spring 2018.
Answerฯcourseiโdโ(ฯsemester=โฒFallโฒโงyear=2017โ(teaches))โฯcourseiโdโ(ฯ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,courseiโdโ(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โ(ฯdeptnโame=โฒ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
- ฯdeptnโame=โฒ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