Skip to main content

GATE CSE 2019Databases › SQLVerified

GATE CSE 2019 PYQ: A relational database contains two tables Student and Performance as shown below: Roll_no Student_name 1…

A relational database contains two tables Student and Performance as shown below:

Roll_noStudent_name1Amit2Priya3Vinit4Rohan5SmitaTable: studentRoll_noSubject_codeMarks1A861B951C902A892C923C80Table: Performance\overset{\text{Table: student}}{\begin{array}{|l|l|} \hline \text{Roll\_no} & \text{Student\_name}\\\hline 1 & \text{Amit} \\\hline 2 & \text{Priya} \\\hline 3 & \text{Vinit} \\\hline 4 & \text{Rohan} \\\hline 5 & \text{Smita} \\\hline \end{array}} \qquad\overset{\text{Table: Performance}}{\begin{array}{|l|l|l|} \hline \text{Roll\_no} & \text{Subject\_code} & \text{Marks}\\\hline 1 & \text{A} & 86 \\\hline 1 & \text{B} & 95 \\\hline 1 & \text{C} & 90 \\\hline 2 & \text{A} & 89 \\\hline 2 & \text{C} & 92 \\\hline 3 & \text{C} & 80 \\\hline \end{array}}

The primary key of the Student table is Roll_no. For the performance table, the columns Roll_no. and Subject_code together form the primary key. Consider the SQL query given below:

  1. SELECT S.Student_name, sum(P.Marks)
  2. FROM Student S, Performance P
  3. WHERE P.Marks >84
  4. GROUP BY S.Student_name;

The number of rows returned by the above SQL query is ________

    Answer

    5

    Approach

    Interpret the FROM clause as a Cartesian product because there is no join condition relating $S.Roll_no$ and $P.Roll_no$. Filter Performance rows with Marks > 84, combine them with every Student row, then group the resulting tuples by $S.Student_name$. The number of result rows is the number of distinct $Student_name$ values in that filtered product.

    The query outputs one row per distinct $S.Student_name$ over the filtered Cartesian product Student x Performance. All five student names appear in the result, so the number of returned rows is 5.

    Reviewed by GateIQ Editorial Team: IIT & IISc Alumni.