GATE / CS ARCHIVE

SIMILAR QUESTIONS / DATABASE MANAGEMENT SYSTEMS

Evaluate ALL using empty-set vacuous truth

2 questions solved the same way, newest first. Back to Database Management Systems

How these are solved

Evaluate the subquery first; ALL over no rows is true, whereas ANY requires a witness.

2 marks · NAT
Report issue

SQL & Queries

Question 46, GATE 2017: top_scorer(player,country,goals) rows: Klose,Germany,16; Ronaldo,Brazil,15; G Müller,Germany,14; Fontaine,France,13; Pelé,Brazil,12; Klinsmann,Germany,11; Kocsis,Hungary,11; Batistuta,Argentina,10; Cubillas,Peru,10; Lato,Poland,10; Lineker,England,10; T Müller,Germany,10; Rahn,Germany,10. Query: SELECT ta.player FROM top_scorer AS ta WHERE ta.goals > ALL (SELECT tb.goals FROM top_scorer AS tb WHERE tb.country='Spain') AND ta.goals > ANY (SELECT tc.goals FROM top_scorer AS tc WHERE tc.country='Germany'). How many tuples result?
Tap to enlarge
Answer & solution

Official answer: 7.0

Our answer: 7

1. The Spain subquery returns no rows. A comparison quantified by ALL over an empty set is true, because there is no counterexample. Hence the first predicate eliminates none of the outer rows.

2. The Germany subquery returns goals 16,14,11,10,10. The >ANY condition requires exceeding at least one of them, so with these non-NULL values it is equivalent to goals>10, not goals>16.

3. Seven rows have goal counts above 10: 16,15,14,13,12,11,11. They yield Klose,Ronaldo,G Müller,Fontaine,Pelé,Klinsmann,Kocsis, so the result has 7 tuples. The query does not say DISTINCT, but these seven selected player values are distinct anyway. Empty ALL and empty ANY behave differently.

Original paper, page 19 · Answer key

2 marks · MCQ
Report issue

ALL over an empty SQL subquery

SQL & Queries

Question 51, GATE 2012: Relations: A(Id,Name,Age)={(12,Arun,60),(15,Shreya,24),(99,Rohit,11)}; B(Id,Name,Age)={(15,Shreya,24),(25,Hari,40),(98,Rohit,20),(99,Rohit,11)}; C(Id,Phone,Area)={(10,2200,02),(99,2100,01)}.
How many tuples result from this SQL query?
SELECT A.Id FROM A WHERE A.Age > ALL (SELECT B.Age FROM B WHERE B.Name='Arun');
(A) 4 (B) 3 (C) 0 (D) 1.
Question 51, GATE 2012: Relations: A(Id,Name,Age)={(12,Arun,60),(15,Shreya,24),(99,Rohit,11)}; B(Id,Name,Age)={(15,Shreya,24),(25,Hari,40),(98,Rohit,20),(99,Rohit,11)}; C(Id,Phone,Area)={(10,2200,02),(99,2100,01)}.
How many tuples result from this SQL query?
SELECT A.Id FROM A WHERE A.Age > ALL (SELECT B.Age FROM B WHERE B.Name='Arun');
(A) 4 (B) 3 (C) 0 (D) 1.
Tap to enlarge
Your answer
Answer & solution

Official answer: B

There is no B row with Name=Arun, so the inner query returns an empty relation. A comparison quantified by ALL over an empty set is true: there is no counterexample. Consequently all three rows of A satisfy the WHERE condition, returning Id values 12, 15, 99. The count is 3, B.

Check: ALL means every returned value satisfies the comparison, not that at least one value exists. NOT EXISTS a violating subquery row is true when there are no subquery rows at all. ANY would have the opposite empty-set truth value, false.

Original paper, page 14 · Answer key