skip to content

questions

4

What is relational calculus, and how does it differ from relational algebra as a way of expressing a query?

level: juniorimportance: should knowfreq 32%

answer

  1. algebra = operators, ordered, a recipe
  2. calculus = logic formula, set-builder, a property
  3. { t | P(t) } reads: tuples t such that P
  4. equivalence (safe expressions) = the licence for optimisers
  5. join in algebra becomes EXISTS in calculus

basics

~20 s

Relational calculus is a declarative query notation based on first-order logic: you write a formula describing which tuples belong in the result. Relational algebra is procedural: you compose operators that say how to compute the result step by step. Both express the same queries.

solid answer

~50 s

Codd defined two equivalent formalisms for querying relations. **Relational algebra** is *procedural*. You build a result by applying operators such as selection, projection, product, union and difference in a chosen order. An algebra expression is effectively an evaluation plan: it says what to do and in what sequence. **Relational calculus** is *declarative* and comes from first-order predicate logic. You write a set-builder expression such as `{ t | Employee(t) AND t.salary > 100000 }`, which reads "the set of tuples t such that this predicate holds". There is no order of operations, no intermediate results and no choice of join order, because none of that is part of the statement. The two are equally expressive: for every safe calculus expression there is an equivalent algebra expression and vice versa. That equivalence is what makes practical query languages possible. Users write something calculus-flavoured, describing the result, and the optimiser is free to pick any algebra expression that computes it, because the request never constrained the procedure.

code

text · 10 lines
text
query: names of employees in a department located in Berlin

tuple calculus:
  { t.name | Employee(t) AND
             EXISTS d ( Department(d) AND d.dept_id = t.dept_id
                        AND d.city = 'Berlin' ) }

algebra:
  project[name] ( select[city='Berlin' AND Employee.dept_id=Department.dept_id]
                    ( Employee x Department ) )

go deeper

for a junior

Give the two definitions and one contrasting example; the key phrase is describing the result versus prescribing the steps.

for a middle

State the equivalence explicitly and explain how it licenses query rewriting and join reordering.

for a senior

Connect the formalisms to real query processing: plan trees are algebra, user syntax is sugared calculus, and rewrites are algebraic identities.

for a principal

Frame the split as the classic separation of specification from implementation and note that the equivalence is a floor, since practical workloads need extensions beyond first-order power.

## Two ways to say what you want When Codd formalised the relational model he gave two query notations, and their relationship is the intellectual foundation of every declarative query language since. **Relational algebra** treats relations as values and defines operators over them: selection keeps tuples satisfying a condition, projection keeps chosen attributes, Cartesian product pairs every tuple of one relation with every tuple of another, and union, intersection, difference and renaming complete the core. A query is an expression built by composing these operators, and because composition is ordered, an algebra expression reads like a recipe: first pair these two relations, then keep the matching pairs, then keep these attributes. **Relational calculus** takes the opposite stance. It borrows first-order predicate logic and describes the result as a set comprehension: `{ t | Employee(t) AND t.salary > 100000 }` Read aloud: the set of all tuples t such that t is an Employee tuple and its salary exceeds 100000. The expression states a *property* that qualifying tuples satisfy. It contains no operators, no ordering and no intermediate relations. The language provides variables, the connectives AND, OR and NOT, and the quantifiers "there exists" and "for all", with which surprisingly complex conditions can be phrased. ## Procedural versus declarative, concretely Consider "names of employees who work in a department located in Berlin". In algebra you must decide a procedure: form the product of employees and departments, restrict it to pairs where the department identifiers match and the city is Berlin, then project the name. Note how much you had to commit to, including which relation is mentioned first. In calculus you write a condition: there exists a department tuple d such that d matches this employee's department and d's city is Berlin. There is no product, no ordering and no plan. The existential quantifier does the work that the join did, but it is a statement of fact rather than an instruction. ## Why the equivalence matters Codd's theorem states that relational algebra and safe relational calculus have the same expressive power. That single result is what lets a query language be user-facing and optimisable at the same time. - Because the user's statement is declarative, it does not fix an evaluation order. Any algebra expression computing the same set is a legal implementation. - Because an equivalent algebra expression always exists, the system can translate the declarative statement into operators it knows how to execute. - Because algebra expressions obey algebraic laws such as commutativity of joins and pushing selections downward, the system can rewrite one plan into a cheaper equivalent plan. That is the origin of the standard slogan that a declarative query says *what* you want and the engine decides *how*. It is not marketing; it is a consequence of two formalisms being provably interchangeable. ## Where the two notations live today Algebra survives as the internal language of query processing: plan trees are algebra expressions annotated with physical choices, and rewrite rules are algebraic identities. Calculus survives as the shape of user-facing query languages, whose select-from-where structure and existential subquery conditions read as sugared calculus. Domain calculus in particular inspired visual query-by-example interfaces, where the user fills values into a template of the relation rather than writing operators. ## Boundaries worth stating Two caveats keep the answer honest. First, the equivalence applies to **safe** calculus expressions only; unrestricted logical formulas can describe infinite results that no algebra expression can produce. Second, this equivalence characterises a *floor* of expressive power. Both formalisms are first-order and neither can express aggregation, counting or transitive closure, which is why practical languages extend beyond relational completeness rather than stopping at it. ## Answering in an interview A compact strong answer is: calculus is declarative and logic-based, algebra is procedural and operator-based, they are equally expressive for safe expressions, and that equivalence is exactly what allows an optimiser to reorganise a user's query freely. Following it with one two-line example of the same query in both notations demonstrates understanding far faster than more definitions.

  • If the two formalisms are equally expressive, why does a database engine bother with the algebra at all?
    Because algebra expressions are executable and rewritable. Each operator corresponds to a physical implementation the engine can run, and algebraic identities such as pushing a selection below a join give the optimiser legal transformations to search over. The declarative statement is the user interface; the algebra is the internal representation where cost-based decisions are made.
  • Which notation is a select-from-where style query language closer to, and why does that matter?
    It is closer to calculus: the user names the relations of interest and states a condition that the result must satisfy, without specifying an evaluation order. That matters because it leaves the engine free to choose join order and access paths, so the same query can be executed differently as data volumes and available indexes change, with no rewrite by the user.

Algebra is a driving route: turn left, then right, then straight. Calculus is a destination description: the house with the red door opposite the park. Any route reaching that house is acceptable, which is exactly the freedom an optimiser exploits.

saying these in an interview costs you the question

  • Describing calculus as an alternative set of operators rather than a logical formalism
  • Claiming calculus is strictly more powerful than algebra, or the reverse, instead of equally powerful for safe expressions
  • Believing the calculus expression prescribes an evaluation order
  • Saying a declarative language is inherently slower because it does not specify the procedure
  • Assuming the equivalence covers aggregation or recursion

context

open as a page

Explain the difference between tuple relational calculus and domain relational calculus, and how existential and universal quantifiers are used in each.

level: middleimportance: should knowfreq 26%

basics

~20 s

In tuple calculus, variables range over whole tuples of a relation and you write t.attribute. In domain calculus, variables range over individual attribute values and a relation is matched positionally. Both use the existential quantifier for 'there is some matching tuple' and the universal quantifier for 'all'; they are equally expressive.

open as a page

What is a 'safe' expression in relational calculus, and what goes wrong with an unsafe one?

level: seniorimportance: should knowfreq 18%

basics

~20 s

A safe expression is one whose result contains only values already present in the database or in the query, so it is finite and independent of the underlying domains. Unsafe expressions, typically unrestricted negation such as 'all tuples not in R', describe infinite or domain-dependent results that no engine can compute.

open as a page

What does it mean to call a query language 'relationally complete', and which queries still fall outside that bar?

level: principalimportance: nice to knowfreq 16%

basics

~20 s

Relationally complete means the language can express every query expressible in relational algebra, equivalently in safe relational calculus, which is Codd's theorem. It is a floor, not a ceiling: first-order power excludes counting and aggregation, and transitive closure such as reachability, so real languages add extensions.

open as a page