Introduction

Tuple relational calculus (TRC) is a declarative query language: a query states the property that answer tuples must satisfy and says nothing about how to compute them. A TRC query has the form {t∣P(t)}\{ t \mid P(t) \}, "the set of tuples tt for which the formula PP is true", where PP is a formula of first-order logic over the database.

Relational algebra and safe TRC have exactly the same expressive power (Codd's theorem), so every algebra expression has a calculus equivalent and the other way round. The two basic tasks are to say what a given TRC expression returns, or which of several expressions expresses an English query. The points that need most care are the placement of quantifiers, writing ∧\wedge where ⇒\Rightarrow is needed under ∀\forall, and safety.

Prerequisites

Core concepts

Tuple variables range over tuples. A formula is built from atoms:

AtomMeaning
t∈Rt \in Rtt is a tuple of relation RR
t.A  θ  s.Bt.A \;\theta\; s.Bcomparison of two attribute values, θ∈{=,≠,<,≤,>,≥}\theta \in \{=, \neq, <, \leq, >, \geq\}
t.A  θ  ct.A \;\theta\; ccomparison with a constant

Formulas combine atoms with ¬\neg, ∧\wedge, ∨\vee, ⇒\Rightarrow and the quantifiers ∃s∈R (P)\exists s \in R\,(P) and ∀s∈R (P)\forall s \in R\,(P). The bounded forms abbreviate ∃s (s∈R∧P)\exists s\,(s \in R \wedge P) and ∀s (s∈R⇒P)\forall s\,(s \in R \Rightarrow P).

Free and bound variables. A variable is bound inside the scope of a quantifier on it and free otherwise. In {t∣P(t)}\{ t \mid P(t) \}, tt must be the only free variable of PP.

Equivalences used constantly.

  • ∀s (P)≡¬∃s (¬P)\forall s\,(P) \equiv \neg \exists s\,(\neg P) and ∃s (P)≡¬∀s (¬P)\exists s\,(P) \equiv \neg \forall s\,(\neg P).
  • P⇒Q≡¬P∨QP \Rightarrow Q \equiv \neg P \vee Q.
  • ∃\exists distributes over ∨\vee; ∀\forall distributes over ∧\wedge; neither distributes over the other connective.

Safety

The formula {t∣¬(t∈R)}\{ t \mid \neg (t \in R) \} describes every tuple that is not in RR, over all possible values, an infinite set. Here safe requires domain independence: for every database instance, evaluating the formula over any ambient domain containing its active domain gives the same answer, whose values lie in that active domain. The domain of the formula, dom(P)\text{dom}(P), comprises values in its mentioned relations plus its constants. An output-value bound alone is insufficient: unbounded quantifiers can change truth when the ambient domain grows. Syntactically range-restricted formulas bound output attributes to relation values/constants and bound quantified variables correctly, providing a sufficient safe fragment. Only safe expressions are allowed; safe TRC is exactly as expressive as relational algebra.

How it works: reading an expression

Read a TRC expression from the outside in:

  1. The target tuple tt and what it must belong to (its schema comes from the attributes used on it).
  2. Each ∃\exists is "there is some row such that"; each ∀s∈R\forall s \in R is "for every row of RR".
  3. Translate the inner comparison last.

Translation table.

Relational algebraTuple relational calculus
σA=5(R)\sigma_{A = 5}(R){t∣t∈R∧t.A=5}\{ t \mid t \in R \wedge t.A = 5 \}
πA(R)\pi_A(R){t∣∃r∈R (t.A=r.A)}\{ t \mid \exists r \in R\,(t.A = r.A) \}
R∪SR \cup S{t∣t∈R∨t∈S}\{ t \mid t \in R \vee t \in S \}
R−SR - S{t∣t∈R∧¬(t∈S)}\{ t \mid t \in R \wedge \neg (t \in S) \}
R⋈R.B=S.BSR \bowtie_{R.B = S.B} S projected on A,CA, C{t∣∃r∈R ∃s∈S (r.B=s.B∧t.A=r.A∧t.C=s.C)}\{ t \mid \exists r \in R\, \exists s \in S\,(r.B = s.B \wedge t.A = r.A \wedge t.C = s.C) \}
R(X,Y)÷S(Y)R(X, Y) \div S(Y){t∣∃r∈R (t.X=r.X)∧∀s∈S ∃r2∈R (r2.X=t.X∧r2.Y=s.Y)}\{ t \mid \exists r \in R\,(t.X = r.X) \wedge \forall s \in S\, \exists r_2 \in R\,(r_2.X = t.X \wedge r_2.Y = s.Y) \}

Algorithms and pseudocode

For the syntactically range-restricted fragment taught here, with all quantified variables bounded to relations and output attributes range-bound, evaluate by brute force over the domain of the formula. Do not apply this algorithm to arbitrary formulas merely because their current output lies in the active domain:

Text
EVAL-TRC({t | P(t)})
  D <- dom(P)                          // values in mentioned relations plus constants
  answer <- empty set
  for each tuple t over D with the target attributes
      if P(t) is true, evaluating
           exists s in R (Q) as: some row s of R makes Q true
           forall s in R (Q) as: every row s of R makes Q true
      then add t to answer
  return answer

For this bounded fragment, time is exponential in the nesting depth of quantifiers (each quantifier loops over a relation), polynomial in the data for a fixed query.

Mathematical explanation

Why ∀\forall needs ⇒\Rightarrow. An unbounded ∀s (s∈S∧Q)\forall s\,(s \in S \wedge Q) asserts that every tuple ss whatever is in SS, which is false as soon as the domain has a tuple outside SS. The intended "every ss of SS satisfies QQ" is ∀s (s∈S⇒Q)\forall s\,(s \in S \Rightarrow Q), which is true for tuples outside SS vacuously. Dually, ∃s (s∈S⇒Q)\exists s\,(s \in S \Rightarrow Q) is almost always true (any ss outside SS satisfies it), so ∃\exists pairs with ∧\wedge.

Why safety matters. {t∣¬(t∈R)}\{ t \mid \neg (t \in R) \} over the domain of RR alone is the complement of RR within that domain; over any larger domain the answer grows. An expression whose answer depends on the domain chosen is not a well-defined query, and domain-independent safety rules it out. Even an active-domain-bounded output can be unsafe: for unary R={1}, the query {t in R | forall s (s in R)} returns {1} over domain {1} but nothing over {1,2}; the unbounded quantifier is the problem.

Why ∃\exists does not distribute over ∧\wedge. ∃x (P(x)∧Q(x))\exists x\,(P(x) \wedge Q(x)) needs one witness for both; ∃x P(x)∧∃x Q(x)\exists x\,P(x) \wedge \exists x\,Q(x) allows two different witnesses. The second is weaker.

Worked examples

All examples use Enrolled(sid, cid) = {(s1,c1),(s1,c2),(s1,c3),(s2,c1),(s2,c3),(s3,c2),(s3,c3),(s4,c1),(s4,c2),(s4,c3)}\{(s_1, c_1), (s_1, c_2), (s_1, c_3), (s_2, c_1), (s_2, c_3), (s_3, c_2), (s_3, c_3), (s_4, c_1), (s_4, c_2), (s_4, c_3)\} and Req(cid) = {c1,c3}\{c_1, c_3\} from Relational Algebra.

Example 1: selection and projection. {t∣∃e∈Enrolled (e.cid=c2∧t.sid=e.sid)}\{ t \mid \exists e \in \text{Enrolled}\,(e.cid = c_2 \wedge t.sid = e.sid) \} returns the students enrolled in c2c_2: {s1,s3,s4}\{s_1, s_3, s_4\}.

Example 2: division. {t∣∃e∈Enrolled (t.sid=e.sid)∧∀q∈Req ∃e2∈Enrolled (e2.sid=t.sid∧e2.cid=q.cid)}\{ t \mid \exists e \in \text{Enrolled}\,(t.sid = e.sid) \wedge \forall q \in \text{Req}\, \exists e_2 \in \text{Enrolled}\,(e_2.sid = t.sid \wedge e_2.cid = q.cid) \} returns the students enrolled in every required course: {s1,s2,s4}\{s_1, s_2, s_4\}, the same as Enrolled÷Req\text{Enrolled} \div \text{Req}.

Example 3: the ∧\wedge trap. Replace the bounded ∀q∈Req\forall q \in \text{Req} by ∀q (q∈Req∧…)\forall q\,(q \in \text{Req} \wedge \ldots) with qq ranging over the course values of the domain, {c1,c2,c3}\{c_1, c_2, c_3\}. The tuple with q.cid=c2q.cid = c_2 is not in Req, so the body is false for it and the answer is empty.

Example 4: negation. {t∣∃e∈Enrolled (t.sid=e.sid)∧¬∃e2∈Enrolled (e2.sid=t.sid∧e2.cid=c1)}\{ t \mid \exists e \in \text{Enrolled}\,(t.sid = e.sid) \wedge \neg \exists e_2 \in \text{Enrolled}\,(e_2.sid = t.sid \wedge e_2.cid = c_1) \} returns students not enrolled in c1c_1: {s3}\{s_3\}.

Example 5: an unsafe query. {t∣¬(t∈Enrolled)}\{t\mid\neg(t\in\text{Enrolled})\}. Use the same global untyped active domain as EVAL-TRC and Example 7: its 4 student and 3 course values give ∣D∣=7|D|=7, hence 72=497^2=49 two-field tuples, of which 10 belong to Enrolled. The active-domain complement contains 39 tuples, including ill-typed pairs; it is not just the two missing student-course enrolments. Adding one new global value gives 82−72=158^2-7^2=15 additional pairs. Over an unrestricted domain, the complement can be infinite: this illustrates why the query is unsafe.

Example 6: maximum. For R(A)={3,7,5}R(A) = \{3, 7, 5\}, {t∣t∈R∧¬∃s∈R (s.A>t.A)}\{ t \mid t \in R \wedge \neg \exists s \in R\,(s.A > t.A) \} returns {7}\{7\}. Changing >> to ≥\geq returns the empty set, because every tt has s=ts = t with s.A≥t.As.A \geq t.A.

Example 7: division by double negation, and the empty divisor. Rewrite ∀q∈Req P\forall q \in \text{Req}\, P as ¬∃q∈Req ¬P\neg \exists q \in \text{Req}\, \neg P:

{t∣∃e∈Enrolled (t.sid=e.sid)∧¬∃q∈Req ¬∃e2∈Enrolled (e2.sid=t.sid∧e2.cid=q.cid)}\{ t \mid \exists e \in \text{Enrolled}\,(t.sid = e.sid) \wedge \neg \exists q \in \text{Req}\, \neg \exists e_2 \in \text{Enrolled}\,(e_2.sid = t.sid \wedge e_2.cid = q.cid) \}

Read it from the inside: "there is no required course qq for which there is no enrolment of tt in qq".

  • With Req ={c1,c3}= \{c_1, c_3\}: s3s_3 fails (no enrolment in c1c_1); the answer is {s1,s2,s4}\{s_1, s_2, s_4\}, the same as Example 2 and the algebra division.
  • With Req empty: ∃q∈Req\exists q \in \text{Req} is false for every tt, so the body is true and every enrolled student qualifies: {s1,s2,s3,s4}\{s_1, s_2, s_3, s_4\}. This matches R÷∅=πX(R)R \div \emptyset = \pi_X(R).
  • Drop the range condition ∃e∈Enrolled (t.sid=e.sid)\exists e \in \text{Enrolled}\,(t.sid = e.sid) and keep Req empty: now the body is true for every value tt of the domain, including the course names, so the answer is all 7 values s1,…,s4,c1,c2,c3s_1, \ldots, s_4, c_1, c_2, c_3. The expression is unsafe; the range condition is what keeps it safe. With Req ={c1,c3}= \{c_1, c_3\} the same expression happens to return {s1,s2,s4}\{s_1, s_2, s_4\}, which hides the problem.

The SQL form is the double NOT EXISTS of SQL Subqueries and Views, and it gives the same two answers on these tables.

Important properties and observations

  • Codd's theorem. Safe TRC, safe domain relational calculus and relational algebra (the six basic operators) are equally expressive.
  • TRC has set semantics like the algebra; SQL's duplicates and aggregates are outside it.
  • Every query needs a "range" for tt. Without a conjunct such as t∈Rt \in R or ∃r∈R (t.A=r.A)\exists r \in R\,(t.A = r.A), tt is unrestricted and the query is usually unsafe.
  • Negation turns quantifiers. ¬∀s∈S (P)≡∃s∈S (¬P)\neg \forall s \in S\,(P) \equiv \exists s \in S\,(\neg P): "not every required course" means "some required course missing".
  • Strict versus non-strict comparisons change answers at the boundary, as in Example 6.

Related material. Domain relational calculus uses variables over attribute values instead of tuples, for example {⟨x⟩∣∃y (⟨x,y⟩∈Enrolled∧y=c2)}\{ \langle x \rangle \mid \exists y\,(\langle x, y \rangle \in \text{Enrolled} \wedge y = c_2) \}; its safety condition and expressive power match TRC.

Common misconceptions.

  • "∀s (s∈S∧P)\forall s\,(s \in S \wedge P) means every ss in SS satisfies PP." It means every tuple is in SS and satisfies PP, almost always false.
  • "Any first-order formula is a valid query." Only safe ones.
  • "¬∃\neg \exists and ∃¬\exists \neg are the same." They are not: "no row satisfies" versus "some row fails".

GATE examination perspective

  • What does the expression return? Evaluate on the given instance, quantifier by quantifier.
  • Which expression means "for all"? Look for ∀\forall paired with ⇒\Rightarrow (or a bounded ∀\forall), or the double negation ¬∃…¬∃\neg \exists \ldots \neg \exists.
  • Equivalence to algebra. Match a TRC expression with the algebra expression (division, difference, join and projection).
  • Safety. Identify the unsafe expression among options: a negated membership with no positive range for tt.

Summary

Sources

  1. A. Silberschatz, H. F. Korth, S. Sudarshan, Database System Concepts, 7th ed., McGraw-Hill, 2019 (2020 copyright), Chapter 27 Formal Relational Query Languages (online chapter).
  2. R. Elmasri, S. B. Navathe, Fundamentals of Database Systems, 7th ed., Pearson, 8.6 The Tuple Relational Calculus; 8.7 The Domain Relational Calculus.

Next steps