Subqueries & CTEs · Question 13 · Medium

Subqueries & CTEs question 13

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

What does this query return?

SELECT m.name,
       (SELECT COUNT(*) FROM Loans l WHERE l.mid = m.mid)
FROM Members m
ORDER BY m.name;
  1. Geeta, 2; Hari, 0; Imran, 1; Jaya, 0
  2. Geeta, 2; Imran, 1
  3. Geeta, 2; Hari, NULL; Imran, 1; Jaya, NULL
  4. Geeta, 4; Hari, 4; Imran, 4; Jaya, 4
Answer and explanation

Answer: A. Geeta, 2; Hari, 0; Imran, 1; Jaya, 0

Explanation: The subquery in the SELECT list is evaluated for each member and counts that member's loans. Every member stays in the result, and COUNT(*) over no rows is 0, not NULL.

Previous: Table Faculty: fid | name | dept | salary 1 | Anand | CSE | 90000 2 | Bhavna |… · Next: Table Marks: student | subject | score Kavya | Maths | 88 Kavya | Physics | 70…

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