Sub topic 22
GATE strategies: counting tuples and checking equivalence
This chapter assumes you know the operators and SQL constructs from the earlier chapters: selection, projection, set operations, joins, division, grouping, NULL handling and subqueries. Here you use them for the two question types GATE asks most often. One asks how many tuples a query returns, or the minimum or maximum it can return. The other asks which of several expressions are equivalent.
Counting tuples with bounds
Most counting questions give you only table sizes and key facts. You then need bounds, not an exact answer. Let R have m tuples and S have n tuples. Relational algebra uses set semantics, so there are no duplicates.
| Expression | Minimum | Maximum |
|---|---|---|
| σ(R) | 0 | m |
| π(R), R nonempty | 1 | m |
| R ∪ S | max(m, n) | m + n |
| R ∩ S | 0 | min(m, n) |
| R − S | max(0, m − n) | m |
| R × S | m · n | m · n |
| R ⋈θ S | 0 | m · n |
| R ⟕ S (left outer join) | m | m · n |
| R(A,B) ÷ S(B), S nonempty | 0 | ⌊m / n⌋ |
Three points explain most of the table:
- The union minimum is max(m, n). The union contains all of R, so it has at least m tuples. It also contains all of S, so it has at least n.
- A projection can collapse many tuples into one. Projecting onto a constant-valued column leaves a single tuple.
- The division maximum comes from counting. Each A-value in the result needs at least n tuples in R, one for each tuple of S. At most ⌊m / n⌋ distinct A-values can qualify.
Joins and keys
Key and foreign-key facts tighten the bounds on a natural join R ⋈ S.
- No common attributes. The join is a Cartesian product, so it returns exactly m · n tuples.
- Common attribute is a key of S. Each R tuple matches at most one S tuple, so the result has between 0 and m tuples.
- Common attribute is a key of S and a non-null foreign key in R. Each R tuple matches exactly one S tuple, so the result has exactly m tuples.
- Common attribute is a key in neither relation. The result has between 0 and m · n tuples.
For example, take Enrolled(sid, cid) with 600 tuples, where sid is a non-null foreign key to Student(sid). Then Enrolled ⋈ Student has exactly 600 tuples, whatever the size of Student.
SQL counts differ from algebra counts
SQL uses bag semantics, which means duplicate rows are kept. Adjust the algebra bounds in these cases:
- SELECT without DISTINCT. A projection keeps all m rows, so its minimum and maximum are both m.
- UNION ALL. It returns exactly m + n rows. Plain UNION, INTERSECT and EXCEPT remove duplicates.
- COUNT(*) and COUNT(col). COUNT(*) counts all rows. COUNT(col) skips NULLs.
- GROUP BY. It returns one row per distinct group, so the count is at most m and at least 1 for a nonempty table.
- Aggregate query with no GROUP BY. It returns exactly one row, even on an empty table. On an empty table, COUNT gives 0 and SUM gives NULL.
Checking equivalence
To decide whether two expressions are equivalent, first look for a counterexample. If you cannot find one, look for a rewriting rule. A small instance is enough to refute equivalence. Try the instances below in order, because they catch the most common traps:
- An empty table.
- A table with duplicates.
- A table with a NULL in the compared column.
Equivalences that fail
These "obvious" rewrites are wrong, and exam options are often built from them:
- π(R ∩ S) = π(R) ∩ π(S). Only ⊆ holds. R = {(1,a)} and S = {(1,b)} projected onto the first column give {1} on the right and ∅ on the left.
- π(R − S) = π(R) − π(S). This is also false, for the same instance.
- NOT IN versus NOT EXISTS. They differ when the subquery column can contain NULL, as the example below shows.
- Inner join versus LEFT JOIN. A LEFT JOIN keeps unmatched left tuples. Adding WHERE on a right-side column silently turns it back into an inner join.
Equivalences that hold
- σ commutes with σ, and σ distributes over ∪, ∩ and −.
- ∪ and ∩ are commutative and associative. Difference is neither.
- A theta join equals σθ(R × S).
- A LEFT JOIN with
WHERE e.sid IS NULLis an anti-join. On a non-null join column it is equivalent to NOT EXISTS.
Worked example: NOT IN against NOT EXISTS
This script runs in SQLite or PostgreSQL. The Enrolled table contains a NULL sid.
CREATE TABLE Student (sid INT PRIMARY KEY, sname VARCHAR(20), age INT);
CREATE TABLE Enrolled (sid INT, cid INT);
INSERT INTO Student VALUES (1, 'Ann', 18), (2, 'Bob', 21), (3, 'Cy', NULL);
INSERT INTO Enrolled VALUES (1, 10), (NULL, 11);
-- Returns 0 rows.
SELECT sid FROM Student
WHERE sid NOT IN (SELECT sid FROM Enrolled);
-- Returns sids 2 and 3.
SELECT s.sid FROM Student s
WHERE NOT EXISTS (SELECT 1 FROM Enrolled e WHERE e.sid = s.sid);For sid 2, the test 2 NOT IN (1, NULL) expands to 2 <> 1 AND 2 <> NULL. That is TRUE AND UNKNOWN, which is UNKNOWN, and WHERE keeps only TRUE rows. The NOT EXISTS version asks whether a matching row exists, and e.sid = s.sid never matches the NULL row, so students 2 and 3 survive.
A workflow for exam questions
The diagram shows the order to work through a question.
For minimum-and-maximum questions, build one instance that reaches the minimum and another that reaches the maximum. This confirms that both bounds are actually attainable.
Exercise
R(A, B) has 12 tuples and S(B) has 4 tuples. What are the minimum and maximum numbers of tuples in R ÷ S? Then, if R(A, B) ∪ T(A, B) has T with 5 tuples, what are the minimum and maximum sizes of the union?
Show answer
For the division, the minimum is 0, because no A-value need be paired with all four B-values. The maximum is ⌊12 / 4⌋ = 3, because each qualifying A-value uses at least 4 of the 12 tuples. For the union, the minimum is max(12, 5) = 12, when T is contained in R. The maximum is 12 + 5 = 17, when the two share no tuples.
Exercise
Student has sids 1, 2, 3. Enrolled has the rows (1, 10) and (NULL, 11). How many rows does SELECT sid FROM Student WHERE sid NOT IN (SELECT sid FROM Enrolled) return, and how many does the NOT EXISTS version return?
Show answer
The NOT IN query returns 0 rows. For sids 2 and 3 the comparison against the NULL is UNKNOWN, and for sid 1 the match makes the condition FALSE. The NOT EXISTS version returns 2 rows, sids 2 and 3, because the NULL row never satisfies e.sid = s.sid.