The Relational Algebra and The Relational Calculus
Relational Algebra Overview
- Relational algebra is the basic set of operations for the relational model; it specifies retrieval requests.
- Each operation takes one or more relations as input and produces a new relation as output → the algebra is closed (all objects are relations).
- A sequence of operations is a relational algebra expression; its result is a relation (the query answer).
Groups of operations
| Group | Operations |
|---|---|
| Unary | SELECT |
| Set theory | UNION |
| Binary | JOIN (variants), DIVISION |
| Additional | OUTER JOIN, OUTER UNION, AGGREGATE functions |
A complete set is
Unary Operations
SELECT —
Selects a subset of tuples satisfying a Boolean condition (horizontal filter); schema unchanged.
- General:
. - Commutative:
. - Cascaded SELECTs collapse into one with AND of conditions.
- Result cardinality
.
σ DNO = 4 (EMPLOYEE) -- dept 4 employees
σ SALARY > 30000 (EMPLOYEE) -- salary filterPROJECT —
Keeps specified columns (vertical partition); discards the rest. Duplicate tuples are removed (result is a set).
- General:
. - Not commutative (generally); if
list2containslist1, then. - If the list includes a key, result cardinality
.
π LNAME, FNAME, SALARY (EMPLOYEE)RENAME —
Renames relation and/or attributes (needed for self-joins and multi-step queries).
ρ S (B1, B2, …, Bn) (R) -- rename relation to S and attributes to B1..Bn
ρ S (R) -- rename relation only
ρ (B1, B2, …, Bn) (R) -- rename attributes onlyShorthand assignment ← (e.g., DEP5_EMPS ← σ DNO=5 (EMPLOYEE)); can also rename inline: RESULT(F,M,L,…) ← ρ RESULT(F,M,L,…) (DEP5_EMPS).
Composing operations
Single expression vs. step-by-step (intermediate names):
π FNAME,LNAME,SALARY (σ DNO=5 (EMPLOYEE))
-- equivalently:
DEP5_EMPS ← σ DNO=5 (EMPLOYEE)
RESULT ← π FNAME,LNAME,SALARY (DEP5_EMPS)Set-Theoretic Operations (require type compatibility)
Two relations
| Op | Notation | Meaning |
|---|---|---|
| UNION | tuples in R or S or both (duplicates removed) | |
| INTERSECTION | tuples in both | |
| SET DIFFERENCE | tuples in R but not in S |
- UNION and INTERSECTION are commutative and associative.
- DIFFERENCE is not commutative (
).
DEP5_EMPS ← σ DNO=5 (EMPLOYEE)
RESULT1 ← π SSN (DEP5_EMPS)
RESULT2(SSN) ← π SUPERSSN (DEP5_EMPS)
RESULT ← RESULT1 ∪ RESULT2CARTESIAN PRODUCT —
-- not meaningful alone:
EMP_DEPENDENTS ← EMPNAMES × DEPENDENT
-- make it meaningful by selecting the matching pairs:
ACTUAL_DEPS ← σ SSN=ESSN (EMP_DEPENDENTS)Binary Operations: JOIN and DIVISION
JOIN —
Combines CARTESIAN PRODUCT + SELECT. General:
- Theta-join: join condition
is any Boolean expression (e.g., ). - Equijoin: condition uses only equality comparisons; leaves two identical join columns.
- Natural join
(or ): implicit equijoin on all attributes with the same name; one copy of each join attribute is kept. If names differ, apply first.
-- EQUIJOIN: manager of each department
DEPT_MGR ← DEPARTMENT ⋈ MGRSSN=SSN EMPLOYEE
-- NATURAL JOIN: one shared attribute DNUMBER
DEPT_LOCS ← DEPARTMENT ⋈ DEPT_LOCATIONS
-- general: Q ← R(A,B,C,D) ⋈ S(C,D,E) ⇒ Q(A,B,C,D,E)DIVISION —
Intuition: "find entities associated with all values in S." Used for "for all" queries (complement of the double-negation EXISTS pattern in SQL).
OUTER JOIN
In (natural/equi) join, unmatched tuples are discarded (information loss). Outer joins keep them, padding missing attributes with NULL:
- LEFT OUTER: keep all tuples of the left relation.
- RIGHT OUTER: keep all of the right.
- FULL OUTER: keep all of both, padding as needed.
OUTER UNION
For partially type-compatible relations STUDENT(Name,SSN,Dept,Advisor) OUTER UNION INSTRUCTOR(Name,SSN,Dept,Rank) → STUDENT_OR_INSTRUCTOR(Name,SSN,Dept,Advisor,Rank).
Aggregate Functions and Grouping —
Basic algebra cannot express statistics; add
, , , . COUNTcounts rows without removing duplicates.
Grouping: place the grouping attribute(s) to the left of
DNO ℱ COUNT SSN, AVERAGE Salary (EMPLOYEE)This groups employees by DNO and computes count + average salary per department. Rename to give result schema:
ρ R(Dno, No_of_emps, Avg_sal) ( DNO ℱ COUNT Ssn, AVERAGE Salary (EMPLOYEE) )Recursive closure
Not expressible in basic algebra without looping. Retrieves all supervisees of an employee at all levels (e.g., CEO → everyone). SQL:3 provides WITH RECURSIVE.
Query Trees
- An internal structure: leaf nodes = base relations; internal nodes = operations (
). - Gives a visual sense of complexity and intermediate results.
- Algebraic query optimization rewrites the tree into an equivalent (cheaper) tree (Ch. 15 / 19).
Worked Examples (procedural form)
Q1: name & address of employees in the 'Research' department.
RESEARCH_DEPT ← σ DNAME='Research' (DEPARTMENT)
RESEARCH_EMPS ← RESEARCH_DEPT ⋈ DNUMBER=DNO EMPLOYEE
RESULT ← π FNAME,LNAME,ADDRESS (RESEARCH_EMPS)
-- single expression:
π Fname,Lname,Address ( σ Dname='Research' (DEPARTMENT ⋈ Dnumber=Dno EMPLOYEE) )Q6: names of employees with no dependents.
ALL_EMPS ← π SSN (EMPLOYEE)
EMPS_WITH_DEPS(SSN) ← π ESSN (DEPENDENT)
EMPS_WITHOUT_DEPS ← ALL_EMPS - EMPS_WITH_DEPS
RESULT ← π Lname,Fname (EMPS_WITHOUT_DEPS ⋈ EMPLOYEE)Relational Calculus (declarative)
- Calculus specifies what to retrieve, not how (non-procedural); algebra is procedural (sequence of operations).
- Two forms, both equivalent to algebra: tuple relational calculus and domain relational calculus.
Tuple Relational Calculus
Query form:
- Quantifiers:
(exists), (for all). A variable is bound if quantified, free otherwise. Only free variables appear left of |. - Q (salary > 50000):
- With existential quantifier (Research employees):
- "For all" (works on all projects controlled by dept 5) uses
with exclusions: (Exclude tuples not in PROJECT, then those not in dept 5, then require participation in all remaining projects.)
Domain Relational Calculus
Variables range over single domain values; an BDATE and ADDRESS domain variables are free.
QBE (Query-By-Example)
A domain-calculus-based, form-filling language: example elements (_value), P. (print), condition boxes, joins via shared example elements, .CNT/.MAX/.MIN/.AVG aggregates, .G grouping. Equivalent to SQL; available in IBM DB2 QMF, MS Access, Paradox.
Chapter Summary
- Algebra: unary (
), set ( — type compatibility required), binary ( theta/equi/natural, ), additional (outer join/union, aggregates with grouping, recursive closure). is complete. OUTER JOIN keeps unmatched tuples (NULL-padded). - Query trees represent plans; optimization rewrites trees.
- Calculus is declarative (tuple/domain); SQL derives from tuple calculus. "For all" needs
(calculus) or double-negation/ DIVISION(algebra/SQL).