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 ()
Selects tuples (rows) that satisfy a given predicate . Example:
Projection ()
Selects specific attributes (columns) from a relation. Example:
Set Operations
- Union (): Tuples in or (must be union-compatible).
- Difference (): Tuples in but not in .
- Intersection (): Tuples in both and .
🟡 2. Join Operations
Joins allow for the combination of related data from different relations.
Cartesian Product ()
Combines every tuple of with every tuple of .
Natural Join ()
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 (): Keeps all tuples from , filling with for missing matches in .
- Right Outer Join (): Keeps all tuples from .
- Full Outer Join (): 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
- Pushing Selections: Perform as early as possible to reduce the size of the relations being joined.
- Pushing Projections: Perform early to reduce the width of the tuples.
- Commutativity of Joins: . The optimizer chooses the order based on table statistics (cardinality).