Skip to content

Relational Algebra

🧩 Relational Algebra

Relational algebra is a procedural query language which takes instances of relations as input and yields instances of relations as output. It is the formal foundation upon which SQL and relational database engines are built.


🟢 1. Fundamental Operators

Selection (σ\sigma)

Selects tuples (rows) that satisfy a given predicate pp. σp(R)\sigma_{p}(R) Example: σsalary>50000(Employees)\sigma_{\text{salary} > 50000}(\text{Employees})

Projection (π\pi)

Selects specific attributes (columns) from a relation. πa1,a2,,an(R)\pi_{a_1, a_2, \dots, a_n}(R) Example: πname, department(Employees)\pi_{\text{name, department}}(\text{Employees})

Set Operations

  • Union (RSR \cup S): Tuples in RR or SS (must be union-compatible).
  • Difference (RSR - S): Tuples in RR but not in SS.
  • Intersection (RSR \cap S): Tuples in both RR and SS.

🟡 2. Join Operations

Joins allow for the combination of related data from different relations.

Cartesian Product (R×SR \times S)

Combines every tuple of RR with every tuple of SS.

Natural Join (RSR \Join S)

A Cartesian product followed by a selection on common attributes, then a projection to remove duplicate columns. This is the most common way to link tables via foreign keys.

Outer Joins

  • Left Outer Join (RLSR \Join^L S): Keeps all tuples from RR, filling with NULLNULL for missing matches in SS.
  • Right Outer Join (RRSR \Join^R S): Keeps all tuples from SS.
  • Full Outer Join (RFSR \Join^F S): Keeps all tuples from both relations.

🔴 3. Query Optimization

The real power of relational algebra lies in its Mathematical Identities, which allow a database engine to rewrite a query into a more efficient form.

Common Rewrite Rules

  1. Pushing Selections: Perform σ\sigma as early as possible to reduce the size of the relations being joined. σp(RS)(σp(R))S\sigma_p(R \Join S) \equiv (\sigma_p(R)) \Join S
  2. Pushing Projections: Perform π\pi early to reduce the width of the tuples.
  3. Commutativity of Joins: RSSRR \Join S \equiv S \Join R. The optimizer chooses the order based on table statistics (cardinality).