Lecture 03: Relational Algebra I & Fundamental Operators
NULL semantics, Three-Valued Logic, and the 6 fundamental relational operators
Lecture Source Slide Deck
Original Slides: Lecture 03 (99 slides) · Amit Kumar Dhar (IIT Bhilai)
Download lecture03.pdf
1. NULL: The Value That Isn't
In relational databases, NULL does not equal zero (0) and does not equal the empty string (''). It signifies:
- Unknown value (e.g., we know an instructor has a salary, but do not know the exact number yet).
- Missing value (e.g., phone number not provided during signup).
- Inapplicable value (e.g., apartment number for a single-family house).
Three-Valued Logic (3VL)
Any standard comparison with NULL (such as salary > 50000 or salary = NULL) evaluates to UNKNOWN rather than TRUE or FALSE.
| Condition | Logic Value |
|---|---|
5 = 5 | TRUE |
5 = 10 | FALSE |
5 = NULL | UNKNOWN |
NULL = NULL | UNKNOWN (Two unknowns cannot be proven equal!) |
Rule: To check for nullability, always use IS NULL or IS NOT NULL.
2. What is Relational Algebra?
Relational Algebra is a formal procedural query language consisting of a set of mathematical operations that take one or two relations as input and produce a new relation as output.
The Closure Property
Because every relational algebra operator takes relations as input and outputs a relation, operators can be nested and composed arbitrarily into query trees:
π_name(σ_(salary > 80000)(instructor))3. The Six Fundamental Operators
All relational queries can theoretically be expressed using these 6 fundamental operators:
1. Selection (σ_p)
Selects tuples that satisfy a given predicate p:
σ_p(r) = { t ∈ r | p(t) is true }- Input: Relation
r, Predicatepusing comparison operators (=,!=,<,<=,>,>=,AND,OR,NOT). - Example: Find all instructors in the 'Physics' department earning more than 75,000:
σ_(dept_name = 'Physics' AND salary > 75000)(instructor)2. Projection (π_(A1, A2, ..., Ak))
Extracts specified attributes and eliminates duplicate tuples (since the output is a mathematical set):
π_(A1, ..., Ak)(r)- Example: List all department names from the instructor table:
π_dept_name(instructor)(Note: Any duplicate department rows are automatically collapsed).
3. Rename (ρ)
Assigns a new name to a relation or its attributes to resolve naming conflicts:
ρ_S(r) or ρ_S(B1, B2, ..., Bn)(r)- Example: Rename relation
instructortofaculty:
ρ_faculty(instructor)4. Union (∪)
Combines all tuples from two relations into a single set:
r ∪ s = { t | t ∈ r OR t ∈ s }- Prerequisite (Union Compatibility):
- Relations
randsmust have the exact same arity (same number of attributes). - The domains of corresponding attributes must be compatible from left to right.
- Relations
- Example: All courses taught in Fall 2025 or Spring 2026:
π_course_id(σ_(sem='Fall' AND year=2025)(section)) ∪ π_course_id(σ_(sem='Spring' AND year=2026)(section))5. Set Difference (−)
Finds tuples that are present in relation r but not in relation s:
r - s = { t | t ∈ r AND t ∉ s }- Prerequisite: Relations
randsmust be union-compatible. - Example: Courses taught in Fall 2025 but NOT taught in Spring 2026:
π_course_id(σ_(sem='Fall' AND year=2025)(section)) - π_course_id(σ_(sem='Spring' AND year=2026)(section))6. Cartesian Product (×)
Pairs every tuple of relation r with every tuple of relation s:
r × s = { t ∘ q | t ∈ r AND q ∈ s }- If
rhasn1tuples andk1attributes, andshasn2tuples andk2attributes:- Output has
n1 * n2tuples. - Output has
k1 + k2attributes.
- Output has
4. Derived Operator: Set Intersection (∩)
Set intersection returns tuples that belong to both relations:
r ∩ s = { t | t ∈ r AND t ∈ s }Intersection can be rewritten using set difference:
r ∩ s = r - (r - s)5. Review Exercises & Conceptual Questions
Expression:
π_name(σ_(tot_cred > 100)(student))Answer:
A Cartesian product blindly pairs every row in table A with every row in table B (|A| * |B| combinations). In student × department, a CS student will be paired with Biology, Music, and History departments. A selection condition σ_(student.dept_name = department.dept_name) must be applied over the product to retain only semantically meaningful relationships.
Answer:
- They must have the same degree (same number of columns).
- The domain of the
i-th column of the first relation must match the domain of thei-th column of the second relation for alli.