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 be relations; a predicate; a list of retained attributes. Rename changes relation or attribute names, without changing values.
| Operator | Result | Condition |
|---|---|---|
| Rows satisfying | Same schema as | |
| Columns , with duplicate tuples removed | belongs to the input schema | |
| Rows in either input | Union-compatible schemas | |
| Rows in both inputs | Union-compatible schemas | |
| Rows in absent from | Union-compatible schemas | |
| All row pairs | Rename 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 . With a key on the join columns, it is at most . 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 distinct entities have this property” without aggregation, use renamed copies, impose the common property, and require their keys to be pairwise distinct. Combining these copies takes 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 be a relation whose attribute sets are disjoint, and let contain required values. Extra values associated with an value do not disqualify it.
If the question's candidate universe is a separate entity relation , replace by 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 be the row count of , and the number of distinct values of attribute .
For uniformly distributed integers from to inclusive, count satisfying integers and divide by . Clip at zero and one; do not use a continuous interval length for an integer domain. Without independence, use 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.