∑GATE DA Atlas

Tuple Relational Calculus

Declarative queries in tuple relational calculus — tuple variables, atoms, connectives and quantifiers, free and bound variables, translating between calculus and algebra (selection, projection, join, difference, division), expressing "for all" with negated existentials, safety, domain relational calculus in brief, and Codd's theorem on expressive equivalence.

Section 58 practice questions

Declarative vs procedural

Relational algebra describes how to compute a result (a sequence of operations). Relational calculus describes what the result must satisfy, in first-order logic; SQL's WHERE EXISTS / NOT EXISTS style is calculus in disguise.

Tuple relational calculus (TRC)

A query has the form

{ t∣P(t) }or{ t.A1,…,t.Ak∣P(t) },\{\,t \mid P(t)\,\} \quad\text{or}\quad \{\,t.A_1, \dots, t.A_k \mid P(t)\,\},

the set of tuples tt (or chosen attributes of them) for which the formula PP is true.

Atoms:

  • t∈Rt \in R — tt is a tuple of relation RR;
  • t.A  θ  s.Bt.A \;\theta\; s.B or t.A  θ  ct.A \;\theta\; c with θ∈{=,≠,<,≤,>,≥}\theta \in \{=, \ne, <, \le, >, \ge\}.

Formulas combine atoms with ∧,∨,¬,⇒\wedge, \vee, \neg, \Rightarrow and the quantifiers ∃s (… )\exists s\,(\dots) and ∀s (… )\forall s\,(\dots).

A variable is bound if it is quantified, free otherwise; the result variables are the free ones.

Translating between algebra and calculus

AlgebraTuple relational calculus
σc(R)\sigma_{c}(R){t∣t∈R∧c(t)}\{t \mid t \in R \wedge c(t)\}
πA(R)\pi_{A}(R){t.A∣t∈R}\{t.A \mid t \in R\}
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.a=S.aSR \bowtie_{R.a = S.a} S{⟨r,s⟩∣r∈R∧s∈S∧r.a=s.a}\{\langle r, s\rangle \mid r \in R \wedge s \in S \wedge r.a = s.a\}
semi-join ("has a match"){t∣t∈R∧∃s (s∈S∧s.a=t.a)}\{t \mid t \in R \wedge \exists s\,(s \in S \wedge s.a = t.a)\}
anti-join ("has no match"){t∣t∈R∧¬∃s (s∈S∧s.a=t.a)}\{t \mid t \in R \wedge \neg\exists s\,(s \in S \wedge s.a = t.a)\}
R÷SR \div S{t.x∣t∈R∧∀s (s∈S⇒∃u (u∈R∧u.x=t.x∧u.y=s.y))}\{t.x \mid t \in R \wedge \forall s\,(s \in S \Rightarrow \exists u\,(u \in R \wedge u.x = t.x \wedge u.y = s.y))\}

Universal quantifiers

∀s (s∈S⇒P(s))\forall s\,(s \in S \Rightarrow P(s)) means "every tuple of SS satisfies PP"; it is equivalent to ¬∃s (s∈S∧¬P(s))\neg\exists s\,(s \in S \wedge \neg P(s)). "For all" questions (division, "enrolled in every course", "greater than all salaries") use it.

Safety

An expression is safe if its result is guaranteed to be finite and to contain only values appearing in the database (its active domain). Unsafe examples: {t∣¬(t∈R)}\{t \mid \neg(t \in R)\} (infinitely many tuples), {t∣t.a>5}\{t \mid t.a > 5\} (no relation restricts tt). Practical rule: every free variable must be bound to a relation by a positive atom t∈Rt \in R.

Codd's theorem: relational algebra, safe tuple relational calculus and safe domain relational calculus have exactly the same expressive power ("relational completeness"). None can express recursive queries such as transitive closure (ancestors of a node) — SQL adds WITH RECURSIVE for that.

Domain relational calculus (DRC) in brief

Variables range over values:

{ ⟨n,d⟩∣∃i ∃s (⟨i,n,d,s⟩∈Emp∧s>50000) }\{\,\langle n, d\rangle \mid \exists i\,\exists s\,(\langle i, n, d, s\rangle \in \text{Emp} \wedge s > 50000)\,\}

returns names and departments of employees earning over 50000. DRC underlies Query-by-Example interfaces.

Quick revision

Practice questions

GATE-style questions: 3 one-mark and 5 two-mark. Wrong MCQs cost ⅓ of their marks in the real exam; MSQ and NAT have no negative marking. Attempt each before opening the solution.

Q1MCQ1 mark

The TRC query {t∣t∈Student∧t.cgpa>8}\{t \mid t \in \text{Student} \wedge t.\textit{cgpa} > 8\} is equivalent to

Q2MCQ2 marks

With Student(sid,name)\text{Student}(\textit{sid}, \textit{name}) and Enrolled(sid,cid)\text{Enrolled}(\textit{sid}, \textit{cid}), what does this query return?

{ t.name∣t∈Student∧¬∃e (e∈Enrolled∧e.sid=t.sid) }\{\,t.\textit{name} \mid t \in \text{Student} \wedge \neg\exists e\,(e \in \text{Enrolled} \wedge e.\textit{sid} = t.\textit{sid})\,\}
Q3MSQ2 marks

Which statements about relational calculus are correct?

One or more options are correct.

Q4MCQ2 marks

With E(sid,cid)E(\textit{sid}, \textit{cid}) and C(cid)C(\textit{cid}), the TRC query

{ s.sid∣s∈E∧∀c (c∈C⇒∃e (e∈E∧e.sid=s.sid∧e.cid=c.cid))}\{\,s.\textit{sid} \mid s \in E \wedge \forall c\,\big(c \in C \Rightarrow \exists e\,(e \in E \wedge e.\textit{sid} = s.\textit{sid} \wedge e.\textit{cid} = c.\textit{cid})\big)\}

corresponds to which algebra expression?

Q5NAT2 marks

Emp(eid,dept,sal)={(1,A,50),(2,A,70),(3,B,60),(4,B,40)}\text{Emp}(\textit{eid}, \textit{dept}, \textit{sal}) = \{(1, A, 50), (2, A, 70), (3, B, 60), (4, B, 40)\}. The number of tuples returned by

{ t∣t∈Emp∧∃s (s∈Emp∧s.dept=t.dept∧s.sal>t.sal) }\{\,t \mid t \in \text{Emp} \wedge \exists s\,(s \in \text{Emp} \wedge s.\textit{dept} = t.\textit{dept} \wedge s.\textit{sal} > t.\textit{sal})\,\}

is ______.

Q6MCQ1 mark

In domain relational calculus, variables range over

Q7NAT2 marks

For Emp(eid,dept,sal)={(1,A,50),(2,A,70),(3,B,60),(4,B,40)}\text{Emp}(\textit{eid}, \textit{dept}, \textit{sal}) = \{(1, A, 50), (2, A, 70), (3, B, 60), (4, B, 40)\}, the number of tuples returned by

{ t∣t∈Emp∧∀s (s∈Emp⇒s.sal≤t.sal) }\{\,t \mid t \in \text{Emp} \wedge \forall s\,(s \in \text{Emp} \Rightarrow s.\textit{sal} \le t.\textit{sal})\,\}

is ______.

Q8MCQ1 mark

In the TRC formula { t∣t∈R∧∃s (s∈S∧s.a=t.a) }\{\,t \mid t \in R \wedge \exists s\,(s \in S \wedge s.a = t.a)\,\}, the variable ss is