Skip to content
GATEiit

GATE DA · Database Management and Warehousing

Relational model: relational algebra, tuple calculus and SQL

Relations, keys and the three query languages GATE tests: relational algebra (including joins and division), tuple relational calculus, and SQL with GROUP BY, HAVING, subqueries and NULLs.

Before you start

  • ER model →

    Relations usually come from mapping an ER design; keys and foreign keys carry over.

  • Predicate logic →

    Tuple calculus is first-order logic with the quantifiers ∃ and ∀.

  • Set operations

    Union, intersection, difference and Cartesian product are the core of relational algebra.

This topic comes before: Integrity constraints and normal forms

Concept notes

1. The relational model

A relation is a table: a set of tuples (rows) over a fixed list of attributes (columns). Each attribute has a domain of atomic values. The schema is R(A1,…,An); the degree (arity) is n, and the cardinality is the number of tuples.

Because a relation is a set: there are no duplicate tuples and tuple order does not matter. SQL tables are bags (multisets): duplicates stay unless you write DISTINCT.

Keys:

  • Superkey: a set of attributes whose values differ for every pair of tuples.
  • Candidate key: a minimal superkey (no proper subset is a superkey).
  • Primary key: the candidate key chosen by the designer; cannot be NULL.
  • Foreign key: attributes in one relation whose values must appear as a key in the referenced relation (or be NULL).

NULL means unknown or not applicable. Any comparison with NULL (even NULL = NULL) gives UNKNOWN, a third truth value. WHERE and HAVING keep a row only if the condition is TRUE.

TRUE UNKNOWN FALSE
NOT FALSE UNKNOWN TRUE
TRUE AND x TRUE UNKNOWN FALSE
FALSE OR x TRUE UNKNOWN FALSE

2. Relational algebra operators

Relational algebra is procedural: each operator takes relations and returns a relation. The six basic operators are σ, π, ∪, −, ×, ρ; the rest can be written using them.

For R with m tuples and S with n tuples (m,n≥1):

Operator Meaning Result size
σp(R) rows of R satisfying p 0 to m
πA(R) columns A, duplicates removed 1 to m
R∪S union (union-compatible) max⁡(m,n) to m+n
R∩S intersection 0 to min⁡(m,n)
R−S in R but not in S max⁡(0,m−n) to m
R×S every pair exactly mn
R⋈θS σθ(R×S) 0 to mn
R⋈S natural join on common attributes 0 to mn
left outer join (⟕) join + unmatched R rows padded with NULL m to mn

Natural join equates all attributes with the same name and keeps one copy of each. With no common attributes it is R×S. If the common attribute is a key of S and a non-null foreign key in R, then R⋈S has exactly m tuples.

Union compatibility: same number of attributes with matching domains. Required for ∪, ∩ and −.

Useful identities: σp∧q(R)=σp(σq(R)); σp(R∪S)=σp(R)∪σp(S); R∩S=R−(R−S). Projection does not distribute over difference.

Relational algebra query treeπ nameσ cid = 'DB'⋈Student(sid, name)Enrolls(sid, cid)evaluated bottom-up
Query tree for π name(σ cid='DB'(Student ⋈ Enrolls)): names of students enrolled in DB.

3. Division: queries with 'for all'

Division R(A,B)÷S(B) returns the values a such that (a,b)∈R for every b∈S. It answers queries containing "all": students who have taken all core courses.

In terms of basic operators: R÷S=πA(R)−πA((πA(R)×S)−R)

Read it as: take every candidate a; pair it with every required b; the pairs missing from R expose the candidates that fail; remove them.

If S is empty, every a in πA(R) qualifies. Extra values of B in R that are not in S do not hurt.

In SQL, division is written with a double NOT EXISTS ("there is no required course that this student has not taken"):

SELECT DISTINCT t.sid FROM Takes t
WHERE NOT EXISTS (
  SELECT * FROM Core k
  WHERE NOT EXISTS (
    SELECT * FROM Takes t2
    WHERE t2.sid = t.sid AND t2.cid = k.cid));

or by counting: group by sid and keep groups whose count of distinct core courses equals the number of core courses.

4. Tuple relational calculus

Tuple relational calculus (TRC) is declarative: it says what tuples are wanted, not how to get them. A query has the form { t∣P(t) } where t is a tuple variable and P is a formula built from:

  • atoms t∈R, t.A  θ  s.B, t.A  θ  c (with θ one of =,≠,<,≤,>,≥);
  • connectives ∧, ∨, ¬, ⇒;
  • quantifiers ∃s∈R (… ) and ∀s∈R (… ).

Example: names of students enrolled in DB: {t∣∃s∈Student ∃e∈Enrolls (s.sid=e.sid∧e.cid=’DB’∧t.name=s.name)}

"For all" is written with ∀, or as ¬∃¬: ∀x P(x)≡¬∃x ¬P(x).

Safety: a query is safe if its answer is finite and drawn from values in the database. {t∣¬(t∈R)} is unsafe: it asks for every possible tuple not in R, an infinite set.

Codd's theorem: safe TRC, safe domain calculus and relational algebra have the same expressive power. SQL adds aggregates, grouping and bags, which basic relational algebra lacks.

5. SQL: grouping, NULLs and subqueries

Logical order of evaluation: FROM → WHERE → GROUP BY → HAVING → SELECT → ORDER BY. WHERE filters rows (no aggregates allowed); HAVING filters groups.

Aggregates COUNT, SUM, AVG, MIN, MAX ignore NULLs. COUNT() counts rows, NULLs included. AVG(x) = SUM(x) / COUNT(x), not divided by COUNT(). On an empty set COUNT gives 0 and the others give NULL. GROUP BY puts all NULLs of the grouping column into one group.

Subqueries:

  • x IN (subquery): TRUE if x equals some value.
  • x NOT IN (subquery): if the subquery returns any NULL, the result is never TRUE, so no rows come out.
  • EXISTS (subquery): TRUE if the subquery returns at least one row; NULLs do not matter.
  • x > ALL (subquery): greater than every value; TRUE for an empty subquery.
  • x > ANY (or SOME): greater than at least one value; FALSE for an empty subquery.

A correlated subquery refers to the outer row and is re-evaluated for each one.

Joins: INNER JOIN keeps matches only; LEFT OUTER JOIN also keeps unmatched left rows, padded with NULL; FULL OUTER keeps unmatched rows of both sides.

Duplicates: SELECT keeps duplicates; SELECT DISTINCT removes them. UNION, INTERSECT and EXCEPT remove duplicates; UNION ALL keeps them.

Formulas to remember

Theta join
R⋈θS=σθ(R×S)
Intersection from difference
R∩S=R−(R−S)
Division
R÷S=πA(R)−πA((πA(R)×S)−R)
Cartesian product sizeDegree is the sum of the two degrees.
∣R×S∣=∣R∣⋅∣S∣
Union size bounds
max⁡(m,n)≤∣R∪S∣≤m+n
Universal from existential
∀x P(x)≡¬∃x ¬P(x)
AVG ignores NULLCOUNT(x) counts non-NULL values only.
AVG(x)=SUM(x)COUNT(x)

Common mistakes

  • Forgetting that projection removes duplicates in relational algebra but SELECT keeps them in SQL.
  • Dividing by COUNT(*) instead of COUNT(column) when an average involves NULLs.
  • Expecting NOT IN to behave like NOT EXISTS when the subquery can return NULL.
  • Using WHERE to filter on an aggregate; that needs HAVING.
  • Treating natural join as a Cartesian product when the relations do share attribute names, or forgetting it becomes one when they share none.
  • Pushing projection through difference: π(R − S) is not π(R) − π(S).

Worked examples

Try each step yourself, then reveal the working.

Example 1: Division by hand

numerical

Enrolls(sid, cid) = {(1, C1), (1, C2), (2, C1), (3, C1), (3, C2), (3, C3)} and Course(cid) = {C1, C2}. Find Enrolls÷Course.

  1. Step 1: List the candidates.

    Try it on paper first.

Example 2: Aggregates with NULLs

numerical

Emp(name, dept, salary) = (Asha, CS, 50), (Ravi, CS, NULL), (Meena, EE, 40), (Kiran, EE, 60), (Tara, ME, NULL). Find COUNT(*), COUNT(salary) and AVG(salary) over the whole table, and the result of SELECT dept, AVG(salary) FROM Emp GROUP BY dept.

  1. Step 1: Count rows and non-NULL salaries.

    Try it on paper first.

Example 3: NOT IN versus NOT EXISTS with a NULL

conceptual

R(a) = {1, 2, 3}, S(b) = {2, NULL}. Compare the outputs of: (Q1) SELECT a FROM R WHERE a NOT IN (SELECT b FROM S); (Q2) SELECT a FROM R WHERE NOT EXISTS (SELECT * FROM S WHERE S.b = R.a).

  1. Step 1: Expand NOT IN for a = 1.

    Try it on paper first.

Example 4: Reading a TRC query

conceptual

What does {t∣∃s∈Student (t.sid=s.sid∧∀c∈Core ∃e∈Takes (e.sid=s.sid∧e.cid=c.cid))} return?

  1. Step 1: Read the outer part.

    Try it on paper first.

Previous year questions

9 official GATE DA questions on this topic, newest first.

Practise these 9 PYQs →

Topic test

10 questions: up to 4 official PYQs and original practice questions, with GATE marking (MCQ wrong answers lose marks). Score 60% or more to pass. A new set is drawn each time.

Mark as mastered

Loading your progress…

My revision notes

Notes written by the GATEiit team against the official GATE DA syllabus; being checked by a reviewer. Updated 2026-11-21. Spotted a mistake? Use Report an issue on any question, or contact us.