Skip to main content

GATE CSE 2022Databases › SQLVerified

GATE CSE 2022 PYQ: Consider the relational database with the following four schemas and their respective instances. Students…

Consider the relational database with the following four schemas and their respective instances.

StudentssNosNamedNoS01JamesD01S02RockyD01S03JacksonD02S04JaneD01S05MilliD02DepthdNodNameD01CSED02EEECoursecNocNamedNoC11DSD01C12OSD01C21DED02C22PTD02C23CVD03RegistersNocNoS01C11S01C12S02C11S03C21S03C22S03C23S04C11S04C12S05C11S05C21\begin{array} {|c|c|c|} \hline & \text{Students} & \\ \hline \text{sNo} & \text{sName} & \text{dNo} \\ \hline \text{S01} & \text{James} & \text{D01} \\ \hline \text{S02} & \text{Rocky} & \text{D01} \\ \hline \text{S03} & \text{Jackson} & \text{D02} \\ \hline \text{S04} & \text{Jane} & \text{D01} \\ \hline \text{S05} & \text{Milli} & \text{D02}\\ \hline \end{array} \quad \begin{array} {|c|c|} \hline & \text{Depth} \\ \hline \text{dNo} & \text{dName} \\ \hline \text{D01} & \text{CSE} \\ \hline \text{D02} & \text{EEE} \\ \hline \end{array} \quad \begin{array} {|c|c|c|} \hline & \text{Course} & \\ \hline \text{cNo} & \text{cName} & \text{dNo} \\ \hline \text{C11} & \text{DS} & \text{D01} \\ \hline \text{C12} & \text{OS} & \text{D01} \\ \hline \text{C21} & \text{DE} & \text{D02} \\ \hline \text{C22} & \text{PT} & \text{D02} \\ \hline \text{C23} & \text{CV} & \text{D03}\\ \hline \end{array}\quad \begin{array} {|c|c|} \hline & \text{Register} \\ \hline \text{sNo} & \text{cNo} \\ \hline \text{S01} & \text{C11} \\ \hline \text{S01} & \text{C12} \\ \hline \text{S02} & \text{C11} \\ \hline \text{S03} & \text{C21}\\ \hline \text{S03} & \text{C22} \\ \hline \text{S03} & \text{C23} \\ \hline \text{S04} & \text{C11} \\ \hline \text{S04} & \text{C12} \\ \hline \text{S05} & \text{C11}\\ \hline \text{S05} & \text{C21} \\ \hline \end{array}

SQL query\textbf{SQL query}

  1. SELECT * FROM Student AS S WHERE NOT EXIST
  2.  
  3. (SELECT cNo FROM Course WHERE dNo = “D01”
  4.  
  5. EXCEPT
  6.  
  7. SELECT cNo FROM Register WHERE sNo = S.sNo)

The number of rows returned by the above SQL\text{SQL} query is ____________.

 

    Answer

    2

    Approach

    The query is a relational-division / 'for all' query: it asks for students who have registered for every course offered by department D01. For each student tuple S in Student, the inner correlated subquery computes the set of D01 course numbers that the student has NOT registered for. The student is selected only if that difference set is empty.

    The selected students are S01 and S04, so the number of returned rows is 2. The outer NOT EXISTS condition is true exactly when the inner EXCEPT result is empty, i.e., when the student has taken every D01 course.

    Reviewed by GateIQ Editorial Team: IIT & IISc Alumni.