Chapter 2

Relational algebra and query reasoning

Trace each operator's input columns and output rows. Classical algebra removes duplicate tuples; a natural join compares every common attribute name. Most counting errors come from overlooking one of these two rules.

2.1 Core operators and set semantics

Past papers · CS 2014–2026 (23 papers): 1 question, 2 marks · DA 2024–2026 (3 papers): 1 question, 1 mark

Let r,sr,s be relations; CC a predicate; XX a list of retained attributes. Rename ρ\rho changes relation or attribute names, without changing values.

OperatorResultCondition
σC(r)\sigma_C(r)Rows satisfying CCSame schema as rr
πX(r)\pi_X(r)Columns XX, with duplicate tuples removedXX belongs to the input schema
r∪sr\cup sRows in either inputUnion-compatible schemas
r∩sr\cap sRows in both inputsUnion-compatible schemas
r−sr-sRows in rr absent from ssUnion-compatible schemas
r×sr\times sAll row pairsRename clashing attributes when necessary

Union-compatible means equal arity and matching corresponding domains; rename columns as required. Selection changes rows; projection changes columns. Neither operation sorts the result.

2.2 Joins and repeated relation copies

Past papers · CS 2014–2026 (23 papers): 4 questions, 7 marks · DA 2024–2026 (3 papers): 2 questions, 3 marks

A theta join is selection over a Cartesian product. An equijoin uses equality conditions but may retain both copies of the compared attributes. A natural join compares all same-named attributes and keeps one copy of each. With no common attributes, it is a Cartesian product. A semijoin keeps only the left rows having a match; an antijoin keeps left rows without a match.

For an unconstrained join the count can range from zero to ∣r∣∣s∣|r||s|. With a key on the ss join columns, it is at most ∣r∣|r|. A left outer join also emits unmatched left rows padded with nulls; a full outer join adds unmatched right rows too. Outer joins are extended algebra and use nulls.

To express “at least kk distinct entities have this property” without aggregation, use kk renamed copies, impose the common property, and require their keys to be pairwise distinct. Combining these copies takes k−1k-1 binary cross products. To get maxima by self-join, remove rows having a strictly larger competitor; ties survive. A row with some smaller competitor is not a minimum, rather than necessarily being a maximum.

2.3 Division and universal conditions

Past papers · CS 2014–2026 (23 papers): 3 questions, 5 marks · DA 2024–2026 (3 papers): 1 question, 2 marks

Division answers “paired with every required value”. Let r(X,Y)r(X,Y) be a relation whose attribute sets X,YX,Y are disjoint, and let s(Y)s(Y) contain required values. Extra YY values associated with an XX value do not disqualify it.

If the question's candidate universe is a separate entity relation e(X)e(X), replace πX(r)\pi_X(r) by ee in both candidate positions. This matters when the required set is empty or a candidate has no recorded association.

2.4 Equivalent expressions and pushdown

Past papers · CS 2014–2026 (23 papers): 2 questions, 3 marks

Selections distribute over union, intersection and difference when the same predicate is applied to both sides. Projection distributes over union; it need not distribute over intersection or difference. Inner natural joins are associative and commutative at the relational level. Their physical execution costs can differ greatly.

2.5 Selectivity and output-size estimates

Past papers · CS 2014–2026 (23 papers): 1 question, 1 mark

Estimates need distribution assumptions; key-based bounds need only the constraints. Let nrn_r be the row count of rr, and V(A,r)V(A,r) the number of distinct values of attribute AA.

For uniformly distributed integers from LL to UU inclusive, count satisfying integers and divide by U−L+1U-L+1. Clip at zero and one; do not use a continuous interval length for an integer domain. Without independence, use P(C1∨C2)=P(C1)+P(C2)−P(C1∧C2)P(C_1\lor C_2)=P(C_1)+P(C_2)-P(C_1\land C_2) with the actual overlap.

Last-minute recall

  • Algebra projection removes duplicate tuples.
  • Natural join compares every common column, not just a named key.
  • A non-null foreign-key-to-key join preserves the referencing row count.
  • Division rejects candidates with a missing required pair.
  • A universal condition is true on an empty required set.
  • Keep join columns when pushing projection down.
  • Independence and uniformity are assumptions, not database constraints.

Revise offline: the whole Databases and Data Warehousing handbook as one PDF

58 pages · 2.7 MBFree to read online. Sign in to download the PDF.We save the handbook, version and request time in My downloads. Privacy policy

All chapters · Corrections