Type a branch, a subject or a topic — “round robin”, “paging”, “civil”.

Relational Algebra

Six operators, one expression tree — and the rewrite that makes a query fast instead of slow.

Skip to the animation

Relational algebra is a small set of operators that each take relations and return a relation — the formal language a declarative SQL query is compiled into and optimised as.

Six primitives

OperatorSymbolDoes
Selectionσkeeps rows matching a condition — filters horizontally
Projectionπkeeps named columns, removing duplicate rows
Cartesian product×every row of R paired with every row of S
Unionrows in either, for union-compatible relations
Set differencerows in R but not S
Renameρrenames a relation or its attributes

Everything else is shorthand. R ⋈ S is a product followed by a selection on the shared attributes and a projection to remove the duplicate column. Intersection is R − (R − S). Division, natural join and theta join are all definable from the six.

Every operator returns a relation. That closure property is what allows operators to be stacked into an expression, and is why the algebra is a language rather than a list of tricks.

The vocabulary

Functional dependency (X → Y)
Knowing X pins down Y. If two rows agree on X they must agree on Y. This is a statement about the world being modelled, not about the rows that happen to be in the table today.
Superkey and candidate key
A superkey determines every attribute in the relation. A candidate key is a superkey with nothing spare in it — remove any attribute and it stops working.
Prime attribute
One that appears in some candidate key. The normal forms are mostly rules about how non-prime attributes are allowed to depend on prime ones.
Partial dependency
A non-prime attribute determined by only part of a composite key. Forbidden by 2NF, and the direct cause of the update, insert and delete anomalies.
Transitive dependency
Key → X → Y where X is not a key. Forbidden by 3NF: the fact really belongs in a table keyed by X.
Lossless decomposition
Splitting a relation so that re-joining it gives back exactly the original rows — no more, no fewer. Guaranteed when the shared attribute is a key of one of the two pieces.

Where SQL and the algebra differ

  • Duplicates. A relation is a set, so π removes duplicates automatically. SQL's SELECT keeps them unless you write DISTINCT — SQL operates on multisets.
  • Nulls. The algebra has none. SQL's three-valued logic is an extension with no counterpart here.
  • Order. Neither has any. ORDER BY is applied after the algebra has finished.
  • Aggregation and grouping are additions to the algebra, not part of the classical six.

Why the optimiser needs it

SQL says what is wanted; the algebra says how, and equivalent expressions can differ enormously in cost. The optimiser's job is to generate equivalent forms and pick the cheapest, using rewrite rules that the algebra makes provably safe:

  • Push selections down. σ(R ⋈ S) becomes σ(R) ⋈ σ(S) where the conditions allow. Filtering before joining is the single most valuable rewrite there is.
  • Push projections down. Discard unneeded columns early so less data moves.
  • Reorder joins. Joins are associative and commutative, so the order is free to choose — and with n relations there are exponentially many orders, which is why join ordering is the hard part of query planning.

Filter-then-join and join-then-filter return identical answers. One may touch a thousand rows and the other ten million.

Advantages and disadvantages

Advantages

  • Small enough to reason about formally, which is what makes rewrites provably correct.
  • Every operator returns a relation, so expressions compose without restriction.
  • Gives the optimiser a space of equivalent plans to search.

Disadvantages

  • No aggregation, grouping, ordering or nulls in the classical form.
  • Set semantics disagree with SQL's multiset semantics in ways that matter in practice.
  • It is procedural — an expression fixes an order, which is exactly what SQL users wanted to avoid writing.

Watch it work

loading visualisation…

Check yourself

question 1 / 4

One question at a time. Pick an answer to see why it is right or wrong, then move on — there is no score to keep and nothing is saved.

Which is the single most valuable rewrite an optimiser can make?
σ (selection) and π (projection) differ how?
A natural join and a Cartesian product followed by a selection are related how?
Why is relational algebra called procedural, while SQL is called declarative?

0 / 4

4 still unanswered — the dots above jump straight to them.