Relational Algebra (Lecture 15)
Introduction to Relational Algebra
1. Introduction to Relational Algebra
Relational Algebra is a foundational mathematical framework for the relational database model. It provides a basic set of operations that allow a user to specify data retrieval requests (queries).
Key Concept: The result of every relational algebra operation is a new relation (a new table).
- You can take the result of one operation and use it as input for the next operation.
- A sequence of these operations forms a Relational Algebra Expression.
Why is Relational Algebra Important?
- Foundation of the Relational Model: It defines the rules for manipulating data.
- Query Optimization: Relational Database Management Systems (RDBMS) use Relational Algebra internally to determine the fastest way to execute a SQL query.
- SQL Standard: Some of its concepts are built directly into the SQL language we use today.
Groups of Relational Operations
The operations are divided into four main groups:
- Unary Operations: Work on a single relation.
- Set Theory Operations: Standard math set operations applied to relations.
- Binary Relational Operations: Work on two relations to combine them.
- Additional Operations: Extensions for advanced needs (aggregates, outer joins, recursion).
2. The COMPANY Database Schema (Example Data)
To understand every example in this lecture, we will use the COMPANY database. Below is the Entity-Relationship schema of the database followed by the sample data provided in the slides.
Schema Diagram (Referential Integrity):
Sample Data Tables
EMPLOYEE Table:
| Fname | Minit | Lname | Ssn | Bdate | Address | Sex | Salary | Super_ssn | Dno |
|---|---|---|---|---|---|---|---|---|---|
| John | B | Smith | 123456789 | 1965-01-09 | 731 Fondren, Houston, TX | M | 30000 | 333445555 | 5 |
| Franklin | T | Wong | 333445555 | 1955-12-08 | 638 Voss, Houston, TX | M | 40000 | 888665555 | 5 |
| Alicia | J | Zelaya | 999887777 | 1968-01-19 | 3321 Castle, Spring, TX | F | 25000 | 987654321 | 4 |
| Jennifer | S | Wallace | 987654321 | 1941-06-20 | 291 Berry, Bellaire, TX | F | 43000 | 888665555 | 4 |
| Ramesh | K | Narayan | 666884444 | 1962-09-15 | 975 Fire Oak, Humble, TX | M | 38000 | 333445555 | 5 |
| Joyce | A | English | 453453453 | 1972-07-31 | 5631 Rice, Houston, TX | F | 25000 | 333445555 | 5 |
| Ahmad | V | Jabbar | 987987987 | 1969-03-29 | 980 Dallas, Houston, TX | M | 25000 | 987654321 | 4 |
| James | E | Borg | 888665555 | 1937-11-10 | 450 Stone, Houston, TX | M | 55000 | NULL | 1 |
DEPARTMENT Table:
| Dname | Dnumber | Mgr_ssn | Mgr_start_date |
|---|---|---|---|
| Research | 5 | 333445555 | 1988-05-22 |
| Administration | 4 | 987654321 | 1995-01-01 |
| Headquarters | 1 | 888665555 | 1981-06-19 |
DEPT_LOCATIONS Table:
| Dnumber | Dlocation |
|---|---|
| 1 | Houston |
| 4 | Stafford |
| 5 | Bellaire |
| 5 | Sugarland |
| 5 | Houston |
WORKS_ON Table:
| Essn | Pno | Hours |
|---|---|---|
| 123456789 | 1 | 32.5 |
| 123456789 | 2 | 7.5 |
| 666884444 | 3 | 40.0 |
| 453453453 | 1 | 20.0 |
| 453453453 | 2 | 20.0 |
| 333445555 | 2 | 10.0 |
| 333445555 | 3 | 10.0 |
| 333445555 | 10 | 10.0 |
| 333445555 | 20 | 10.0 |
| 999887777 | 30 | 30.0 |
| 999887777 | 10 | 10.0 |
| 987987987 | 10 | 35.0 |
| 987987987 | 30 | 5.0 |
| 987654321 | 30 | 20.0 |
| 987654321 | 20 | 15.0 |
| 888665555 | 20 | NULL |
PROJECT Table:
| Pname | Pnumber | Plocation | Dnum |
|---|---|---|---|
| ProductX | 1 | Bellaire | 5 |
| ProductY | 2 | Sugarland | 5 |
| ProductZ | 3 | Houston | 5 |
| Computerization | 10 | Stafford | 4 |
| Reorganization | 20 | Houston | 1 |
| Newbenefits | 30 | Stafford | 4 |
DEPENDENT Table:
| Essn | Dependent_name | Sex | Bdate | Relationship |
|---|---|---|---|---|
| 333445555 | Alice | F | 1986-04-05 | Daughter |
| 333445555 | Theodore | M | 1983-10-25 | Son |
| 333445555 | Joy | F | 1958-05-03 | Spouse |
| 987654321 | Abner | M | 1942-02-28 | Spouse |
| 123456789 | Michael | M | 1988-01-04 | Son |
| 123456789 | Alice | F | 1988-12-30 | Daughter |
| 123456789 | Elizabeth | F | 1967-05-05 | Spouse |
3. Unary Relational Operations
Unary operations work on one single table (relation).
3.1 The SELECT Operation ()
- Symbol: (Sigma).
- Purpose: It acts as a horizontal filter. It selects a subset of rows (tuples) from a relation based on a specific condition. Rows that meet the condition are kept; rows that fail are discarded.
- General Formula:
Examples:
- Select employees who work in Department 4:
- Select employees whose salary is greater than $30,000:
- Select employees in Department 4 AND having a salary > 25,000:
SQL Equivalent:
In SQL, the SELECT operation is written in the WHERE clause.
SELECT * FROM EMPLOYEE WHERE Dno = 4 AND Salary > 25000;Properties of SELECT:
- Commutative: You can perform SELECTs in any order.
- Cascade Combination: A sequence of SELECTs is equivalent to a single SELECT with an
ANDcondition.
- Row Count: The number of rows in the result will always be less than or equal to the number of rows in the original relation .
3.2 The PROJECT Operation ()
- Symbol: (Pi).
- Purpose: It acts as a vertical filter. It selects a subset of columns (attributes) from a relation and discards the rest.
- General Formula:
Example: To list only the last name, first name, and salary of all employees:
Important Rule: The PROJECT operation automatically removes duplicate rows. Because the result of a projection must be a mathematical set, and sets do not allow duplicate elements.
SQL Equivalent:
In SQL, the PROJECT operation is written in the SELECT clause.
SELECT DISTINCT Gender, Salary FROM EMPLOYEE;Properties of PROJECT:
-
Not Commutative: You cannot swap the order of the attribute lists. only if contains all attributes in .
-
Tuple Count: The number of rows in the result is less than or equal to the number of rows in because duplicates are removed.
Example of Combined SELECT and PROJECT
Request: Find the first name, last name, and salary of employees who work in department 5.
Relational Expression:
(Note: The SELECT runs first, creating an intermediate table of department 5 employees. Then PROJECT keeps only the 3 requested columns.)
Result:
| Fname | Lname | Salary |
|---|---|---|
| John | Smith | 30000 |
| Franklin | Wong | 40000 |
| Ramesh | Narayan | 38000 |
| Joyce | English | 25000 |
3.3 The RENAME Operation ()
- Symbol: (Rho).
- Purpose: To give a new name to a relation, its columns, or both. This is incredibly useful for self-joins.
- General Formula (3 forms):
- — Renames relation to and renames the columns.
- — Renames the relation name only.
- — Renames the column names only.
SQL Equivalent:
SELECT E.Fname AS First_name, E.Lname AS Last_name
FROM EMPLOYEE E
WHERE E.Dno = 5;4. Relational Algebra Operations from Set Theory
These are standard mathematical set operations, but they require Union Compatibility.
Union Compatibility Rule: Two relations and are compatible if:
- They have the exact same number of attributes (columns).
- The domain (data type) of each column in matches the corresponding column in .
4.1 UNION ()
- Result: All tuples that are in , in , or in both. Duplicates are automatically removed.
- Example — Find the SSN of employees who work in Dept 5 OR supervise an employee in Dept 5:
Diagram of Result:
| RESULT1 (SSN) | RESULT2 (SSN) | RESULT (SSN) |
|---|---|---|
| 123456789 | 333445555 | 123456789 |
| 333445555 | 888665555 | 333445555 |
| 666884444 | 666884444 | |
| 453453453 | 453453453 | |
| 888665555 |
4.2 INTERSECTION ()
- Result: Only tuples that are in both and .
- Example:
This gives SSNs of people who both work in department 5 AND supervise someone in department 5.
4.3 SET DIFFERENCE ()
- Result: Tuples that are in but NOT in .
- Important: Set difference is NOT commutative. .
Visual Example of UNION, INTERSECTION, SET DIFFERENCE
| (a) STUDENT | (a) INSTRUCTOR | (b) UNION | (c) INTERSECTION | (d) STUDENT − INSTRUCTOR | (e) INSTRUCTOR − STUDENT |
|---|---|---|---|---|---|
| Susan, Yao | John, Smith | Susan, Yao | Susan, Yao | Johnny, Kohler | John, Smith |
| Ramesh, Shah | Ricardo, Browne | Ramesh, Shah | Ramesh, Shah | Barbara, Jones | Ricardo, Browne |
| Johnny, Kohler | Susan, Yao | Johnny, Kohler | Amy, Ford | Francis, Johnson | |
| Barbara, Jones | Francis, Johnson | Barbara, Jones | Jimmy, Wang | ||
| Amy, Ford | Ramesh, Shah | Amy, Ford | Ernest, Gilbert | ||
| Jimmy, Wang | Jimmy, Wang | ||||
| Ernest, Gilbert | Ernest, Gilbert | ||||
| John, Smith | |||||
| Ricardo, Browne | |||||
| Francis, Johnson |
4.4 CARTESIAN PRODUCT ()
- Symbol: (Cross Product).
- Purpose: Combines every tuple from with every tuple from .
- Mathematical Properties:
- If has columns and has columns, the result has columns.
- If has rows and has rows, the result has rows.
- and do NOT need to be union-compatible.
- By itself rarely meaningful — but becomes powerful when followed by SELECT. In fact: Join = Cartesian Product + Select.
Example:
5. Binary Relational Operations: JOIN
Because Cartesian Product + SELECT is such a common pattern, Relational Algebra provides a dedicated shortcut: The JOIN operation ().
5.1 The JOIN Operation ()
- General Formula:
- Purpose: Combine related tuples from two tables, discarding unrelated ones.
- Example (Theta-Join): Find the manager details for each department.
5.2 The EQUIJOIN
- The most common type of Join — uses only the equality (
=) operator. - Side Effect: The result includes the two matching columns twice (once from each table), holding identical values.
5.3 The NATURAL JOIN ()
- Symbol:
- Purpose: A cleaner EQUIJOIN that is automatic.
- Automatic Matching: Automatically joins on attributes with the exact same name in both relations.
- Removes Duplicates: Automatically removes the redundant duplicate column.
Example 1:
(Both have column DNUMBER, so it joins on DEPARTMENT.DNUMBER = DEPT_LOCATIONS.DNUMBER and shows DNUMBER only once.)
Example 2:
Result of PROJ_DEPT:
| Pname | Pnumber | Plocation | Dnum | Dname | Mgr_ssn | Mgr_start_date |
|---|---|---|---|---|---|---|
| ProductX | 1 | Bellaire | 5 | Research | 333445555 | 1988-05-22 |
| ProductY | 2 | Sugarland | 5 | Research | 333445555 | 1988-05-22 |
| Computerization | 10 | Stafford | 4 | Administration | 987654321 | 1995-01-01 |
6. The Complete Set of Relational Algebra Operations
The Relational Algebra is mathematically complete. Any complex operation can be expressed using only these 6 fundamental operations:
- SELECT ()
- PROJECT ()
- UNION ()
- SET DIFFERENCE ()
- RENAME ()
- CARTESIAN PRODUCT ()
Proof of Completeness:
7. The DIVISION Operation
- Symbol:
- Purpose: Used to answer queries like "Find the employee who works on ALL projects."
- How it works: Given where , the result is a relation where .
- The Logic: A tuple appears in the result only if appears in in combination with every single tuple in . This is the equivalent of a "FOR ALL" (Universal Quantifier).
8. Additional Relational Operations
8.1 Aggregate Functions and Grouping ()
Basic Relational Algebra cannot calculate summary statistics. The Aggregate Function operation solves this.
Common Aggregates: SUM, AVERAGE, MAXIMUM, MINIMUM, COUNT.
Formula with grouping:
Example 1 — Max salary from EMPLOYEE:
Example 2 — Per department: count employees and average salary:
Result:
| Dno | Count_ssn | Average_Salary |
|---|---|---|
| 5 | 4 | 33250 |
| 4 | 3 | 31000 |
| 1 | 1 | 55000 |
8.2 OUTER JOIN Operations
In a standard Join, rows with no match are dropped. Outer Joins keep them, filling missing values with NULL.
- LEFT OUTER JOIN: Keeps every tuple in . Missing attributes become
NULL. - RIGHT OUTER JOIN: Keeps every tuple in . Missing attributes become
NULL. - FULL OUTER JOIN: Keeps every tuple in both and .
Example of LEFT OUTER JOIN:
| Fname | Minit | Lname | Dname |
|---|---|---|---|
| John | B | Smith | NULL |
| Franklin | T | Wong | Research |
| Alicia | J | Zelaya | NULL |
| Jennifer | S | Wallace | Administration |
| Ramesh | K | Narayan | NULL |
| James | E | Borg | Headquarters |
(John Smith has NULL for Dname — he has no department, but the Left Outer Join keeps him in the result.)
8.3 OUTER UNION
Normal UNION requires perfectly type-compatible tables. OUTER UNION works on partially compatible relations:
- Common columns appear once.
- Unique columns from both relations are included.
- Missing values are padded with
NULL.
Example:
STUDENT(Name, SSN, Department, Advisor) OUTER UNION INSTRUCTOR(Name, SSN, Department, Rank) produces:
STUDENT_OR_INSTRUCTOR(Name, SSN, Department, Advisor, Rank)
8.4 RECURSIVE CLOSURE
Recursive operations cannot be expressed in basic Relational Algebra. They handle self-referential relationships like the Employee-Supervisor relationship.
Goal: Find all employees supervised by James Borg at all levels.
Step 1 — Find Borg's SSN:
Step 2 — Build a SUPERVISION table:
Step 3 — Level 1 (direct reports):
Step 4 — Level 2 (reports of direct reports):
Step 5 — All levels combined:
(In a real RDBMS this is done with WITH RECURSIVE SQL, but in basic Relational Algebra you manually repeat the join for each level.)
9. Query Trees
A Query Tree (Query Evaluation Tree / Execution Tree) is an internal data structure used by a DBMS to represent a query.
- Leaf Nodes: Represent input relations (tables).
- Internal Nodes: Represent relational algebra operations (, , , etc.).
Execution Order:
- Leaf nodes (raw tables) are loaded.
- Internal node operations execute when their child operands are ready.
- The result of each internal node replaces that node.
- Execution ends when the root node produces the final result.
Standard logical order (Bottom to Top):
- SELECT () — Filter rows early to reduce data size.
- JOIN () — Combine the filtered tables.
- PROJECT () — At the root, output only the requested columns.
10. Practice Examples
Q1 — Retrieve the name and address of all employees who work for the 'Research' department:
Q2 — Find names of employees who work on ALL projects controlled by department 5:
Q3 — List project numbers involving an employee with last name 'Smith' (as worker OR department manager):
Q6 — Retrieve names of employees who have NO dependents:
11. Summary of Chapter 6
- Relational Algebra is a mathematical language for manipulating relations.
- Unary Operations: SELECT ( — filter rows), PROJECT ( — filter columns), RENAME ( — rename relation/columns).
- Set Theory Operations: UNION (), INTERSECTION (), SET DIFFERENCE (), CARTESIAN PRODUCT (). All require Union Compatibility except Cartesian Product.
- Binary Relational Operations: JOIN (), EQUIJOIN, NATURAL JOIN () — combine related data from multiple tables.
- Other Operations: DIVISION (), AGGREGATE FUNCTIONS () with GROUPING, OUTER JOINS, OUTER UNION, RECURSIVE CLOSURE.
- Completeness: The 6 fundamental operations (SELECT, PROJECT, UNION, SET DIFFERENCE, RENAME, CARTESIAN PRODUCT) can express all other operations.
- Query Trees are internal diagrams the DBMS optimizer uses to plan the fastest execution path for a query.