| sid |
sname |
rating |
age |
| 18 |
jones |
3 |
30.0 |
| 41 |
jonah |
6 |
56.0 |
| 22 |
ahab |
7 |
44.0 |
| 63 |
moby |
null |
15.0 |
Consider a below relation Sailors
We want to find the names of all sailors with a higher rating than all sailors with age<21
The following two SQL queries attempt to obtain the answer to the above question.Do they both Compute the same result?Under what condition, would they compute the same result?
Q1: Select S.sname
FROM Sailors S
WHERE NOT EXISTS ( SELECT *
FROM Sailors S2
WHERE S2.age<21
AND S.rating <=S2.rating)
Q2: SELECT *
FROM Sailors S
WHERE S.rating>ANY(SELECT S2.rating
FROM Sailors S2
WHERE S2.age<21)
I think first query returns all the sailors name because the co-related nested query when selects S2.age<21 picks up sailor with sid 63, but as soon as comparison S.rating<=S2.rating is done, it results in Unknown as comparison with Null is Unknown and the whole inner query for each sailor records goes empty and NOT EXISTS(EMPTY SET)=TRUE.
In the Second query, in nest query returns an empty set, and .>ANY(Empty Set)=False for all sailor records and hence no record will be selected.
So, NO, both queries will not give same result.Infact, both give incorrect result but different result sets.
Now suppose I give a rating to the Sailor with sid 63 having age=15.Now if this sailor is given highest rating , then both queries will give same result → the empty set.
Is my analysis Correct?