2,100 views
1 1 vote
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?

Please log in or register to answer this question.

Position:
Show:

Related questions

1 1 vote
0 0 answers
622
622 views
Harikesh Kumar asked Jan 14, 2018
622 views
Provide the answer
0 0 votes
1 1 answer
1.9k
1.9k views
ibia asked Apr 7, 2016
1,945 views
Specify the following queries in SQL on the database schema of Figure 1.2.Figure 1.2:A database that storesstudent and courseinformation.CaptionRetrieve the names of all ...
0 0 votes
0 0 answers
4.1k
4.1k views
ibia asked Apr 6, 2016
4,075 views
Specify the following queries in SQL on the COMPANY relational database schema shown in figure 3.5 .Show the result of each query if it is applied to the COMPANY databas...
1 1 vote
0 0 answers
1.2k
1.2k views
ibia asked Apr 5, 2016
1,186 views
Specify the updates of Exercise 3.11 using the SQL update commands.EXERCISE 3.11 : Suppose that each of the following Update operations is applied directly to the databa...