IAE Academy

GATE 2023 CS Q.61: solution and answer

GATE 2023 · CSQ.61 · 2 marks · NAT

Consider the following table named Student in a relational database. The primary key of this table is rollNum.

The SQL query below is executed on this database.

SELECT *
FROM Student
WHERE gender = 'F' AND
   marks > 65;

The number of rows returned by the query is ______.

GATE 2023 CS question 61: Table Student with columns rollNum (primary key, underlined), name, gender, marks and rows: (1, Naman, M, 62), (2, Aliya…Tap to open full size
Figure from the GATE 2023 CS paper. It is part of the question.
Figure description

Table Student with columns rollNum (primary key, underlined), name, gender, marks and rows: (1, Naman, M, 62), (2, Aliya, F, 70), (3, Aliya, F, 80), (4, James, M, 82), (5, Swati, F, 65).

Answer and solution

ANSWER

The query returns 2 rows (rollNum 2 and 3).

Official answer: 2Source: Official answer key, GATE 2023 CS (IIT Kanpur)

  1. The WHERE clause keeps a row only if both conditions hold: gender = 'F' and marks > 65.

  2. Filter on gender first. Rows 2, 3 and 5 have gender F. Rows 1 and 4 are M, so they drop out.

  3. Apply marks > 65 to those three rows. Row 2 has 70, which passes. Row 3 has 80, which passes. Row 5 has 65, which fails because 65 is not strictly greater than 65.

  4. Rows 2 and 3 satisfy both conditions. Their names are the same (Aliya), but SELECT * has no DISTINCT and the rows differ in rollNum, so both are returned.

  5. The query returns 2 rows.

KEY IDEA

A WHERE with AND keeps only rows that satisfy both conditions. The comparison marks > 65 is strict, so a row with exactly 65 is excluded. Without DISTINCT, rows are never merged, even if some columns repeat.

WATCH OUT FOR

Counting Swati (marks = 65) because 65 looks like it qualifies, which gives 3. Another slip is merging the two Aliya rows into one because the names match, which gives 1. Without DISTINCT both rows stay.

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

More SQL questions