Question
Consider a relational database containing the following schemas.
| sno | pno | cost |
|---|---|---|
| S1 | P1 | 150 |
| S1 | P2 | 50 |
| S1 | P3 | 100 |
| S2 | P4 | 200 |
| S2 | P5 | 250 |
| S3 | P1 | 250 |
| S3 | P2 | 150 |
| S3 | P5 | 300 |
| S3 | P4 | 250 |
| sno | sname | location |
|---|---|---|
| S1 | M/s Royal furniture | Delhi |
| S2 | M/s Balaji furniture | Bangalore |
| S3 | M/s Premium furniture | Chennai |
| pno | pname | part_spec |
|---|---|---|
| P1 | Table | Wood |
| P2 | Chair | Wood |
| P3 | Table | Steel |
| P4 | Almirah | Steel |
| P5 | Almirah | Wood |
The primary key of each table is indicated by underlining the constituent fields.
SELECT s.sno, s.sname
FROM Suppliers s, Catalogue c
WHERE s.sno = c.sno AND
cost > (SELECT AVG(cost)
FROM Catalogue
WHERE pno = 'P4'
GROUP BY pno);
The number of rows returned by the above SQL query is
Official answer and solution
- Official answer
- A
- From
- Official GATE 2020 answer key
Solution
Evaluate the nested SQL subquery to find the threshold cost, then count matching rows from the join.
Step 1: Evaluate the inner subquery:
SELECT AVG(cost)
FROM Catalogue
WHERE pno = 'P4'
GROUP BY pno
- In Catalogue, the rows where
pno = 'P4'are:(S2, P4, 200)(S3, P4, 250)
- The average cost for
P4is:
Step 2: Filter rows in Catalogue where cost > 225:
- Checking all 9 rows of
Catalogue:- (S1, P1, 150): 150 is not > 225
- (S1, P2, 50): 50 is not > 225
- (S1, P3, 100): 100 is not > 225
- (S2, P4, 200): 200 is not > 225
- (S2, P5, 250): 250 > 225 (Match 1)
- (S3, P1, 250): 250 > 225 (Match 2)
- (S3, P2, 150): 150 is not > 225
- (S3, P5, 300): 300 > 225 (Match 3)
- (S3, P4, 250): 250 > 225 (Match 4)
Step 3: Join with Suppliers on s.sno = c.sno:
- Each of the 4 matching rows from
Cataloguehas a validsno('S2' or 'S3') present inSuppliers:- (S2, M/s Balaji furniture) from Catalogue (S2, P5, 250)
- (S3, M/s Premium furniture) from Catalogue (S3, P1, 250)
- (S3, M/s Premium furniture) from Catalogue (S3, P5, 300)
- (S3, M/s Premium furniture) from Catalogue (S3, P4, 250)
- Since the query does not specify
DISTINCT, all 4 rows are returned in the result set.
A
The answer is the one in the official GATE answer key. The solution is this site's own explanation.
Source
- Paper
- GATE 2020 Computer Science and Information Technology (CS)
- Question
- 23 of 65, printed as CS Q.13
- Section
- Computer Science and Information Technology
Report an error in this question
GATE questions are the work of the organising institutes. This site is independent and is not affiliated with them.