Subqueries & CTEs · Question 8 · Medium

Subqueries & CTEs question 8

Table Members:
mid | name
1 | Geeta
2 | Hari
3 | Imran
4 | Jaya

Table Loans:
loan_id | mid | book
1 | 1 | Wings
2 | 1 | Malgudi
3 | 3 | Godan
4 | NULL | Ignited

One loan has no member recorded. Which names does this query return?

SELECT name
FROM Members m
WHERE NOT EXISTS (SELECT 1 FROM Loans l
                  WHERE l.mid = m.mid)
ORDER BY name;
  1. No rows
  2. Geeta; Imran
  3. Hari; Jaya
  4. Geeta; Hari; Imran; Jaya
Answer and explanation

Answer: C. Hari; Jaya

Explanation: For each member the subquery looks for a loan with the same mid. None exists for Hari and Jaya, so NOT EXISTS is true for them. The loan with a NULL mid simply matches nobody; it does not empty the result as it would with NOT IN.

Previous: Table Members: mid | name 1 | Geeta 2 | Hari 3 | Imran 4 | Jaya Table Loans… · Next: Table Faculty: fid | name | dept | salary 1 | Anand | CSE | 90000 2 | Bhavna |…

Practise subqueries & ctes

Try a fresh set of subqueries & ctes questions with an explanation after every answer, free and without signing up: Subqueries & CTEs practice. Or sit a full placement mock test.

More subqueries & ctes questions

More sql practice

Window Functions · NULL Handling · SQL Functions & CASE · UNION, INTERSECT & EXCEPT · Tables, Keys & Constraints · DML & Transactions · SELECT, WHERE & ORDER BY · SQL Joins

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