Sub topic 18
GATE Strategies: Counting Tuples and Checking Equivalence
Assume you know basic set operations, Cartesian products, first-order logic, and simple database tables. This chapter focuses on two exam skills: finding how many tuples a query can return and deciding whether two queries are equivalent.
Count in Stages
Read a query from the inside out. Track the number of tuples after each operation.
For relations \(R\) and \(S\):
| Operation | Useful cardinality rule | Column | Column | Column | Column |
|---|---|---|---|---|---|
| Selection \(\sigma\) | At most \( | R | \) tuples | ||
| Projection \(\pi\) | At most \( | R | \), because duplicates merge | ||
| Cartesian product \(R \times S\) | Exactly \( | R | \times | S | \) |
| Union \(R \cup S\) | Between the larger input size and the sum | ||||
| Difference \(R-S\) | Between 0 and \( | R | \) |
A selection can return every tuple or no tuple. Without information about the condition, its minimum is usually zero and its maximum is the input size.
A projection can reduce the number of tuples because relational algebra uses set semantics. For example, projecting age from five students may produce only three distinct ages.
A Cartesian product has an exact count. If Student has 40 tuples and Course has 8 tuples, Student × Course has \(40 \times 8 = 320\) tuples.
Joins are filtered Cartesian products. A join may return zero tuples. An equi-join can return more tuples than either input when one value matches many values on the other side. If duplicate matching values are possible, do not assume a join preserves the size of either relation.
Check Equivalence Safely
Two relational algebra expressions are equivalent when they produce the same relation for every legal database instance. Test algebraic laws before testing sample data.
Common valid laws include:
- Selection operations can often commute: \(\sigma{p}(\sigma{q}(R)) = \sigma{q}(\sigma{p}(R))\).
- Selections can combine: \(\sigmap(\sigmaq(R)) = \sigma_{p \land q}(R)\).
- Projection can move below selection when the selection attributes remain available.
- Projection does not generally commute with selection if it removes an attribute needed by the condition.
- Natural join is commutative, but outer joins require more care.
SQL requires an additional check. Plain SELECT uses bag semantics, so duplicate rows remain. Relational algebra normally uses set semantics. Therefore, equivalent-looking SQL queries may differ unless DISTINCT is present.
COUNT(*) counts rows, while COUNT(column) ignores NULL values. Also distinguish WHERE, which filters rows before grouping, from HAVING, which filters groups after aggregation.
Exam Procedure
- Write the size after each operator.
- Use keys and foreign keys to determine whether matches are one-to-one or one-to-many.
- Check whether projection removes duplicates.
- For SQL, account for bags, NULL, grouping, and
DISTINCT. - To disprove equivalence, find one legal counterexample database.
Exercise
A relation R has 12 tuples. What are the minimum and maximum sizes of πa(σp(R))?
Show answer
Minimum 0, maximum 12. Selection can remove every tuple, and projection cannot create more distinct tuples than its input.