Consider the relations \(r(A, B)\) and \(s(B, C)\), where \(s.B\) is a primary…
2018
Consider the relations \(r(A, B)\) and \(s(B, C)\), where \(s.B\) is a primary key and \(r.B\) is a foreign key referencing \(s.B\). Consider the query
\(Q: 𝑟 ⋈ (𝜎𝐵<5(𝑠))\)
Let \(LOJ\) denote the natural left outer-join operation. Assume that \(r\) and \(s\) contain no null values.
Which one of the following queries is NOT equivalent to Q?
- A.
\(𝜎_{𝐵<5}(𝑟 ⋈ 𝑠)\) - B.
\(𝜎_{𝐵<5}(𝑟 \ 𝐿𝑂𝐽 \ 𝑠)\) - C.
\(𝑟 \ 𝐿𝑂𝐽 \ (𝜎_{𝐵<5} (𝑠))\) - D.
\(𝜎_{𝐵<5} \ (𝑟) \ 𝐿𝑂𝐽 \ 𝑠\)
Attempted by 101 students.
Show answer & explanation
Correct answer: C
Answer: r LOJ (σ_{B<5}(s)) is NOT equivalent to the original query.
Key facts:
s.B is a primary key and r.B is a foreign key referencing s.B; there are no nulls. Therefore every tuple of r has a matching tuple in s.
The original query r ⋈ (σ_{B<5}(s)) returns exactly those r tuples whose matching s has B<5, joined with their s attributes.
Why the three equivalent expressions match the original query:
σ_{B<5}(r ⋈ s): Since every r tuple has a matching s tuple, performing the join first and then filtering B<5 keeps exactly those joined pairs where the matched s has B<5, same as the original.
σ_{B<5}(r LOJ s): Because of referential integrity (every r matches an s), the left outer join behaves like an inner join here. Filtering B<5 afterward therefore yields the same result as the original query.
σ_{B<5}(r) LOJ s: Selecting r tuples with B<5 first restricts to r rows whose matching s tuples have B<5; left outer-joining s then matches those s tuples, giving the same pairs as the original.
Why r LOJ (σ_{B<5}(s)) is not equivalent:
Filtering s before a left outer join removes s tuples with B>=5. For r tuples whose matching s had B>=5, the left outer join now produces those r tuples with NULLs for s attributes instead of excluding them. The original query excludes those r tuples because their matching s was filtered out of σ_{B<5}(s).
Counterexample: s = {(B=4,...), (B=6,...)} and r = {(B=4,...), (B=6,...)}. The original query r ⋈ (σ_{B<5}(s)) returns only the row with B=4. The expression r LOJ (σ_{B<5}(s)) returns both r rows, with the B=6 row having NULL s attributes, so the results differ.
Conclusion: r LOJ (σ_{B<5}(s)) returns extra r tuples when their matching s tuples were removed by the selection, so it is not equivalent to the original query; the other listed expressions are equivalent under the given constraints.
A video solution is available for this question — log in and enroll to watch it.