CSE Roadmap

Common tier · about 3 hours · 5 parts

Part 4 of 5 · 2 min read

Domain relational calculus

Describing a result one attribute value at a time, instead of one whole tuple at a time.

Domain relational calculus

Think of describing a guest list not by whole guests at all, but by naming the exact first name and exact last name each qualifying guest must have.

In plain words: domain relational calculus describes a query using individual attribute values, not whole tuples. An expression looks like { ⟨x, y⟩ | condition on x and y }, naming each value position separately instead of naming one tuple variable that stands for the whole row.

A worked example. Every order's ID and date, for customer 107, as domain relational calculus.

The key lines.

{ ⟨i, d⟩ | ⟨i, 107, d⟩ ∈ orders }
SELECT order_id, placed_on FROM orders WHERE customer_id = 107;

Domain relational calculus

Domain relational calculus expresses a query using individual attribute values as separate variables, rather than one variable standing for a whole tuple.

Example: { ⟨i, d⟩ | ⟨i, 107, d⟩ ∈ orders } names the order ID and date directly as i and d, instead of naming a tuple t and then referring to t.order_id and t.placed_on.

For the curious Why SQL feels closer to calculus

SQL's SELECT ... WHERE reads as a condition a result must satisfy, much like calculus, even though a real database executes it using algebra-like steps underneath. This is exactly the point of a declarative language: the person writing SQL describes what they want, and the query optimizer, covered in Level 6, decides the actual steps.

Quick notes

  • Relational algebra is procedural: a sequence of operators describing how to compute a result.
  • Relational calculus is declarative: a condition describing what the result must satisfy.
  • Tuple relational calculus names whole tuples satisfying a condition.
  • Domain relational calculus names individual attribute values satisfying a condition.
  • SQL reads closer to calculus, even though a database executes it using algebra-like steps internally.