Skip to main content

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.

~4 hrs ~1–1.5 marks · High 8 samples · 8 practice · 8 cards

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)
OperatorResult degreeResult cardinality (minimum .. maximum)Needs compatible schemas?
sigma[c](R)deg(R)0 .. |R|no
pi[A1..Ak](R)k1 .. |R| (0 if R is empty); exactly |R| if a key is keptno
R U Sdeg(R)max(|R|, |S|) .. |R| + |S|yes
R n Sdeg(R)0 .. min(|R|, |S|)yes
R - Sdeg(R)0 .. |R|yes
R x Sdeg(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.

Emp and Dept tables with the results of selection on did equal 10, projection on did and the natural join, showing 2, 2 and 2 tuples, plus key algebra identities.
sigma filters rows, pi keeps columns and removes duplicates, and a natural join combines tuples that agree on the common attribute.

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 typeKeepsUnmatched padded with NULL?
Natural / inner / thetamatching pairs onlyno
Left outerall of R + matchesS columns
Right outerall of S + matchesR columns
Full outerall of R and all of Sboth sides as needed
Semi-joinR tuples that have a matchno (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.

R(A,B) divided by S(B): a1 and a3 are paired with every B value of S so the quotient is a1 and a3, with the derivation using projection, product and difference.
Division: keep an A value only if it is paired with EVERY B value in S. The derivation subtracts the A values that have at least one missing pair.

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 Bmin |R |X| S|max |R |X| S|
B arbitrary in both0|R| * |S|
B is a key of S0|R|
B is a key of both R and S0min(|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.

R(A,B) and S(B,C) with the natural join result of 2 tuples and the left, right and full outer join results of 3, 3 and 4 tuples with NULL padding for dangling tuples.
Same data, four joins: the inner join keeps only matches; each outer join adds the dangling tuples of one or both sides padded with NULL.

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.
Exam pattern · 8 questions

GATE sample questions & answers

8 GATE-style MCQs for Relational Algebra & Calculus with full explanations. Attempt each question before revealing the answer.

Q1 GATE join-size pattern

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:

  • A) 50
  • B) 100
  • C) 150
  • D) 5000
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.

Q2

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:

  • A) 1
  • B) 2
  • C) 3
  • D) 5
Show answer & explanation

Answer: B

a1 and a3 are paired with both b1 and b2; a2 only with b1. So R / S = {a1, a3}.

Q3

Which of the following CANNOT be expressed using the six fundamental operators (select, project, union, difference, product, rename)?

  • A) intersection
  • B) natural join
  • C) division
  • D) count of tuples per group
Show answer & explanation

Answer: D

Intersection, natural join and division are derived. Aggregation with grouping is an extended operator outside the basic algebra.

Q4

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:

  • A) 0
  • B) 3
  • C) 5
  • D) 8
Show answer & explanation

Answer: D

No matches means every R tuple (5) and every S tuple (3) appears padded with NULLs: 5 + 3 = 8.

Q5

Which expression is equivalent to R n S?

  • A) R - (R - S)
  • B) (R U S) - R
  • C) R - S
  • D) (R - S) U (S - R)
Show answer & explanation

Answer: A

R - S removes the common tuples from R; subtracting that from R leaves exactly the tuples common to both.

Q6

Which tuple calculus expression is NOT safe?

  • A) { t | R(t) AND t.A > 5 }
  • B) { t | NOT R(t) }
  • C) { t | R(t) OR S(t) }
  • D) { t | R(t) AND NOT S(t) }
Show answer & explanation

Answer: B

All tuples not in R form an infinite set; the other expressions restrict t to relations of the database.

Q7

Given R(A, B) = {(1,2), (1,3), (2,2)}, how many tuples are in pi[A](sigma[B = 2](R))?

  • A) 1
  • B) 2
  • C) 3
  • D) 0
Show answer & explanation

Answer: B

sigma[B=2] keeps (1,2) and (2,2); projecting A gives {1, 2}: 2 tuples.

Q8

Student(sid, name), Enrolled(sid, cid), Course(cid). Which expression gives the sids of students enrolled in ALL courses?

  • A) pi[sid, cid](Enrolled) / pi[cid](Course)
  • B) pi[sid](Enrolled) - pi[cid](Course)
  • C) pi[sid, cid](Enrolled) |X| pi[cid](Course)
  • D) pi[cid](Course) / pi[sid, cid](Enrolled)
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.

Topic drill · 8 questions

8 practice questions — Relational Algebra & Calculus

Extra page-specific practice beyond the GATE samples. Attempt all 8, then reveal answers to check your reasoning.

P1

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:

  • A) 10 tuples, degree 2
  • B) 24 tuples, degree 2
  • C) 24 tuples, degree 1
  • D) 10 tuples, degree 1
Show answer & explanation

Answer: B) 24 tuples, degree 2

Tuples multiply: 6 * 4 = 24; degrees add: 1 + 1 = 2.

P2

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:

  • A) 13 and 8
  • B) 13 and 5
  • C) 40 and 8
  • D) 8 and 5
Show answer & explanation

Answer: A) 13 and 8

Maximum when disjoint: 8 + 5 = 13. Minimum when S is a subset of R: 8.

P3

If R and S have no common attribute, R |X| S is equivalent to:

  • A) R U S
  • B) R - S
  • C) R x S
  • D) the empty relation
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.

P4

R(A, B, C) has A as its key. Which of the following is TRUE about |pi[A](R)|?

  • A) |pi[A](R)| < |R| always
  • B) |pi[A](R)| = |R|
  • C) |pi[A](R)| > |R|
  • D) |pi[A](R)| = 1
Show answer & explanation

Answer: B) |pi[A](R)| = |R|

A is a key so all A values are distinct; no duplicates are removed.

P5

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?

  • A) 6
  • B) 10
  • C) 14
  • D) 4
Show answer & explanation

Answer: B) 10

6 matched tuples + 4 unmatched R tuples padded with NULL = 10.

P6

Which equivalence is NOT valid?

  • A) sigma[c1](sigma[c2](R)) = sigma[c2](sigma[c1](R))
  • B) R |X| S = S |X| R
  • C) R - S = S - R
  • D) (R |X| S) |X| T = R |X| (S |X| T)
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).

P7

In domain relational calculus the expression { <n> | EXISTS r (Student(r, n) AND r > 10) } returns:

  • A) all roll numbers
  • B) names of students whose roll number exceeds 10
  • C) all students
  • D) an infinite set
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.

P8

Query optimisation in relational algebra generally prefers to:

  • A) perform Cartesian products before selections
  • B) push selections as early as possible
  • C) delay projections to the end
  • D) join the largest relations first
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.

Quick revision · 8 cards

8 flashcards — Relational Algebra & Calculus

Hover a card to reveal the answer with a 3D flip. On touch devices, tap the card or use Show answer.

Q1 Six fundamental relational algebra operators?
Q2 Does projection remove duplicates?
Q3 Size of the natural join of R and S on a key/foreign key?
Q4 Formula for R n S in terms of difference?
Q5 What does R(A,B) / S(B) return?
Q6 Division with basic operators?
Q7 Safe calculus expression?
Q8 Equivalent power?