Subqueries & CTEs questions and answers

Scalar and correlated subqueries, IN, EXISTS and WITH. Practise subqueries & ctes questions from the sql section of placement tests, with a worked explanation after every answer. Practice is free and needs no sign-up.

Sample subqueries & ctes questions with worked answers

Question 1

A subquery that returns exactly one row with one column, so it can be used wherever a single value is expected, is called a:

  1. Correlated subquery
  2. Derived table
  3. Common table expression
  4. Scalar subquery

Answer: D. Scalar subquery

Explanation: A scalar subquery produces a single value, as in salary > (SELECT AVG(salary) FROM Faculty). A derived table is a subquery in FROM, and a correlated subquery is one that refers to the outer query.

Question 2

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: 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.

Question 3

Tables Members(mid, name) and Loans(loan_id, mid, book) are given, and Loans.mid can be NULL. Which query reliably returns the members who have never taken a loan?

  1. SELECT name FROM Members WHERE mid NOT IN (SELECT mid FROM Loans);
  2. SELECT name FROM Members WHERE mid IN (SELECT mid FROM Loans WHERE mid IS NULL);
  3. SELECT m.name FROM Members m JOIN Loans l ON m.mid = l.mid WHERE l.mid IS NULL;
  4. SELECT name FROM Members m WHERE NOT EXISTS (SELECT 1 FROM Loans l WHERE l.mid = m.mid);

Answer: D. SELECT name FROM Members m WHERE NOT EXISTS (SELECT 1 FROM Loans l WHERE l.mid = m.mid);

Explanation: If the subquery returns even one NULL, x NOT IN (...) can never be true, so the NOT IN version returns no rows. NOT EXISTS only checks whether a matching row exists and is not affected by NULLs. The inner join version has no unmatched rows to test.

All 20 subqueries & ctes questions with answers

Subqueries & CTEs: common questions

What are subqueries & ctes questions?

Scalar and correlated subqueries, IN, EXISTS and WITH. They appear in the sql section of many placement tests.

Where can I practise subqueries & ctes questions with answers for free?

Right here on LogicWiz Exams. Every subqueries & ctes question has a worked explanation after you answer. Practice is free and needs no sign-up.

How should I practise subqueries & ctes?

Start with easy questions, then move to medium and hard. Read the explanation after each answer, and come back to the ones you missed.

Start practising

On this page you can start a fresh set of subqueries & ctes questions, choose easy, medium or hard, and time yourself if you want to. Or practise the whole subject: SQL practice.

More sql topics

Other subjects

LogicWiz Exams home · All practice topics · Placement mock tests · Daily 10 challenge · Test from a job description · LogicWiz Open: free team quiz · Free interview prep