Bird
Raised Fist0
SQLquery~5 mins

Nested subqueries in SQL

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Introduction
Nested subqueries let you use one query inside another to find data step-by-step, like asking a question within a question.
When you want to filter results based on a condition that depends on another query.
When you need to find records related to the results of a different query.
When you want to compare values against a set of results from another query.
When you want to calculate something first and then use that result to get final data.
Syntax
SQL
SELECT column1, column2
FROM table1
WHERE column3 IN (
    SELECT column3
    FROM table2
    WHERE condition
);
The inner query (subquery) runs first and its result is used by the outer query.
Subqueries can be placed in WHERE, FROM, or SELECT clauses.
Examples
Find employees who work in departments located in New York.
SQL
SELECT name
FROM employees
WHERE department_id IN (
    SELECT id
    FROM departments
    WHERE location = 'New York'
);
Find products priced higher than the average price of all products.
SQL
SELECT product_name
FROM products
WHERE price > (
    SELECT AVG(price)
    FROM products
);
Find the customer who made order number 101.
SQL
SELECT customer_name
FROM customers
WHERE id = (
    SELECT customer_id
    FROM orders
    WHERE order_id = 101
);
Sample Program
This query finds employees whose salary is higher than the average salary of all employees.
SQL
SELECT employee_name
FROM employees
WHERE salary > (
    SELECT AVG(salary)
    FROM employees
);
OutputSuccess
Important Notes
Make sure the subquery returns a single column when used with IN or = operators.
If the subquery returns multiple rows but you use =, it will cause an error; use IN instead.
Nested subqueries can sometimes be replaced with JOINs for better performance.
Summary
Nested subqueries let you use one query inside another to filter or compare data.
They run the inner query first, then use its result in the outer query.
Use them to answer complex questions step-by-step in your database.

Practice

(1/5)
1. What does a nested subquery in SQL do?
easy
A. Runs a query inside another query to filter or compare data
B. Creates a new table from existing data
C. Deletes data from multiple tables at once
D. Updates all rows in a table without conditions

Solution

  1. Step 1: Understand the concept of nested subqueries

    A nested subquery is a query inside another query that runs first to provide data for the outer query.
  2. Step 2: Identify the correct description

    Runs a query inside another query to filter or compare data correctly describes this behavior as running a query inside another to filter or compare data.
  3. Final Answer:

    Runs a query inside another query to filter or compare data -> Option A
  4. Quick Check:

    Nested subquery = query inside query [OK]
Hint: Nested means one query inside another [OK]
Common Mistakes:
  • Confusing nested subquery with table creation
  • Thinking nested subqueries delete data
  • Assuming nested subqueries update all rows blindly
2. Which of the following is the correct syntax for a nested subquery in SQL?
easy
A. SELECT * FROM table WHERE id IN SELECT id FROM table2 WHERE value = 10;
B. SELECT * FROM table WHERE id == (SELECT id FROM table2 WHERE value = 10);
C. SELECT * FROM table WHERE id = SELECT id FROM table2 WHERE value = 10;
D. SELECT * FROM table WHERE id = (SELECT id FROM table2 WHERE value = 10);

Solution

  1. Step 1: Review correct nested subquery syntax

    The inner query must be enclosed in parentheses and used with operators like = or IN.
  2. Step 2: Check each option

    SELECT * FROM table WHERE id = (SELECT id FROM table2 WHERE value = 10); uses parentheses correctly and equals operator, making it valid SQL syntax.
  3. Final Answer:

    SELECT * FROM table WHERE id = (SELECT id FROM table2 WHERE value = 10); -> Option D
  4. Quick Check:

    Nested subquery syntax uses parentheses [OK]
Hint: Always use parentheses around subqueries [OK]
Common Mistakes:
  • Missing parentheses around subquery
  • Using double equals (==) instead of single =
  • Omitting parentheses causing syntax errors
3. Given the tables:
Employees(emp_id, name, dept_id)
Departments(dept_id, dept_name)
What does this query return?
SELECT name FROM Employees WHERE dept_id = (SELECT dept_id FROM Departments WHERE dept_name = 'Sales');
medium
A. Names of employees who work in the Sales department
B. All employee names regardless of department
C. Department names where employees work
D. Employee names who do not work in Sales

Solution

  1. Step 1: Understand the inner query

    The inner query finds the dept_id for the 'Sales' department from Departments table.
  2. Step 2: Apply the outer query condition

    The outer query selects employee names whose dept_id matches the Sales dept_id found by the inner query.
  3. Final Answer:

    Names of employees who work in the Sales department -> Option A
  4. Quick Check:

    Inner query finds Sales dept_id, outer filters employees [OK]
Hint: Inner query finds filter value, outer applies it [OK]
Common Mistakes:
  • Thinking it returns all employees
  • Confusing employee names with department names
  • Assuming it returns employees not in Sales
4. Identify the error in this SQL query:
SELECT name FROM Employees WHERE dept_id = SELECT dept_id FROM Departments WHERE dept_name = 'HR';
medium
A. Using = instead of IN for subquery
B. Missing parentheses around the subquery
C. Wrong table name used in subquery
D. Subquery returns multiple columns

Solution

  1. Step 1: Check subquery syntax

    The subquery must be enclosed in parentheses to be valid inside WHERE clause.
  2. Step 2: Identify the missing parentheses

    The query lacks parentheses around the subquery, causing syntax error.
  3. Final Answer:

    Missing parentheses around the subquery -> Option B
  4. Quick Check:

    Subqueries need parentheses [OK]
Hint: Always wrap subqueries in parentheses [OK]
Common Mistakes:
  • Forgetting parentheses around subquery
  • Using wrong operator without parentheses
  • Assuming subquery syntax is optional
5. You want to find all customers who placed orders with amounts greater than the average order amount. Which query correctly uses a nested subquery to achieve this?
hard
A. SELECT customer_id FROM Orders WHERE amount < (SELECT AVG(amount) FROM Orders);
B. SELECT customer_id FROM Orders WHERE amount = (SELECT AVG(amount) FROM Orders);
C. SELECT customer_id FROM Orders WHERE amount > (SELECT AVG(amount) FROM Orders);
D. SELECT customer_id FROM Orders WHERE amount IN (SELECT AVG(amount) FROM Orders);

Solution

  1. Step 1: Understand the goal

    We want customers with orders greater than the average order amount.
  2. Step 2: Analyze each option's condition

    SELECT customer_id FROM Orders WHERE amount > (SELECT AVG(amount) FROM Orders); uses > with a subquery calculating average amount, correctly filtering orders above average.
  3. Final Answer:

    SELECT customer_id FROM Orders WHERE amount > (SELECT AVG(amount) FROM Orders); -> Option C
  4. Quick Check:

    Use > with AVG subquery to find above-average orders [OK]
Hint: Compare with (SELECT AVG(...)) using > for above average [OK]
Common Mistakes:
  • Using = instead of > to find above average
  • Using < which finds below average
  • Using IN with a single value subquery incorrectly