Subqueries Questions with Answers

7 practice questions on Subqueries (SQL) with answers and step-by-step solutions. Try 7 samples below, then practise all 7 free in the AptiNest app.

Sample questions

Question 1Easy

What is a correlated subquery?

  1. Any subquery that is written in the FROM clause as a derived table
  2. A subquery that joins two tables before the outer query runs
  3. A subquery that refers to columns of the outer query's current row
  4. A subquery that is guaranteed to return exactly one row and column

Answer: C. A subquery that refers to columns of the outer query's current row

Explanation

A correlated subquery depends on the current outer row, as in WHERE EXISTS (SELECT 1 FROM orders o WHERE o.customer_id = c.id).

Step-by-step solution

  1. A correlated subquery refers to a column of the outer query.
  2. It runs once for each outer row, using that row's values.
  3. So option C is the definition.

Why the other options are wrong

  • A. A derived table in FROM does not depend on the outer row.
  • B. A join before the outer query is not correlation.
  • D. A subquery returning one value is a scalar subquery, not necessarily correlated.

Tip: If the inner query uses an alias from the outer query, it is correlated.

Question 2Medium

What does this query return?

employees
id | name  | dept  | salary | manager_id
1  | Asha  | Sales | 60000  | NULL
2  | Ravi  | Sales | 45000  | 1
3  | Meera | HR    | 50000  | 1
4  | John  | IT    | 70000  | NULL
5  | Kiran | IT    | 45000  | 4
SELECT MAX(salary) FROM employees
WHERE salary < (SELECT MAX(salary) FROM employees);
  1. 50000
  2. 45000
  3. 60000
  4. 70000

Answer: C. 60000

Explanation

The subquery finds the highest salary (70000), and the outer query takes the maximum of the salaries below it: the second-highest distinct salary, 60000.

Step-by-step solution

  1. The subquery gives the maximum salary, 70000 (John).
  2. The outer query keeps salaries below 70000: 60000, 45000, 50000, 45000.
  3. The largest of these is 60000 (option C).

Why the other options are wrong

  • A. 50000 is Meera's salary, the third highest.
  • B. 45000 is the lowest salary, not the second highest.
  • D. 70000 is the maximum, and the strict < excludes it.

Tip: Second-highest: MAX of values strictly below the overall MAX.

Question 3Medium

Which names does this query return?

employees
id | name  | dept  | salary | manager_id
1  | Asha  | Sales | 60000  | NULL
2  | Ravi  | Sales | 45000  | 1
3  | Meera | HR    | 50000  | 1
4  | John  | IT    | 70000  | NULL
5  | Kiran | IT    | 45000  | 4
SELECT name FROM employees
WHERE salary > (SELECT AVG(salary) FROM employees)
ORDER BY name;
  1. Asha, John, Meera
  2. Asha, Meera
  3. John
  4. Asha, John

Answer: D. Asha, John

Explanation

The average is (60000 + 45000 + 50000 + 70000 + 45000) / 5 = 54000. Only Asha and John earn more.

Step-by-step solution

  1. Average salary: (60000 + 45000 + 50000 + 70000 + 45000) ÷ 5 = 270000 ÷ 5 = 54000.
  2. Salaries above 54000: Asha 60000 and John 70000.
  3. Sorted by name: Asha, John (option D).

Why the other options are wrong

  • A. Meera's 50000 is below 54000, so she is not returned.
  • B. Meera is below the average, and John (70000) is missing.
  • C. Asha (60000) is also above 54000, so she is missing.

Tip: A scalar subquery in WHERE compares every row with one value computed once.

Question 4Medium

What does this query return?

employees
id | name  | dept  | salary | manager_id
1  | Asha  | Sales | 60000  | NULL
2  | Ravi  | Sales | 45000  | 1
3  | Meera | HR    | 50000  | 1
4  | John  | IT    | 70000  | NULL
5  | Kiran | IT    | 45000  | 4
SELECT COUNT(*) FROM (
  SELECT dept FROM employees
  GROUP BY dept
  HAVING AVG(salary) > 50000
) AS d;
  1. 0
  2. 1
  3. 3
  4. 2

Answer: D. 2

Explanation

The derived table holds departments with average salary above 50000: Sales (52500) and IT (57500). HR averages exactly 50000 and is excluded.

Step-by-step solution

  1. Sales average: (60000 + 45000) ÷ 2 = 52500, above 50000.
  2. HR average is 50000, which is not above 50000. IT average: (70000 + 45000) ÷ 2 = 57500, above 50000.
  3. Two departments pass, so COUNT(*) is 2 (option D).

Why the other options are wrong

  • A. Sales (52500) and IT (57500) both pass, so the count is not 0.
  • B. Two departments pass, not one.
  • C. HR averages exactly 50000, which fails > 50000, so there are 2, not 3.

Tip: HAVING on AVG keeps groups above a threshold; a group equal to the threshold is excluded.

Question 5Medium

Which names does this query return?

customers
id | name
1  | Asha
2  | Ravi
3  | Meera
orders
id  | customer_id | amount
101 | 1           | 250
102 | 1           | 80
103 | 2           | 120
104 | 5           | 60
SELECT name FROM customers c
WHERE EXISTS (
  SELECT 1 FROM orders o WHERE o.customer_id = c.id
)
ORDER BY name;
  1. Asha, Ravi
  2. Asha
  3. Meera
  4. Asha, Meera, Ravi

Answer: A. Asha, Ravi

Explanation

EXISTS is true for customers with at least one matching order. Asha and Ravi have orders and Meera does not.

Step-by-step solution

  1. EXISTS is TRUE when the correlated query finds at least one order.
  2. Asha has orders 101 and 102; Ravi has order 103. Meera has none.
  3. Sorted by name: Asha, Ravi (option A).

Why the other options are wrong

  • B. Ravi also has an order (103), so he must be returned.
  • C. Meera has no orders, so she is excluded.
  • D. Meera has no orders, so she is excluded.

Tip: EXISTS checks whether a matching row exists; it does not return the order rows.

Question 6Medium

In standard SQL, what is wrong with WHERE id = (SELECT customer_id FROM orders) when orders has several rows?

  1. It must be written with == because the right side is a subquery
  2. Subqueries are not allowed in a WHERE clause in standard SQL
  3. = needs a single value, so several rows cause an error; use IN
  4. Nothing; = automatically compares against every row the subquery returns

Answer: C. = needs a single value, so several rows cause an error; use IN

Explanation

A scalar comparison needs a subquery that returns at most one row. For a set of values use IN, or = ANY.

Step-by-step solution

  1. The = operator compares one value with one value.
  2. A subquery with several rows gives several values, so the comparison has no single answer and the query fails.
  3. Use IN for a set of values (option C).

Why the other options are wrong

  • A. Standard SQL uses a single = sign; == is not the SQL comparison operator.
  • B. Subqueries are allowed in WHERE in standard SQL.
  • D. = does not compare against every row; several values cause an error.

Tip: Use IN (or = ANY) when a subquery returns several values.

Question 7Hard

How many rows does this query return?

employees
id | name  | dept  | salary | manager_id
1  | Asha  | Sales | 60000  | NULL
2  | Ravi  | Sales | 45000  | 1
3  | Meera | HR    | 50000  | 1
4  | John  | IT    | 70000  | NULL
5  | Kiran | IT    | 45000  | 4
SELECT name FROM employees
WHERE id NOT IN (SELECT manager_id FROM employees);
  1. 3
  2. 5
  3. 0
  4. 2

Answer: C. 0

Explanation

The subquery contains NULLs. x NOT IN (1, 4, NULL) can never be TRUE: it is FALSE or UNKNOWN. So no rows are returned. Use NOT EXISTS, or filter WHERE manager_id IS NOT NULL in the subquery.

Step-by-step solution

  1. The subquery returns manager_id values including NULL (Asha and John have NULL).
  2. NOT IN is never TRUE when the list contains a NULL.
  3. So no rows are returned: 0 (option C).

Why the other options are wrong

  • A. Three would require the NULL to be ignored, but NOT IN returns no rows here.
  • B. Five ignores the NULL problem; every row fails the test.
  • D. Two is the number of employees with NULL manager_id, not the NOT IN result.

Tip: Filter NULLs out of a NOT IN subquery, or use NOT EXISTS.

Practise all 7 questions free

The full question bank with timed practice, progress tracking and Vidya, your AI tutor. Free, no ads.

Open this topic in the app →

Related topics

See all SQL topics · All topics