IAE Academy

GATE 2025 CS2 Q.54: solution and answer

GATE 2025 · CS2Q.54 · 2 marks · MSQ

Consider the following relational schema:

Students (rollno: integer, name: string, age: integer, cgpa: real)
Courses (courseno: integer, cname: string, credits: integer)
Enrolled (rollno: integer, courseno: integer, grade: string)

(Primary keys underlined: rollno in Students; courseno in Courses; (rollno, courseno) in Enrolled.)

Which of the following options is/are correct SQL query/queries to retrieve the names of the students enrolled in course number (i.e., courseno) 1470?

  1. (A)
    SELECT S.name
    FROM Students S
    WHERE EXISTS (SELECT * FROM Enrolled E
                  WHERE E.courseno = 1470
                  AND E.rollno = S.rollno);
  2. (B)
    SELECT S.name
    FROM Students S
    WHERE SIZEOF (SELECT * FROM Enrolled E
                  WHERE E.courseno = 1470
                  AND E.rollno = S.rollno) > 0;
  3. (C)
    SELECT S.name
    FROM Students S
    WHERE  0 < (SELECT COUNT(*)
                FROM Enrolled E
                WHERE E.courseno = 1470
                AND E.rollno = S.rollno);
  4. (D)
    SELECT S.name
    FROM Students S NATURAL JOIN Enrolled E
    WHERE E.courseno = 1470;
See the question in the paper

Answer and solution

ANSWER

Options A, C and D are correct. Option B is not valid SQL, because SIZEOF does not exist.

Official answer: A, C, DSource: Official answer key, GATE 2025 CS2 (IIT Roorkee)

  1. Goal: return the names of students who have an Enrolled row with courseno = 1470. Check each option for validity and meaning.

  2. (A) The correlated subquery selects Enrolled rows with courseno = 1470 and the same rollno as the current student S. EXISTS is true exactly when at least one such row exists. This is valid and correct.

  3. (B) SIZEOF is not an SQL function or operator, so the query is rejected as a syntax error. Incorrect.

  4. (C) For each student, the subquery counts the Enrolled rows with courseno = 1470 and that student's rollno. 0 < COUNT(*) holds exactly when at least one such row exists, which is the same condition as in (A). Valid and correct.

  5. (D) NATURAL JOIN joins on all attribute names the two tables share. Students has and Enrolled has . The only shared attribute is rollno, so each student is paired with their own enrolments. The WHERE clause then keeps the rows with courseno = 1470. Valid and correct.

  6. Duplicates: is the key of Enrolled. A student therefore has at most one row for course 1470, so (A), (C) and (D) return each such student's name once. Two different students with the same name would still produce a repeated name in all three, equally.

  7. Valid and correct queries: A, C and D.

KEY IDEA

To test whether a related row exists, SQL uses EXISTS, a count compared with 0, or a join. A natural join matches on every shared column name, and here the only one is rollno.

WATCH OUT FOR

Treating SIZEOF as a standard SQL operator because it sounds like a set-size test. Another slip is doubting that NATURAL JOIN works without an explicit ON clause, even though rollno is the only shared column.

From the GATE 2025 CS2 paper. Every question of this paper · Download the full paper · All 10 SQL questions, with notes

More SQL questions