Section 9 · 8 min read
Relational algebra and tuple calculus
The relational algebra operators including joins and division, how to count output tuples, and how to read and check safety of tuple relational calculus queries.
On this page
The relational model in one paragraph
A relation is a set of tuples over a fixed list of attributes (its schema). Because it is a set, there are no duplicate tuples and no order. Relational algebra is a procedural language: you combine operators to describe how to compute an answer. Tuple relational calculus (TRC) is declarative: you describe the tuples you want with a logical formula. Both have the same expressive power for safe queries. GATE asks you to evaluate expressions on small tables, count output tuples, and pick the expression or formula that matches an English query.
Always assume set semantics in relational algebra unless told otherwise. Projection removes duplicates. SQL does not, which is a frequent source of wrong answers.
Basic operators
| Operator | Notation | Result |
|---|---|---|
| Selection | Tuples of R satisfying condition c | |
| Projection | Only listed attributes, duplicates removed | |
| Union | Tuples in R or S (union-compatible) | |
| Difference | Tuples in R not in S | |
| Cartesian product | Every pairing, tuples | |
| Rename | Same tuples, new names |
These six are a complete set. Everything else can be rewritten using them:
- Natural join: product, select equality on common attributes, project away the duplicate columns.
Union-compatible means the same number of attributes with matching domains, position by position.
Cardinality bounds
For and :
| Expression | Minimum | Maximum |
|---|---|---|
| 0 | ||
| 1 if | ||
| 0 | ||
| 0 |
The minimum of is : the n tuples of S can cancel at most n tuples of R, and the bound is reached when S's tuples are all in R (or S contains all of R).
If the join attribute is the primary key of S and a non-null foreign key in R referencing S, then exactly.
Joins
Theta join and equijoin
. An equijoin uses only equality conditions and keeps both copies of the compared attributes.
Natural join
equates all attributes with the same name and keeps one copy of each. Two edge cases:
- No common attributes: natural join becomes the Cartesian product.
- Identical schemas: natural join becomes intersection.
Outer joins
Outer joins keep unmatched tuples and pad the missing side with NULL.
- Left outer join R ⟕ S: all of R.
- Right outer join R ⟖ S: all of S.
- Full outer join R ⟗ S: both.
Counting rule: size of R ⟕ S = size of + (number of R tuples with no match), and similarly for the others.
Semijoin
: the tuples of R that have at least one partner in S.
Division
Division answers "for all" questions. If and , then is the set of A values that are paired in R with every B value in S.
Read it in three steps: build every pair an A value should have, subtract the pairs that exist to find what is missing, then remove A values that miss anything. If S is empty, every A in qualifies.
Tuple relational calculus
A TRC query has the form : all tuples for which formula is true. Atoms are , and . Formulas combine atoms with , , , , and .
Useful identities:
A "for all" query in TRC usually looks like , which is the calculus version of division.
Safety
A TRC expression is safe if it is guaranteed to produce a finite result whose values come from the relations and constants mentioned in the query (the domain of the expression). is unsafe: it asks for everything not in R. is safe because is tied to R. Relational algebra expressions are always safe, and safe TRC has the same power as relational algebra.
Domain relational calculus (DRC) is the same idea with variables ranging over attribute values instead of tuples.
Worked examples
Worked example 1: join counts
and :
| A | B |
|---|---|
| 1 | p |
| 2 | p |
| 3 | q |
| 4 | r |
| B | C |
|---|---|
| p | 10 |
| p | 20 |
| p | 30 |
| q | 40 |
| s | 50 |
- : A=1 and A=2 each match three p tuples (6), A=3 matches one q tuple (1). Total 7.
- R ⟕ S: add (4, r, NULL) for the unmatched R tuple. Total 8.
- R ⟖ S: add (NULL, s, 50). Total 8.
- R ⟗ S: 7 + 1 + 1 = 9.
- : {p, q}, 2 tuples, since projection removes duplicates.
Worked example 2: division step by step
= {(s1,p1), (s1,p2), (s1,p3), (s2,p1), (s2,p3), (s3,p2), (s4,p1), (s4,p2), (s4,p3), (s4,p4)} and = {p1, p2}. Find suppliers who supply all needed parts.
- = {s1, s2, s3, s4}.
- has 8 pairs.
- Subtract : the missing pairs are (s2, p2) and (s3, p1).
- of the missing pairs = {s2, s3}.
- {s1, s2, s3, s4} - {s2, s3} = {s1, s4}.
s4 supplies an extra part p4, which does not matter.
Worked example 3: highest value without aggregates
Relational algebra has no MAX in its basic form, so "the largest salary" is found by removing every value that is beaten by another:
The join keeps every salary that has a bigger one somewhere. Subtracting leaves only the maximum. Adding to the join condition gives the maximum within each department instead, and ties all survive.
Worked example 4: reading a TRC query
Relations , , .
For a student , the inner part says every course has a Takes tuple linking it to . So the query returns names of students who take all courses: the calculus form of followed by a join with Student for names. If Course is empty, the is vacuously true and every student qualifies, which matches the algebra definition.
Equivalence rules worth knowing
- and selections commute.
- .
- Selection distributes over , and .
- Projection distributes over but not over or .
- Join is commutative and associative (up to column order).
- .
- Push selections down toward base relations before joins: same result, smaller intermediates.
The projection-over-difference trap: with and , but .
Common traps
- Keeping duplicates after projection. Relational algebra projection returns a set.
- Treating a natural join with no common attributes as empty. It is a Cartesian product.
- Confusing "at least one" with "all". The first is a join or semijoin; the second needs division or a double negation.
- Writing division backwards: subtracting from R instead of R from it.
- Counting an outer join without adding the unmatched tuples, or adding unmatched tuples from the wrong side.
- Forgetting that and need union-compatible schemas.
- Calling a valid query. It is unsafe.
- Assuming projection distributes over difference.
Quick revision
- Complete set: .
- ; join = select on product.
- ; it answers "for all".
- ; .
- Outer join size = inner join size + unmatched tuples padded with NULL.
- Natural join with no shared attribute = product; with identical schemas = intersection.
- TRC: ; is ; safe means finite and drawn from the query's domain.
- Safe TRC, safe DRC and relational algebra are equally expressive.