ISRO | ISRO CS 2017 | Question 11
Consider the schema
Sailors(sid, sname, rating, age) with the following data
sid | sname | rating | age |
---|---|---|---|
22 | Dustin | 7 | 45 |
29 | Borg | 1 | 33 |
31 | Pathy | 8 | 55 |
32 | Robert | 8 | 25 |
58 | Raghu | 10 | 17 |
64 | Herald | 7 | 35 |
71 | Vishnu | 10 | 16 |
74 | King | 9 | 35 |
85 | Archer | 3 | 26 |
84 | Bob | 3 | 64 |
96 | Flinch | 3 | 17 |
For the query
SELECT S.rating, AVG(S.age) AS avgage FROM Sailors S Where S.age >= 18 GROUP BY S.rating HAVING 1 < (SELECT COUNT(*) FROM Sailors S2 where S.rating = S2.rating)
The number of rows returned is
(A) 6
(B) 5
(C) 4
(D) 3
Answer: (D)
Explanation: The qiven query would run like:
First of all where statement would be executed and all the tuples with age less than 18 would be eliminated.
Now with Group By the remaining tuples will be grouped according to rating:
rating | sid | sname | age |
---|---|---|---|
1 | 29 | Borg | 33 |
3 | 85 | Archer | 26 |
84 | Bob | 64 | |
7 | 22 | Dustin | 45 |
64 | Herald | 35 | |
8 | 31 | Pathy | 55 |
32 | Robert | 25 | |
9 | 74 | King | 35 |
After this, all the group-by tuples having their count more than 1 will be selected and their ratings and average age will be given as output.
rating | avgage |
---|---|
3 | 45 |
7 | 40 |
8 | 40 |
So, the correct answer is (C)