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.
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)},
the set of tuples t (or chosen attributes of them) for which the formula P is true.
Atoms:
t∈R — t is a tuple of relation R;
t.Aθs.B or t.Aθc with θ∈{=,=,<,≤,>,≥}.
Formulas combine atoms with ∧,∨,¬,⇒ and the quantifiers ∃s(…) and ∀s(…).
A variable is bound if it is quantified, free otherwise; the result variables are the free ones.
Translating between algebra and calculus
Algebra
Tuple relational calculus
σc(R)
{t∣t∈R∧c(t)}
πA(R)
{t.A∣t∈R}
R∪S
{t∣t∈R∨t∈S}
R−S
{t∣t∈R∧¬(t∈S)}
R⋈R.a=S.aS
{⟨r,s⟩∣r∈R∧s∈S∧r.a=s.a}
semi-join ("has a match")
{t∣t∈R∧∃s(s∈S∧s.a=t.a)}
anti-join ("has no match")
{t∣t∈R∧¬∃s(s∈S∧s.a=t.a)}
R÷S
{t.x∣t∈R∧∀s(s∈S⇒∃u(u∈R∧u.x=t.x∧u.y=s.y))}
Universal quantifiers
∀s(s∈S⇒P(s)) means "every tuple of S satisfies P"; it is equivalent to ¬∃s(s∈S∧¬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)} (infinitely many tuples), {t∣t.a>5} (no relation restricts t). Practical rule: every free variable must be bound to a relation by a positive atom t∈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)}
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} is equivalent to
Q2MCQ2 marks
With Student(sid,name) and Enrolled(sid,cid), what does this query return?
{t.name∣t∈Student∧¬∃e(e∈Enrolled∧e.sid=t.sid)}
Q3MSQ2 marks
Which statements about relational calculus are correct?