Bird
Raised Fist0
SQLquery~5 mins

Nested subqueries in SQL - Cheat Sheet & Quick Revision

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
Recall & Review
beginner
What is a nested subquery in SQL?
A nested subquery is a query written inside another query. It helps to use the result of one query inside another query.
Click to reveal answer
beginner
Can a nested subquery return multiple rows?
Yes, a nested subquery can return multiple rows if used with operators like IN or EXISTS. But if used with =, it must return only one row.
Click to reveal answer
intermediate
How does a nested subquery help in filtering data?
A nested subquery can find a set of values or a single value that the outer query uses to filter its results, like finding employees who work in departments with more than 10 people.
Click to reveal answer
intermediate
What is the difference between a correlated and a non-correlated nested subquery?
A correlated subquery depends on the outer query for its values and runs once per row. A non-correlated subquery runs once and returns a result used by the outer query.
Click to reveal answer
beginner
Write a simple example of a nested subquery to find employees with salary above the average salary.
SELECT * FROM employees WHERE salary > (SELECT AVG(salary) FROM employees);
Click to reveal answer
What does a nested subquery do in SQL?
ACreates a new table
BRuns a query inside another query
CDeletes data from a table
DChanges the database schema
Which operator allows a nested subquery to return multiple rows?
AIN
B=
C<>
DBETWEEN
What is a correlated subquery?
AA subquery that deletes rows
BA subquery that runs once
CA subquery that creates a new table
DA subquery that depends on the outer query
Which of these is a valid use of a nested subquery?
AUPDATE employees SET salary = salary + 1000;
BDELETE FROM employees WHERE salary = 50000;
CSELECT * FROM employees WHERE salary > (SELECT AVG(salary) FROM employees);
DCREATE TABLE new_employees;
If a nested subquery returns more than one row but is used with '=', what happens?
AError occurs
BOnly first row is used
CQuery runs successfully
DRows are concatenated
Explain what a nested subquery is and give a simple example.
Think about a query inside another query.
You got /2 concepts.
    Describe the difference between correlated and non-correlated nested subqueries.
    Focus on how the subquery uses values from the outer query.
    You got /3 concepts.

      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