Skip to main content

3.4 Fundamental Relational Algebra Operators: Selection, Projection & Set Operations

πŸ“šModule 03: Relational Model & Relational AlgebraTopic 3.4⏱️9 min read
🎯High-Yield For:University Semester Exams β€’ Technical Interviews β€’ Query Optimization Foundations

πŸ’‘ Core Intuition​

🍳 The Everyday Analogy: Slicing and Combining Spreadsheets​

Imagine you have a printed physical spreadsheet of 10,000 university students:

  • Selection (Οƒ\sigma): 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 (Ο€\pi): Taking scissors and cutting away all columns except Name and Email. You are slicing vertically; duplicate identical cards are collapsed into one.
  • Cartesian Product (Γ—\times): Pairing every single student with every single available campus locker to explore all theoretical combinations.
  • Union (βˆͺ\cup) & 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:

  1. Every operator consumes one or two relations as input and produces exactly one relation as output (Closure Property).
  2. 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:

OperatorSymbolArityPrimary ActionDegree of OutputCardinality Range
Selectσ\sigmaUnaryHorizontal row filterDeg(R)\text{Deg}(R)$0 \le
ProjectΟ€\piUnaryVertical column filterNumber of chosen cols (kk)$1 \le
Unionβˆͺ\cupBinaryCombines tuples from two relationsDeg(R)\text{Deg}(R)max⁑(βˆ₯Rβˆ₯,βˆ₯Sβˆ₯)≀βˆ₯RβˆͺSβˆ₯≀βˆ₯Rβˆ₯+βˆ₯Sβˆ₯\max(\|R\|, \|S\|) \le \|R \cup S\| \le \|R\| + \|S\|
Set Differenceβˆ’-BinaryTuples in RR but not in SSDeg(R)\text{Deg}(R)0≀βˆ₯Rβˆ’Sβˆ₯≀βˆ₯Rβˆ₯0 \le \|R - S\| \le \|R\|
Cartesian ProductΓ—\timesBinaryCross pairs all tuplesDeg(R)+Deg(S)\text{Deg}(R) + \text{Deg}(S)βˆ₯Rβˆ₯Γ—βˆ₯Sβˆ₯\|R\| \times \|S\|
Renameρ\rhoUnaryRenames relation/attributesDeg(R)\text{Deg}(R)βˆ₯Rβˆ₯\|R\|

3. Detailed Operator Analysis & Mathematical Bounds​

A. Select Operation (Οƒ\sigma) β€” Horizontal Filtering​

Definition: Selects tuples that satisfy a given boolean predicate condition pp.

Οƒp(r)={t∣t∈r∧p(t)=true}\sigma_p(r) = \{ t \mid t \in r \land p(t) = \text{true} \}

Properties of Selection:

  • Degree: Degree(Οƒp(r))=Degree(r)\text{Degree}(\sigma_p(r)) = \text{Degree}(r) (Column count remains invariant).
  • Cardinality Bounds: 0β‰€βˆ£Οƒp(r)βˆ£β‰€βˆ£r∣0 \le |\sigma_p(r)| \le |r| Minimum cardinality is 00 (when no tuples satisfy pp); maximum cardinality is ∣r∣|r| (when every tuple satisfies pp).
  • Commutativity: Selection operations are strictly commutative: Οƒp1(Οƒp2(r))=Οƒp2(Οƒp1(r))\sigma_{p_1}(\sigma_{p_2}(r)) = \sigma_{p_2}(\sigma_{p_1}(r))
  • Predicate Connectives: Selection conditions can combine comparison operators (=,β‰ ,<,≀,>,β‰₯=, \neq, <, \le, >, \ge) using logical connectives ∧\land (AND), ∨\lor (OR), and Β¬\neg (NOT).

B. Project Operation (Ο€\pi) β€” Vertical Filtering​

Definition: Selects specified columns and discards all unlisted columns.

Ο€A1,A2,…,Ak(r)={t[A1,A2,…,Ak]∣t∈r}\pi_{A_1, A_2, \dots, A_k}(r) = \{ t[A_1, A_2, \dots, A_k] \mid t \in r \}

Properties of Projection:

  • Degree: The degree of the result equals the number of attributes specified in the projection list: Degree(Ο€A1,…,Ak(r))=k(1≀k≀n)\text{Degree}(\pi_{A_1, \dots, A_k}(r)) = k \quad (1 \le k \le n)
  • Duplicate Elimination: Because relations are mathematical sets, any identical duplicate rows produced after removing distinguishing columns are automatically eliminated.
  • Cardinality Bounds: 1β‰€βˆ£Ο€A1,…,Ak(r)βˆ£β‰€βˆ£r∣(assuming ∣r∣β‰₯1)1 \le |\pi_{A_1, \dots, A_k}(r)| \le |r| \quad (\text{assuming } |r| \ge 1) If the projection list contains a candidate key of rr, then βˆ£Ο€(r)∣=∣r∣|\pi(r)| = |r| (no duplicates can exist).
  • Non-Commutative: Cascaded projections cannot be swapped arbitrarily: Ο€A1(Ο€A1,A2(r))=Ο€A1(r)butΟ€A1,A2(Ο€A1(r))Β isΒ illegal\pi_{A_1}(\pi_{A_1, A_2}(r)) = \pi_{A_1}(r) \quad \text{but} \quad \pi_{A_1, A_2}(\pi_{A_1}(r)) \text{ is illegal}

C. Union Operation (βˆͺ\cup)​

Definition: Merges all tuples from relation RR and relation SS.

RβˆͺS={t∣t∈R∨t∈S}R \cup S = \{ t \mid t \in R \lor t \in S \}

Union Compatibility Requirements: Two relations RR and SS can participate in a Union (or Set Difference / Intersection) if and only if they are Union Compatible:

  1. Equal Degree: Degree(R)=Degree(S)=n\text{Degree}(R) = \text{Degree}(S) = n.
  2. Compatible Domains: For all 1≀i≀n1 \le i \le n, dom(Ai)=dom(Bi)\text{dom}(A_i) = \text{dom}(B_i) (corresponding attributes have matching data types).

Mathematical Bounds:

  • Degree: Degree(RβˆͺS)=Degree(R)=Degree(S)\text{Degree}(R \cup S) = \text{Degree}(R) = \text{Degree}(S).
  • Cardinality: max⁑(∣R∣,∣S∣)β‰€βˆ£RβˆͺSβˆ£β‰€βˆ£R∣+∣S∣\max(|R|, |S|) \le |R \cup S| \le |R| + |S|
    • Lower bound occurs when one relation is a complete subset of the other (RβŠ†SR \subseteq S or SβŠ†RS \subseteq R).
    • Upper bound occurs when RR and SS are mutually disjoint (R∩S=βˆ…R \cap S = \emptyset).

D. Set Difference Operation (βˆ’-)​

Definition: Finds tuples that belong to relation RR but do not belong to relation SS.

Rβˆ’S={t∣t∈R∧tβˆ‰S}R - S = \{ t \mid t \in R \land t \notin S \}

Mathematical Bounds:

  • Degree: Degree(Rβˆ’S)=Degree(R)=Degree(S)\text{Degree}(R - S) = \text{Degree}(R) = \text{Degree}(S) (requires union compatibility).
  • Cardinality: 0β‰€βˆ£Rβˆ’Sβˆ£β‰€βˆ£R∣0 \le |R - S| \le |R|
    • Lower bound 00 occurs when RβŠ†SR \subseteq S.
    • Upper bound ∣R∣|R| occurs when R∩S=βˆ…R \cap S = \emptyset.
  • Non-Commutative: Rβˆ’Sβ‰ Sβˆ’RR - S \neq S - R.

E. Cartesian Product (Cross Product Γ—\times)​

Definition: Associates every tuple in relation R1R_1 with every tuple in relation R2R_2.

R1Γ—R2={t1∘t2∣t1∈R1∧t2∈R2}R_1 \times R_2 = \{ t_1 \circ t_2 \mid t_1 \in R_1 \land t_2 \in R_2 \}

Mathematical Bounds:

  • Degree: Degree(R1Γ—R2)=Degree(R1)+Degree(R2)\text{Degree}(R_1 \times R_2) = \text{Degree}(R_1) + \text{Degree}(R_2).
  • Cardinality: ∣R1Γ—R2∣=∣R1βˆ£Γ—βˆ£R2∣=mβ‹…n|R_1 \times R_2| = |R_1| \times |R_2| = m \cdot n

Step-by-Step Derivation Example​

Let R1(A,B)R_1(A, B) have 33 tuples, and R2(B,C)R_2(B, C) have 33 tuples:

Table R1R_1:

AB
1P
2Q
3R

Table R2R_2:

BC
QX
RY
SZ

Cartesian Product R1Γ—R2R_1 \times R_2 (3Γ—3=93 \times 3 = 9 tuples):

AR1.BR_1.BR2.BR_2.BC
1PQX
1PRY
1PSZ
2QQX
2QRY
2QSZ
3RQX
3RRY
3RSZ

F. Rename Operation (ρ\rho)​

Definition: Used to rename relation results and attribute names so expressions can be referenced cleanly in subsequent algebra operations.

ρX(B1,B2,…,Bn)(E)\rho_{X(B_1, B_2, \dots, B_n)}(E)

Where:

  • XX is the new relation name assigned to expression EE.
  • B1,B2,…,BnB_1, B_2, \dots, B_n 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 Γ—\times before filtering results in catastrophic performance.

  • Naive Query: ΟƒAge>25(RΓ—S)\sigma_{\text{Age} > 25}(R \times S). If RR has 1,000,0001,000,000 rows and SS has 1,0001,000 rows, computing RΓ—SR \times S creates 11 billion intermediate tuples, exhausting server RAM.
  • Optimized Algebra Rule (Pushdown): ΟƒAge>25(RΓ—S)≑(ΟƒAge>25(R))Γ—S\sigma_{\text{Age} > 25}(R \times S) \equiv (\sigma_{\text{Age} > 25}(R)) \times S
  • Engine Execution: The PostgreSQL query planner recognizes the algebraic equivalence and pushes the selection Οƒ\sigma down directly to the disk storage scan on table RR. If only 50,00050,000 rows satisfy Age>25\text{Age} > 25, the intermediate Cartesian product drops from 1,000,000,0001,000,000,000 rows down to 50,000,00050,000,000 rowsβ€”a 95% reduction in memory and CPU cycles.

🎯 Exam & Interview Pitfall Check​

Core Conceptual Questions

Question 1: Prove why the set intersection operator (∩\cap) 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 (R∩SR \cap S) can be completely derived using the fundamental Set Difference (βˆ’-) operator:

R∩S=Rβˆ’(Rβˆ’S)R \cap S = R - (R - S)

Alternatively, R∩SR \cap S can also be expressed using Union and Set Difference:

R∩S=(RβˆͺS)βˆ’((Rβˆ’S)βˆͺ(Sβˆ’R))R \cap S = (R \cup S) - ((R - S) \cup (S - R))

Because it can be synthesized entirely from fundamental operators, ∩\cap is formally classified as a derived operator.

Question 2: If relation RR has degree 44 and cardinality 1010, and relation SS has degree 33 and cardinality 55, compute the degree and cardinality of RΓ—SR \times S.
Answer:

  • Degree of RΓ—SR \times S: Deg(R)+Deg(S)=4+3=7\text{Deg}(R) + \text{Deg}(S) = 4 + 3 = \mathbf{7}.
  • Cardinality of RΓ—SR \times S: ∣Rβˆ£Γ—βˆ£S∣=10Γ—5=50|R| \times |S| = 10 \times 5 = \mathbf{50}.
Common Interview Traps

Trap 1: "Is Projection commutative: Ο€A(Ο€B(R))=Ο€B(Ο€A(R))\pi_{A}(\pi_{B}(R)) = \pi_{B}(\pi_{A}(R))?"
Answer: No. Projection is not commutative. In fact, if attribute BB is not present in attribute set AA, evaluating the outer projection Ο€B\pi_{B} after Ο€A\pi_{A} will fail immediately with an "attribute not found" error because the inner projection already discarded attribute BB.

Trap 2: "Can two relations of different degrees be combined with Union (βˆͺ\cup)?"
Answer: No. Union requires strict union compatibility: both relations must have the exact same number of attributes (Deg(R)=Deg(S)\text{Deg}(R) = \text{Deg}(S)) and corresponding columns must share compatible data types.


πŸ’¬

Discussion & Doubts