Skip to content
Official PYQ · 2022

GATE CS 2022 Q46 — Tuple Calculus and SQL

Explain with AI

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

  • Student(sNo, sName, dNo) Dept(dNo, dName)
  • Course(cNo, cName, dNo) Register(sNo, cNo)

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}

SELECT * FROM Student AS S WHERE NOT EXIST

  (SELECT cNo FROM Course WHERE dNo = “D01”

         EXCEPT

    SELECT cNo FROM Register WHERE sNo = S.sNo)

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

 
View worked solution

Answer: 2

To find the number of rows returned by the SQL query, let us analyze the query step-by-step.

Step 1: Analyze the Inner Subqueries

For a given student SS from the outer query:

  1. First subquery:
    SELECT cNo FROM Course WHERE dNo = 'D01'
    Looking at the Course table, the courses with dNo = 'D01' are: {C11,C12}\{C11, C12\}
  2. Second subquery:
    SELECT cNo FROM Register WHERE sNo = S.sNo
    This returns the set of courses registered by the current student SS.

Step 2: Understand the EXCEPT and NOT EXISTS Logic

The query evaluates:

{C11,C12}∖{Courses registered by student S}\{C11, C12\} \setminus \{\text{Courses registered by student } S\}

The outer NOT EXISTS (written as NOT EXIST in the query) condition is satisfied if and only if the result of this set difference is empty. This means:

{C11,C12}⊆{Courses registered by student S}\{C11, C12\} \subseteq \{\text{Courses registered by student } S\}

In plain English, the query selects students who have registered for all courses offered by department D01 (i.e., both C11 and C12).

Step 3: Evaluate for Each Student

Let's check the registered courses for each student from the Register table:

  • S01 (James): Registered for {C11,C12}\{C11, C12\}. Since {C11,C12}⊆{C11,C12}\{C11, C12\} \subseteq \{C11, C12\}, this student is selected.
  • S02 (Rocky): Registered for {C11}\{C11\}. Since {C11,C12}⊈{C11}\{C11, C12\} \not\subseteq \{C11\}, this student is not selected.
  • S03 (Jackson): Registered for {C21,C22,C23}\{C21, C22, C23\}. Since {C11,C12}⊈{C21,C22,C23}\{C11, C12\} \not\subseteq \{C21, C22, C23\}, this student is not selected.
  • S04 (Jane): Registered for {C11,C12}\{C11, C12\}. Since {C11,C12}⊆{C11,C12}\{C11, C12\} \subseteq \{C11, C12\}, this student is selected.
  • S05 (Milli): Registered for {C11,C21}\{C11, C21\}. Since {C11,C12}⊈{C11,C21}\{C11, C12\} \not\subseteq \{C11, C21\}, this student is not selected.

Conclusion

The query returns the rows for students S01 and S04. Therefore, the number of rows returned is 2.

Free practice · Your answers on this page are not saved.

Published
Report a correction
Keep exploringMore Tuple Calculus and SQL questions