3.4 Fundamental Relational Algebra Operators: Selection, Projection & Set Operations
π‘ Core Intuitionβ
π³ The Everyday Analogy: Slicing and Combining Spreadsheetsβ
Imagine you have a printed physical spreadsheet of 10,000 university students:
- Selection (): Using a ruler and pen to cross out all rows except students enrolled in "Computer Science". You are slicing horizontally; the number of columns stays identical.
- Projection (): Taking scissors and cutting away all columns except
NameandEmail. You are slicing vertically; duplicate identical cards are collapsed into one. - Cartesian Product (): Pairing every single student with every single available campus locker to explore all theoretical combinations.
- Union () & Difference (): Merging two class rosters, or subtracting students who already graduated from the current semester list.
π» Bridging to Computer Scienceβ
Relational Algebra is a formal procedural query language that defines the theoretical foundation of relational database query processing. An algebra consists of operands (relations/tables) and operators that manipulate relations to produce a new, unnamed relation as the output.
π Core Deep-Dive & Conceptsβ
1. Procedural vs Non-Procedural Query Languagesβ
Database query languages fall into two distinct paradigms:
Relational Algebra is pure mathematics based on Set Theory. Therefore, in pure relational algebra:
- Every operator consumes one or two relations as input and produces exactly one relation as output (Closure Property).
- Duplicate rows are automatically eliminated from any intermediate or final result relation.
2. The Six Fundamental Operatorsβ
All relational queries can be expressed using a minimal set of six fundamental operators:
| Operator | Symbol | Arity | Primary Action | Degree of Output | Cardinality Range |
|---|---|---|---|---|---|
| Select | Unary | Horizontal row filter | $0 \le | ||
| Project | Unary | Vertical column filter | Number of chosen cols () | $1 \le | |
| Union | Binary | Combines tuples from two relations | |||
| Set Difference | Binary | Tuples in but not in | |||
| Cartesian Product | Binary | Cross pairs all tuples | |||
| Rename | Unary | Renames relation/attributes |
3. Detailed Operator Analysis & Mathematical Boundsβ
A. Select Operation () β Horizontal Filteringβ
Definition: Selects tuples that satisfy a given boolean predicate condition .
Properties of Selection:
- Degree: (Column count remains invariant).
- Cardinality Bounds: Minimum cardinality is (when no tuples satisfy ); maximum cardinality is (when every tuple satisfies ).
- Commutativity: Selection operations are strictly commutative:
- Predicate Connectives: Selection conditions can combine comparison operators () using logical connectives (AND), (OR), and (NOT).
B. Project Operation () β Vertical Filteringβ
Definition: Selects specified columns and discards all unlisted columns.
Properties of Projection:
- Degree: The degree of the result equals the number of attributes specified in the projection list:
- Duplicate Elimination: Because relations are mathematical sets, any identical duplicate rows produced after removing distinguishing columns are automatically eliminated.
- Cardinality Bounds: If the projection list contains a candidate key of , then (no duplicates can exist).
- Non-Commutative: Cascaded projections cannot be swapped arbitrarily:
C. Union Operation ()β
Definition: Merges all tuples from relation and relation .
Union Compatibility Requirements: Two relations and can participate in a Union (or Set Difference / Intersection) if and only if they are Union Compatible:
- Equal Degree: .
- Compatible Domains: For all , (corresponding attributes have matching data types).
Mathematical Bounds:
- Degree: .
- Cardinality:
- Lower bound occurs when one relation is a complete subset of the other ( or ).
- Upper bound occurs when and are mutually disjoint ().
D. Set Difference Operation ()β
Definition: Finds tuples that belong to relation but do not belong to relation .
Mathematical Bounds:
- Degree: (requires union compatibility).
- Cardinality:
- Lower bound occurs when .
- Upper bound occurs when .
- Non-Commutative: .
E. Cartesian Product (Cross Product )β
Definition: Associates every tuple in relation with every tuple in relation .
Mathematical Bounds:
- Degree: .
- Cardinality:
Step-by-Step Derivation Exampleβ
Let have tuples, and have tuples:
Table :
| A | B |
|---|---|
| 1 | P |
| 2 | Q |
| 3 | R |
Table :
| B | C |
|---|---|
| Q | X |
| R | Y |
| S | Z |
Cartesian Product ( tuples):
| A | C | ||
|---|---|---|---|
| 1 | P | Q | X |
| 1 | P | R | Y |
| 1 | P | S | Z |
| 2 | Q | Q | X |
| 2 | Q | R | Y |
| 2 | Q | S | Z |
| 3 | R | Q | X |
| 3 | R | R | Y |
| 3 | R | S | Z |
F. Rename Operation ()β
Definition: Used to rename relation results and attribute names so expressions can be referenced cleanly in subsequent algebra operations.
Where:
- is the new relation name assigned to expression .
- are the optional new attribute names assigned to the resulting columns.
π Architecture / Visual Blueprintβ
π In The Real World: Production Case Studyβ
Query Optimizer Rule: Predicate Pushdown in PostgreSQL & CockroachDBβ
In real database engines, evaluating a raw Cartesian product before filtering results in catastrophic performance.
- Naive Query: . If has rows and has rows, computing creates billion intermediate tuples, exhausting server RAM.
- Optimized Algebra Rule (Pushdown):
- Engine Execution: The PostgreSQL query planner recognizes the algebraic equivalence and pushes the selection down directly to the disk storage scan on table . If only rows satisfy , the intermediate Cartesian product drops from rows down to rowsβa 95% reduction in memory and CPU cycles.
π― Exam & Interview Pitfall Checkβ
Question 1: Prove why the set intersection operator () is a derived operator rather than a fundamental operator in relational algebra.
Answer:
An operator is fundamental if it cannot be expressed using any combination of other operators. Set intersection () can be completely derived using the fundamental Set Difference () operator:
Alternatively, can also be expressed using Union and Set Difference:
Because it can be synthesized entirely from fundamental operators, is formally classified as a derived operator.
Question 2: If relation has degree and cardinality , and relation has degree and cardinality , compute the degree and cardinality of .
Answer:
- Degree of : .
- Cardinality of : .
Trap 1: "Is Projection commutative: ?"
Answer: No. Projection is not commutative. In fact, if attribute is not present in attribute set , evaluating the outer projection after will fail immediately with an "attribute not found" error because the inner projection already discarded attribute .
Trap 2: "Can two relations of different degrees be combined with Union ()?"
Answer: No. Union requires strict union compatibility: both relations must have the exact same number of attributes () and corresponding columns must share compatible data types.
Never Study in Isolation. Join Binary Dose.
Connect with fellow engineers, clear conceptual doubts, and get 2 visual breakdowns & engineering mental models daily across our community channels.
Discord Community
Start at #welcome-and-rules, say hello in #general-chat, clear doubts, and collaborate with engineers worldwide.