Databases (DBMS)
Relational Algebra & Calculus
Relational algebra is the procedural core behind SQL: master the six basic operators, derive joins and division from them, and bound the size of every result.
GATE CSE relational algebra and calculus — selection, projection, set operations, Cartesian product, theta/natural/outer joins, division, tuple and domain relational calculus, safe expressions and result-size bounds, with sample questions, flashcards and drills.
Learning outcomes
- Apply select, project, rename, union, difference and Cartesian product correctly
- Evaluate theta, equi, natural, semi and outer joins and bound their result sizes
- Compute division and write "for all" queries
- Read tuple and domain relational calculus and recognise unsafe expressions
Basic operators and set operations
- Notation used here (ASCII form): sigma[c](R) = SELECTION, pi[A,B](R) = PROJECTION, rho = rename, U = union, - = difference, x = Cartesian product, |X| = join.
- SELECTION sigma[c](R) keeps tuples satisfying condition c. It does not change the schema; the result has at most |R| tuples. Selections commute: sigma[c1](sigma[c2](R)) = sigma[c2](sigma[c1](R)) = sigma[c1 AND c2](R).
- PROJECTION pi[A1..Ak](R) keeps the listed columns and REMOVES DUPLICATE tuples (it is a set). Result has at most |R| tuples; the result of projecting onto a key has exactly |R| tuples.
- UNION, INTERSECTION and DIFFERENCE need UNION-COMPATIBLE relations (same degree and compatible domains). |R U S| lies between max(|R|, |S|) and |R| + |S|; |R n S| between 0 and min(|R|, |S|); |R - S| between 0 and |R|.
- CARTESIAN PRODUCT R x S: degree = deg(R) + deg(S), cardinality = |R| * |S| (all combinations).
- The SIX FUNDAMENTAL operators are selection, projection, union, set difference, Cartesian product and rename. Intersection, joins and division can all be derived from them: R n S = R - (R - S).
- Aggregation (G, e.g. count, sum, avg with grouping) is an EXTENDED operator not expressible with the six basic ones.
- RENAME rho[S(X, Y)](R) changes the relation and attribute names; it is needed for SELF-JOINS (R x rho[R2](R)) because a Cartesian product cannot have two attributes with the same name. Assignment T <- expr stores an intermediate result.
- PURE relational algebra works on SETS (no duplicates). SQL works on BAGS (multisets); a bag projection keeps duplicates, bag union adds multiplicities and bag difference subtracts them. Do not mix the two when counting tuples.
- The only operators that can REMOVE tuples are selection, difference and projection (by duplicate elimination); the only ones that can ADD tuples are union and Cartesian product (and joins, which are derived from product).
- ORDER OF OPERATIONS: pi after sigma is the same as sigma after pi only when the projected list contains every attribute used in the selection condition. So sigma[B=1](pi[A](R)) is ill-formed, but pi[A](sigma[B=1](R)) is fine.
Formulas to remember
-
|sigma[c](R)| <= |R|; |pi[A](R)| <= |R| -
R n S = R - (R - S) -
|R x S| = |R| * |S| -
max(|R|,|S|) <= |R U S| <= |R| + |S| (union-compatible)
| Operator | Result degree | Result cardinality (minimum .. maximum) | Needs compatible schemas? |
|---|---|---|---|
| sigma[c](R) | deg(R) | 0 .. |R| | no |
| pi[A1..Ak](R) | k | 1 .. |R| (0 if R is empty); exactly |R| if a key is kept | no |
| R U S | deg(R) | max(|R|, |S|) .. |R| + |S| | yes |
| R n S | deg(R) | 0 .. min(|R|, |S|) | yes |
| R - S | deg(R) | 0 .. |R| | yes |
| R x S | deg(R) + deg(S) | |R| * |S| | no |
Worked example
Problem. R(A, B) = {(1,2), (1,3), (2,2)}. Find |pi[A](R)| and |pi[B](R)|.
Solution. pi[A](R) = {1, 2} so 2 tuples; pi[B](R) = {2, 3} so 2 tuples (duplicates removed). Projection never produces more tuples than R and eliminates duplicates.
Joins and division
- THETA JOIN R |X|[c] S = sigma[c](R x S). EQUI-JOIN: theta join where c is only equality. NATURAL JOIN R |X| S: equi-join on all attributes with the same name, with the duplicate columns dropped. If no common attribute exists, natural join = Cartesian product.
- SIZE BOUNDS for natural join on common attribute A: 0 <= |R |X| S| <= |R| * |S|. If A is a KEY of S and a foreign key in R, then |R |X| S| <= |R| (exactly |R| when the foreign key is NOT NULL and every value is matched).
- OUTER JOINS keep unmatched tuples padded with NULL: LEFT outer keeps all of R, RIGHT outer keeps all of S, FULL outer keeps both. |left outer| >= |R|; |full outer| >= max(|R|, |S|).
- SEMI-JOIN R |X S = pi[attrs of R](R |X| S): tuples of R that have a match in S. ANTI-JOIN = tuples of R with no match.
- DIVISION R(A, B) / S(B) = { a | for EVERY b in S, (a, b) is in R }. The result schema is A. Used for "for all" queries such as "students who took ALL courses".
- Division expressed with basic operators: R / S = pi[A](R) - pi[A]( (pi[A](R) x S) - R ).
- Typical query shapes: "has at least one" = join/select/project; "has none" = set difference; "has all" = division; "has exactly/at most" = self-join plus difference.
- Associativity: natural join is commutative and associative; difference and division are not.
| Join type | Keeps | Unmatched padded with NULL? |
|---|---|---|
| Natural / inner / theta | matching pairs only | no |
| Left outer | all of R + matches | S columns |
| Right outer | all of S + matches | R columns |
| Full outer | all of R and all of S | both sides as needed |
| Semi-join | R tuples that have a match | no (only R columns) |
Worked example
Problem. R(A, B) = {(a1,b1), (a1,b2), (a2,b1), (a3,b1), (a3,b2)} and S(B) = {b1, b2}. Compute R / S.
Solution. a1 occurs with b1 and b2 (both elements of S) -> included. a2 occurs only with b1 -> excluded. a3 occurs with b1 and b2 -> included. R / S = {a1, a3}, 2 tuples.
Outer joins, join sizes and semi-joins in detail
- In a NATURAL JOIN of R(A,B) and S(B,C), a tuple that has no partner on B is DANGLING: it disappears from the inner join. OUTER JOINS keep dangling tuples and pad the missing side with NULL, so no information from the preserved side is lost.
- SIZE ARITHMETIC: if the join value v occurs m times in R and n times in S, the inner join contributes m * n tuples for v. Total inner join size = sum over common values of (count in R) * (count in S). Left outer = inner + dangling R tuples; right outer = inner + dangling S tuples; full outer = inner + dangling R + dangling S.
- MINIMUM and MAXIMUM of |R |X| S| on a common attribute B: minimum is 0 (disjoint B values); maximum is |R| * |S| (every tuple has the same B value). If B is a key of S: maximum is |R|. If B is a key of both: maximum is min(|R|, |S|). If B is a foreign key of R to S with NOT NULL: exactly |R|.
- SEMI-JOIN R |X S returns only the R-tuples that have a partner: |R |X S| <= |R|, and it never adds columns from S. ANTI-JOIN returns R tuples with NO partner: R - (R |X S). Left outer join = natural join U (anti-join padded with NULLs).
- Natural join is COMMUTATIVE and ASSOCIATIVE; theta join is not generally associative. Natural join of R with itself on all attributes gives R back (R |X| R = R) because tuples agree on every attribute.
- EQUIJOIN keeps both copies of the equal columns, natural join keeps one. Hence degree(natural join) = deg(R) + deg(S) - (number of common attributes).
- If R and S have NO common attribute, R |X| S = R x S. If R and S have the SAME schema, R |X| S = R n S.
Formulas to remember
-
inner join size = sum over common values v of (count_R(v) * count_S(v)) -
deg(R |X| S) = deg(R) + deg(S) - |common attributes| -
R left outer S = (R |X| S) U ((R - pi[attrs of R](R |X| S)) x {NULLs}) -
same schema: R |X| S = R n S; no common attribute: R |X| S = R x S
| Condition on join attribute B | min |R |X| S| | max |R |X| S| |
|---|---|---|
| B arbitrary in both | 0 | |R| * |S| |
| B is a key of S | 0 | |R| |
| B is a key of both R and S | 0 | min(|R|, |S|) |
| B is a NOT NULL foreign key of R to key B of S | |R| | |R| |
Worked example
Problem. R(A, B) = {(1,x), (2,y), (3,z)} and S(B, C) = {(x,p), (y,q), (w,r)}. Find the natural join, left outer join, right outer join and full outer join sizes, and the degree of the natural join.
Solution. Matching B values: x and y, so R |X| S = {(1,x,p), (2,y,q)} with 2 tuples and degree 2 + 2 - 1 = 3. Dangling tuples: (3,z) in R and (w,r) in S. Left outer = 2 + 1 = 3 tuples (adds (3,z,NULL)). Right outer = 2 + 1 = 3 tuples (adds (NULL,w,r)). Full outer = 2 + 1 + 1 = 4 tuples.
Query patterns: at least one, none, all, maximum
- "AT LEAST ONE" queries: join, select, project. Example: names of students enrolled in some course = pi[name](Student |X| Enrolled).
- "NONE / NEVER" queries need DIFFERENCE: (all candidates) - (those that qualify). Example: students with no enrollment = pi[sid](Student) - pi[sid](Enrolled).
- "ALL" queries use DIVISION or a double difference: students enrolled in every course = pi[sid, cid](Enrolled) / pi[cid](Course).
- "MAXIMUM / MINIMUM" queries use a SELF-JOIN and difference: highest salary = pi[sal](Emp) - pi[E1.sal](sigma[E1.sal < E2.sal](rho[E1](Emp) x rho[E2](Emp))). The tuples removed are those that have a larger salary above them.
- "AT LEAST TWO" queries need a self-join with an inequality, for example students in two different courses: sigma[E1.sid = E2.sid AND E1.cid <> E2.cid]. "EXACTLY ONE" = at least one minus at least two.
- A query written with NOT EXISTS in SQL corresponds to set difference in algebra, and a pair of nested NOT EXISTS corresponds to division. Recognise the word pattern first, then pick the operator.
- Check the result SCHEMA at the end: division keeps only the dividend attributes not in the divisor; difference and union keep the first operand’s schema.
Worked example
Problem. Emp(eid, sal) has salaries 10, 20, 30, 30, 40 in five tuples. Using the self-join/difference pattern, find the second-highest distinct salary. How many tuples does pi[sal](Emp) have, and what does T = pi[sal](Emp) - pi[E1.sal](sigma[E1.sal < E2.sal](rho[E1](Emp) x rho[E2](Emp))) contain?
Solution. pi[sal](Emp) has 4 tuples {10, 20, 30, 40}. In the self-join, E1.sal is kept when some E2.sal is strictly larger, i.e. for 10, 20 and 30, so the subtracted set is {10, 20, 30} and T = {40} (the maximum). For the second highest, repeat the same trick on Emp minus the tuples with salary 40: that leaves salaries {10, 20, 30}, whose maximum is 30. Second-highest distinct salary = 30.
Tuple and domain relational calculus
- TUPLE RELATIONAL CALCULUS (TRC): { t | P(t) } = set of tuples t that make the formula P true. Variables range over tuples; the formula uses attributes t.A, relation membership R(t), comparison, AND, OR, NOT and quantifiers EXISTS and FOR ALL.
- DOMAIN RELATIONAL CALCULUS (DRC): { <x1, ..., xn> | P(x1, ..., xn) } with variables ranging over attribute VALUES (domains), e.g. { <n> | EXISTS r (Student(r, n) AND r > 10) }.
- A calculus expression is SAFE if it produces a finite result using only values appearing in the database. Example of an UNSAFE expression: { t | NOT (t in R) } (infinite). Safe expressions must restrict every free variable to a relation.
- EXPRESSIVE POWER: relational algebra, safe tuple calculus and safe domain calculus are equivalent (Codd’s theorem). Relationally complete languages are at least as powerful as algebra; SQL is relationally complete.
- Calculus is declarative (what), algebra is procedural (how). Query optimisers translate SQL into an algebra tree and then rearrange it.
- Optimisation heuristics: push selections down, push projections down, combine a selection with a Cartesian product into a join, and join smaller relations first.
- Standard equivalence: for all x P(x) = NOT exists x NOT P(x); this is how division is written in calculus.
Formulas to remember
-
TRC: { t | R(t) AND t.A > 5 } = sigma[A > 5](R) -
TRC: { t.A | R(t) } = pi[A](R) -
FOR ALL x P(x) = NOT EXISTS x NOT P(x)
Worked example
Problem. Express "names of students who have enrolled in at least one course" in TRC and algebra, given Student(sid, name) and Enrolled(sid, cid).
Solution. TRC: { s.name | Student(s) AND EXISTS e (Enrolled(e) AND e.sid = s.sid) }. Algebra: pi[name](Student |X| Enrolled). Both return each qualifying name once.
Exam tips
- Projection removes duplicates; selection and projection never increase the tuple count.
- Natural join on a key/foreign-key pair never produces more tuples than the foreign-key side.
- Division = "for all": count the divisor’s tuples and check each candidate against all of them.
- Only six operators are fundamental: select, project, union, difference, product, rename. Aggregates are extra.
- { t | NOT R(t) } is the standard UNSAFE calculus example.
8 GATE-style MCQs for Relational Algebra & Calculus with full explanations. Attempt each question before revealing the answer.
R(A, B) has 100 tuples and S(B, C) has 50 tuples. B is the primary key of S and B in R is a foreign key (NOT NULL) referencing S. The number of tuples in R |X| S is:
Show answer & explanation
Answer: B
Each R tuple matches exactly one S tuple (key of S, valid non-NULL foreign key), so the natural join has exactly 100 tuples.
R(A, B) = {(a1,b1), (a1,b2), (a2,b1), (a3,b1), (a3,b2)} and S(B) = {b1, b2}. The number of tuples in R / S is:
Show answer & explanation
Answer: B
a1 and a3 are paired with both b1 and b2; a2 only with b1. So R / S = {a1, a3}.
Which of the following CANNOT be expressed using the six fundamental operators (select, project, union, difference, product, rename)?
Show answer & explanation
Answer: D
Intersection, natural join and division are derived. Aggregation with grouping is an extended operator outside the basic algebra.
R has 5 tuples and S has 3 tuples. No tuple of R matches any tuple of S on the join attribute. The number of tuples in R full outer join S is:
Show answer & explanation
Answer: D
No matches means every R tuple (5) and every S tuple (3) appears padded with NULLs: 5 + 3 = 8.
Which expression is equivalent to R n S?
Show answer & explanation
Answer: A
R - S removes the common tuples from R; subtracting that from R leaves exactly the tuples common to both.
Which tuple calculus expression is NOT safe?
Show answer & explanation
Answer: B
All tuples not in R form an infinite set; the other expressions restrict t to relations of the database.
Given R(A, B) = {(1,2), (1,3), (2,2)}, how many tuples are in pi[A](sigma[B = 2](R))?
Show answer & explanation
Answer: B
sigma[B=2] keeps (1,2) and (2,2); projecting A gives {1, 2}: 2 tuples.
Student(sid, name), Enrolled(sid, cid), Course(cid). Which expression gives the sids of students enrolled in ALL courses?
Show answer & explanation
Answer: A
Division of (sid, cid) pairs by the set of all cid values returns exactly the sids paired with every course.
Extra page-specific practice beyond the GATE samples. Attempt all 8, then reveal answers to check your reasoning.
R(A) has 6 tuples and S(B) has 4 tuples. The number of tuples in R x S and the degree of R x S are:
Show answer & explanation
Answer: B) 24 tuples, degree 2
Tuples multiply: 6 * 4 = 24; degrees add: 1 + 1 = 2.
R and S are union-compatible with |R| = 8 and |S| = 5. The number of tuples in R U S can be at most and at least:
Show answer & explanation
Answer: A) 13 and 8
Maximum when disjoint: 8 + 5 = 13. Minimum when S is a subset of R: 8.
If R and S have no common attribute, R |X| S is equivalent to:
Show answer & explanation
Answer: C) R x S
A natural join with no common attribute has no equality conditions, so it equals the Cartesian product.
R(A, B, C) has A as its key. Which of the following is TRUE about |pi[A](R)|?
Show answer & explanation
Answer: B) |pi[A](R)| = |R|
A is a key so all A values are distinct; no duplicates are removed.
R left outer join S on key attribute where |R| = 10, |S| = 4 and 6 tuples of R find a match (each in exactly one S tuple). The left outer join has how many tuples?
Show answer & explanation
Answer: B) 10
6 matched tuples + 4 unmatched R tuples padded with NULL = 10.
Which equivalence is NOT valid?
Show answer & explanation
Answer: C) R - S = S - R
Set difference is not commutative; the other three are valid (selection commutes, natural join is commutative and associative).
In domain relational calculus the expression { <n> | EXISTS r (Student(r, n) AND r > 10) } returns:
Show answer & explanation
Answer: B) names of students whose roll number exceeds 10
Variables range over domain values; the formula filters on r > 10 and projects the name.
Query optimisation in relational algebra generally prefers to:
Show answer & explanation
Answer: B) push selections as early as possible
Early selections reduce intermediate result sizes; projections are pushed down too and small relations are joined first.
Topic FAQs
Which relational algebra operators are fundamental?
Selection, projection, union, set difference, Cartesian product and rename. Joins, intersection and division are derived; aggregation is an extension.
When is a natural join equal to a Cartesian product?
When the two relations have no attribute name in common, there is no equality condition and every pair is combined.
What makes a calculus expression unsafe?
It can return an infinite set, for example { t | NOT (t in R) } or an expression with a free variable not bound to any relation.
How do I write a "for all" query in relational algebra?
Use division R / S, or equivalently subtract from all candidates those that miss at least one divisor value.
Hover a card to reveal the answer with a 3D flip. On touch devices, tap the card or use Show answer.