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:
- Correlated subquery
- Derived table
- Common table expression
- 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;
- No rows
- Geeta; Imran
- Hari; Jaya
- 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?
- SELECT name FROM Members WHERE mid NOT IN (SELECT mid FROM Loans);
- SELECT name FROM Members WHERE mid IN (SELECT mid FROM Loans WHERE mid IS NULL);
- SELECT m.name FROM Members m JOIN Loans l ON m.mid = l.mid WHERE l.mid IS NULL;
- 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
- Table Faculty: fid | name | dept | salary 1 | Anand | CSE | 90000 2 | Bhavna |…
- Table Members: mid | name 1 | Geeta 2 | Hari 3 | Imran 4 | Jaya Table Loans…
- A subquery that returns exactly one row with one column, so it can be used…
- Table Faculty: fid | name | dept | salary 1 | Anand | CSE | 90000 2 | Bhavna |…
- Which keyword starts a common table expression (CTE), a named result that the…
- Table Faculty: fid | name | dept | salary 1 | Anand | CSE | 90000 2 | Bhavna |…
- Table Members: mid | name 1 | Geeta 2 | Hari 3 | Imran 4 | Jaya Table Loans…
- Table Members: mid | name 1 | Geeta 2 | Hari 3 | Imran 4 | Jaya Table Loans…
- Table Faculty: fid | name | dept | salary 1 | Anand | CSE | 90000 2 | Bhavna |…
- Table Faculty: fid | name | dept | salary 1 | Anand | CSE | 90000 2 | Bhavna |…
- Table Faculty: fid | name | dept | salary 1 | Anand | CSE | 90000 2 | Bhavna |…
- Table Faculty: fid | name | dept | salary 1 | Anand | CSE | 90000 2 | Bhavna |…
- Table Members: mid | name 1 | Geeta 2 | Hari 3 | Imran 4 | Jaya Table Loans…
- Table Marks: student | subject | score Kavya | Maths | 88 Kavya | Physics | 70…
- In the query below, how is the inner query logically evaluated?
- Table Marks: student | subject | score Kavya | Maths | 88 Kavya | Physics | 70…
- Table Marks: student | subject | score Kavya | Maths | 88 Kavya | Physics | 70…
- Table Faculty: fid | name | dept | salary 1 | Anand | CSE | 90000 2 | Bhavna |…
- Table Members: mid | name 1 | Geeta 2 | Hari 3 | Imran 4 | Jaya Table Loans…
- Tables Members(mid, name) and Loans(loan_id, mid, book) are given, and…
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
- SELECT, WHERE & ORDER BY: Filtering, DISTINCT, IN, BETWEEN, LIKE, sorting and LIMIT.
- SQL Joins: Inner, left, right, full, cross and self joins, and row counts.
- Aggregates & GROUP BY: COUNT, SUM, AVG, grouping and HAVING versus WHERE.
- Window Functions: ROW_NUMBER, RANK, DENSE_RANK, PARTITION BY, LAG and running totals.
- NULL Handling: Three-valued logic, IS NULL, COALESCE and NULL in aggregates.
- SQL Functions & CASE: CASE WHEN, conditional aggregation, string and numeric functions.
- UNION, INTERSECT & EXCEPT: Combining results, duplicates and column rules.
- Tables, Keys & Constraints: CREATE, ALTER, primary and foreign keys, UNIQUE and CHECK.
- DML & Transactions: INSERT, UPDATE, DELETE, COMMIT, ROLLBACK and ACID.
Other subjects
- Quantitative Aptitude: Arithmetic, percentages, time and work, speed, probability and data interpretation: the numerical section of every fresher test.
- Logical Reasoning: Series, coding-decoding, arrangements, syllogisms and puzzles: the section that separates prepared candidates from the rest.
- Verbal Ability: Vocabulary, grammar, reading comprehension and sentence order: tested in nearly every IT services hiring exam.
- Technical & Programming: CS fundamentals, output prediction and pseudo-code: the technical MCQ round for IT roles.
- Python: Python quizzes by topic, from types and strings to classes and generators: output prediction and interview concepts.
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