Skip to main content

GATE CSE 2020Databases › SQLVerified

GATE CSE 2020 PYQ: Consider a relational database containing the following schemas. sno ‾ pno ‾ cost S 1 P 1 150 S 1 P 2 50 S 1…

Consider a relational database containing the following schemas.

sno‾pno‾costS1P1150S1P250S1P3100S2P4200S2P5250S3P1250S3P2150S3P5300S3P4250Cataloguesno‾snamelocationS1M/s Royal furnitureDelhiS2M/s Balaji furnitureBangaloreS3M/s Premium furnitureChennaiSupplierspno‾pnamepart_specP1TableWoodP2ChairWoodP3TableSteelP4AlmirahSteelP5AlmirahWoodParts\overset{\text{Catalogue}} {\begin{array}{|c|c|c|} \hline \underline{\text{sno}} & \underline{\text{pno}} & \text{cost} \\ \hline S1 & P1 & 150 \\ \hline S1 & P2 & 50 \\ \hline S1 & P3 & 100 \\ \hline S2 & P4 & 200 \\ \hline S2 & P5 & 250 \\ \hline S3 & P1 & 250 \\ \hline S3 & P2 & 150 \\ \hline S3 & P5 & 300 \\ \hline S3 & P4 & 250 \\ \hline \end{array}} \quad \overset{ \text{Suppliers} }{\begin{array}{|c|c|c|} \hline \underline{\text{sno}} & \text{sname} & \text{location} \\ \hline S1 & \text{M/s Royal furniture} & \text{Delhi}\\ \hline S2 & \text{M/s Balaji furniture} & \text{Bangalore} \\ \hline S3 & \text{M/s Premium furniture} & \text{Chennai} \\ \hline \end{array}} \quad \overset{\text{Parts}}{\begin{array}{|c|c|c|} \hline \underline{\text{pno}} & \text{pname} & \text{part\_spec} \\ \hline P1 & \text{Table} & \text{Wood} \\ \hline P2 & \text{Chair} & \text{Wood} \\ \hline P3 & \text{Table} & \text{Steel} \\ \hline P4 & \text{Almirah} & \text{Steel} \\ \hline P5 & \text{Almirah} & \text{Wood} \\ \hline \end{array}}

The primary key of each table is indicated by underlining the constituent fields.

  1. SELECT s.sno, s.sname
  2. FROM Suppliers s, Catalogue c
  3. WHERE s.sno=c.sno AND
  4. cost > (SELECT AVG (cost)
  5.     FROM Catalogue
  6.    WHERE pno = ‘P4’
  7.    GROUP BY pno) ;

The number of rows returned by the above SQL query is

  1. A
    4
    Correct answer
  2. B
    5
  3. C
    0
  4. D
    2

Answer

(A) 4 4 4

Approach

Evaluate the non-correlated inner subquery first to get the threshold value using AVG over rows with pno = 'P4'. Then apply the outer join with the filter cost > threshold and count the qualifying rows (SQL does not remove duplicates without DISTINCT).

The inner subquery yields 225 as the average cost of part P4. Four Catalogue entries have cost above 225, and all four join successfully with Suppliers, producing 4 rows without duplicate removal. Hence option A.

Reviewed by GateIQ Editorial Team: IIT & IISc Alumni.