[[README|← Back to Index]]
Relational Algebra is a procedural query language — you specify how to get the answer using a sequence of operations.
[!abstract] Key Concept Each operation takes one or more relations (tables) as input and produces a relation as output.
Think of them as LEGO blocks you can stack together!
Symbol: σ
Purpose: Keep rows that satisfy a condition
Type: Unary (operates on one table)
Notation: σcondition(R)
Example:
σ_{DNO=5}(EMPLOYEE)
Translation: "Employees in department 5"
SQL Equivalent:
SELECT *
FROM EMPLOYEE
WHERE DNO = 5;Symbol: π
Purpose: Keep specified columns, remove duplicates
Type: Unary
Notation: πA,B,C(R)
Example:
π_{FNAME, LNAME}(EMPLOYEE)
Translation: "First and last names of all employees"
SQL Equivalent:
SELECT DISTINCT FNAME, LNAME
FROM EMPLOYEE;[!important] Duplicate Removal Projection automatically removes duplicates (unlike
SELECTin SQL, which keeps them by default).
Symbol: ρ
Purpose: Rename a relation or its attributes
Type: Unary
Notation: ρS(A,B,C)(R)
Example:
ρ_{EMPLOYEE2(SSN, NAME)}(EMPLOYEE)
Translation: "Rename EMPLOYEE to EMPLOYEE2, and rename columns to SSN and NAME"
Why it matters: Essential for self-joins and when column names clash.
Symbol: ∪
Purpose: Return all rows from R or S (remove duplicates)
Type: Binary
Requirement: R and S must be union-compatible:
- Same number of columns
- Corresponding columns have the same data types
Example:
STUDENTS ∪ TEACHERS
SQL Equivalent:
SELECT * FROM STUDENTS
UNION
SELECT * FROM TEACHERS;Symbol: −
Purpose: Rows in R that are not in S
Type: Binary
Example:
ENROLLED − GRADUATED
Translation: "Students who enrolled but haven't graduated"
SQL Equivalent:
SELECT * FROM ENROLLED
EXCEPT
SELECT * FROM GRADUATED;[!warning] Not Commutative R − S ≠ S − R
Symbol: ×
Purpose: Combine every row of R with every row of S
Type: Binary
Example:
EMPLOYEE × DEPARTMENT
Result: If EMPLOYEE has 100 rows and DEPARTMENT has 5 rows → 500 rows
SQL Equivalent:
SELECT *
FROM EMPLOYEE, DEPARTMENT;[!tip] Product + Selection = Join You almost always follow a product with a selection to create a meaningful join.
Symbol: ⋈
Purpose: Combine rows from R and S where a condition is satisfied
Type: Binary
Combines product and selection:
Definition:
R ⋈_{A=B} S = σ_{A=B}(R × S)
Example:
EMPLOYEE ⋈_{DNO=DNUMBER} DEPARTMENT
SQL Equivalent:
SELECT *
FROM EMPLOYEE
JOIN DEPARTMENT ON EMPLOYEE.DNO = DEPARTMENT.DNUMBER;A theta join where the condition is equality (=).
- Automatically joins on same-named columns
- Removes one copy of the duplicate column
Example:
EMPLOYEE ⋈ DEPARTMENT
(Joins on any columns with the same name, e.g., DNO)
[!note] Name Mismatch If column names don't match, use ρ (rename) first to align them.
Symbol: ÷
Purpose: Find rows in R that are related to all rows in S
Type: Binary
Use Case: "Find employees who worked on all projects in Houston"
Example:
π_{ESSN}(WORKS_ON) ÷ π_{PNO}(σ_{PLOCATION='Houston'}(PROJECT))
SQL Equivalent (using NOT EXISTS):
SELECT ESSN
FROM EMPLOYEE E
WHERE NOT EXISTS (
SELECT PNO FROM PROJECT WHERE PLOCATION = 'Houston'
EXCEPT
SELECT PNO FROM WORKS_ON WHERE ESSN = E.SSN
);Notation: grouping_attrs𝓖agg_func(R)
Example: Average salary per department
_{DNO} 𝓖_{AVG(SALARY) → AvgSal}(EMPLOYEE)
SQL Equivalent:
SELECT DNO, AVG(SALARY) AS AvgSal
FROM EMPLOYEE
GROUP BY DNO;| Function | Meaning |
|---|---|
SUM |
Total |
AVG |
Average |
COUNT |
Count |
MIN |
Minimum |
MAX |
Maximum |
RA:
π_{A,B}(σ_{condition}(R))
SQL:
SELECT A, B
FROM R
WHERE condition;RA:
R ⋈_{R.a = S.b} S
SQL:
SELECT *
FROM R
JOIN S ON R.a = S.b;RA:
π_{FNAME,LNAME}(σ_{DNO=5 AND SALARY>50000}(EMPLOYEE))
SQL:
SELECT FNAME, LNAME
FROM EMPLOYEE
WHERE DNO = 5 AND SALARY > 50000;[!question] Find first and last names of employees earning over $90,000.
RA Answer
π_{FNAME,LNAME}(σ_{SALARY>90000}(EMPLOYEE))
SQL Answer
SELECT FNAME, LNAME
FROM EMPLOYEE
WHERE SALARY > 90000;[!question] List employee names with their department names.
RA Answer
π_{FNAME,LNAME,DNAME}(EMPLOYEE ⋈_{DNO=DNUMBER} DEPARTMENT)
SQL Answer
SELECT E.FNAME, E.LNAME, D.DNAME
FROM EMPLOYEE E
JOIN DEPARTMENT D ON E.DNO = D.DNUMBER;[!question] Find employees who worked on all projects controlled by department 5.
RA Answer
π_{ESSN}(WORKS_ON) ÷ π_{PNO}(σ_{DNUM=5}(PROJECT))
Then join back to EMPLOYEE to get names:
π_{FNAME,LNAME}(
EMPLOYEE ⋈_{SSN=ESSN} (π_{ESSN}(WORKS_ON) ÷ π_{PNO}(σ_{DNUM=5}(PROJECT)))
)
- σc1(σc2(R)) = σc2(σc1(R))
- R ∪ S = S ∪ R
- R ⋈ S = S ⋈ R
- R − S ≠ S − R
- (Usually) R ÷ S ≠ S ÷ R
- (R ∪ S) ∪ T = R ∪ (S ∪ T)
- (R ⋈ S) ⋈ T = R ⋈ (S ⋈ T)
[!tip] Push Operations Down
- Push σ (selection) down → Filter early to reduce data size
- Push π (projection) down → Keep only needed columns
- Replace × followed by σ with ⋈ → More efficient
Example:
Inefficient:
π_{FNAME}(σ_{DNO=5}(EMPLOYEE × DEPARTMENT))
Optimized:
π_{FNAME}(σ_{DNO=5}(EMPLOYEE) ⋈_{DNO=DNUMBER} DEPARTMENT)
- [[04-SQL-Fundamentals|SQL Fundamentals]]
- [[06-Relational-Calculus|Relational Calculus (TRC)]]
- [[11-Query-Processing|Query Optimization]]