NULL Handling · Question 16 · Hard

NULL Handling question 16

Table Student:
roll | name
1 | Irfan
2 | Jaya
3 | Kunal
4 | Lata

Table Hostel:
room | roll
101 | 1
102 | NULL
103 | 3

Table Hostel lists room allotments; room 102 is not yet allotted. Which names does this query return?

SELECT s.name
FROM Student s
WHERE NOT EXISTS (SELECT 1 FROM Hostel h
                  WHERE h.roll = s.roll)
ORDER BY s.name;
  1. Jaya; Lata
  2. No rows
  3. Irfan; Kunal
  4. Jaya; Kunal; Lata
Answer and explanation

Answer: A. Jaya; Lata

Explanation: NOT EXISTS only asks whether the subquery finds a matching row. Rolls 1 and 3 have a match; for rolls 2 and 4 no Hostel row makes h.roll = s.roll true (the NULL row never matches), so Jaya and Lata are returned. The same question written with NOT IN would return no rows because of the NULL.

Previous: A table Voter has 10 rows, and the city column is NULL in exactly 3 of them… · Next: Table Staff: id | name | dept | bonus 1 | Anita | HR | 5000 2 | Bhavesh | IT |…

Practise null handling

Try a fresh set of null handling questions with an explanation after every answer, free and without signing up: NULL Handling practice. Or sit a full placement mock test.

More null handling questions

More sql practice

SQL Functions & CASE · UNION, INTERSECT & EXCEPT · Tables, Keys & Constraints · DML & Transactions · SELECT, WHERE & ORDER BY · SQL Joins · Aggregates & GROUP BY · Subqueries & CTEs

LogicWiz Exams home · All practice topics · Placement mock tests · Daily 10 challenge