Bird
Raised Fist0
SQLquery~20 mins

Correlated subquery execution model in SQL - Practice Problems & Coding Challenges

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
Challenge - 5 Problems
🎖️
Correlated Subquery Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
query_result
intermediate
2:00remaining
Output of correlated subquery with aggregation

Consider the tables Employees(emp_id, dept_id, salary) and Departments(dept_id, dept_name). What is the output of the following query?

SELECT emp_id, salary FROM Employees e WHERE salary > (SELECT AVG(salary) FROM Employees WHERE dept_id = e.dept_id);

Assume the data:

Employees: (1, 10, 5000), (2, 10, 6000), (3, 20, 4000), (4, 20, 4500)
SQL
SELECT emp_id, salary FROM Employees e WHERE salary > (SELECT AVG(salary) FROM Employees WHERE dept_id = e.dept_id);
A
emp_id: 2, salary: 6000
emp_id: 4, salary: 4500
B
emp_id: 1, salary: 5000
emp_id: 2, salary: 6000
Cemp_id: 4, salary: 4500
Demp_id: 2, salary: 6000
Attempts:
2 left
💡 Hint

Think about how the average salary is calculated per department and which employees have salaries above that average.

📝 Syntax
intermediate
2:00remaining
Identify syntax error in correlated subquery

Which option contains a syntax error in the correlated subquery?

SELECT e1.emp_id FROM Employees e1 WHERE e1.salary > (SELECT AVG(e2.salary) FROM Employees e2 WHERE e2.dept_id = e1.dept_id);
ASELECT e1.emp_id FROM Employees e1 WHERE e1.salary > (SELECT AVG(e2.salary) FROM Employees e2 WHERE e2.dept_id = e1.dept_id);
BSELECT emp_id FROM Employees e1 WHERE salary > (SELECT AVG(salary) FROM Employees e2 WHERE e2.dept_id = e1.dept_id);
CSELECT e1.emp_id FROM Employees e1 WHERE e1.salary > (SELECT AVG(salary) FROM Employees e2 WHERE e2.dept_id = e1.dept_id);
DSELECT emp_id FROM Employees WHERE salary > (SELECT AVG(salary) FROM Employees WHERE dept_id = dept_id);
Attempts:
2 left
💡 Hint

Look for missing table aliases or ambiguous column references.

optimization
advanced
2:00remaining
Optimize correlated subquery for performance

Given the query:

SELECT emp_id FROM Employees e WHERE salary > (SELECT AVG(salary) FROM Employees WHERE dept_id = e.dept_id);

Which option is the best way to optimize this query for large datasets?

ARewrite using JOIN with GROUP BY to precompute averages.
BAdd an index on Employees.salary column.
CUse DISTINCT inside the subquery to reduce duplicates.
DReplace AVG with MAX to reduce computation.
Attempts:
2 left
💡 Hint

Think about avoiding repeated subquery execution for each row.

🔧 Debug
advanced
2:00remaining
Debug unexpected empty result from correlated subquery

Why does the following query return no rows?

SELECT emp_id FROM Employees e WHERE salary > (SELECT MAX(salary) FROM Employees WHERE dept_id = e.dept_id AND emp_id <> e.emp_id);

Assume each department has only one employee.

ABecause the outer query filters out all employees by mistake.
BBecause the subquery syntax is invalid and causes error.
CBecause the subquery returns NULL, comparison with NULL yields false.
DBecause the MAX function returns zero when no rows match.
Attempts:
2 left
💡 Hint

Consider what happens when the subquery has no matching rows.

🧠 Conceptual
expert
2:00remaining
Understanding execution order of correlated subqueries

Which statement best describes how a correlated subquery executes?

AThe inner query runs once and its result is reused for all outer rows.
BThe inner query runs once for each row processed by the outer query, using values from that row.
CThe inner query runs independently of the outer query and returns a fixed result.
DThe inner query runs after the outer query completes and uses aggregated results.
Attempts:
2 left
💡 Hint

Think about how the inner query depends on the outer query's current row.

Practice

(1/5)
1. What is a correlated subquery in SQL?
easy
A. A subquery that runs independently of the outer query
B. A subquery that uses values from the outer query to filter results
C. A query that joins two tables without conditions
D. A query that only returns aggregate values

Solution

  1. Step 1: Understand subquery types

    A correlated subquery depends on the outer query's current row to run.
  2. Step 2: Identify correlation

    It uses columns from the outer query inside the subquery's WHERE clause.
  3. Final Answer:

    A subquery that uses values from the outer query to filter results -> Option B
  4. Quick Check:

    Correlated subquery = uses outer query values [OK]
Hint: Correlated subqueries reference outer query columns [OK]
Common Mistakes:
  • Thinking subquery runs once independently
  • Confusing with JOIN operations
  • Assuming it returns only aggregates
2. Which of the following is the correct syntax for a correlated subquery?
easy
A. SELECT e.name FROM employees e WHERE e.salary > (SELECT AVG(salary) FROM employees WHERE department = e.department)
B. SELECT e.name FROM employees e WHERE salary > 50000
C. SELECT e.name FROM employees e WHERE e.salary > (SELECT AVG(salary) FROM employees)
D. SELECT e.name FROM employees e JOIN departments d ON e.department = d.id

Solution

  1. Step 1: Identify correlation in subquery

    SELECT e.name FROM employees e WHERE e.salary > (SELECT AVG(salary) FROM employees WHERE department = e.department) uses 'e.department' inside the subquery, linking it to the outer query.
  2. Step 2: Check other options

    SELECT e.name FROM employees e WHERE e.salary > (SELECT AVG(salary) FROM employees) has no reference to outer query in subquery; the option with 'salary > 50000' lacks a subquery and the JOIN option is not a subquery.
  3. Final Answer:

    SELECT e.name FROM employees e WHERE e.salary > (SELECT AVG(salary) FROM employees WHERE department = e.department) -> Option A
  4. Quick Check:

    Correlation needs outer query column inside subquery [OK]
Hint: Look for outer query columns inside subquery WHERE clause [OK]
Common Mistakes:
  • Missing outer query reference inside subquery
  • Confusing JOIN with subquery
  • Using subquery without correlation
3. Given the tables employees(id, name, department, salary) and the query:
SELECT e1.name FROM employees e1 WHERE e1.salary > (SELECT AVG(e2.salary) FROM employees e2 WHERE e2.department = e1.department);

What does this query return?
medium
A. Employees whose salary is above the average salary of their own department
B. Employees whose salary is above the average salary of all employees
C. Employees with salary above 50000
D. All employees regardless of salary

Solution

  1. Step 1: Understand the subquery correlation

    The subquery calculates average salary for the department of the current employee (e1.department).
  2. Step 2: Compare salaries

    The outer query selects employees whose salary is greater than that department average.
  3. Final Answer:

    Employees whose salary is above the average salary of their own department -> Option A
  4. Quick Check:

    Salary > department average = Employees whose salary is above the average salary of their own department [OK]
Hint: Check which outer column is used inside subquery condition [OK]
Common Mistakes:
  • Assuming average is for all employees
  • Ignoring the correlation condition
  • Confusing with simple WHERE salary > value
4. Identify the error in the following correlated subquery:
SELECT c.customer_id FROM customers c WHERE c.orders_count > (SELECT AVG(o.orders_count) FROM orders o WHERE o.customer_id = c.customer_id);
medium
A. The subquery will return multiple rows causing an error
B. The subquery uses a wrong table alias 'o' which is not defined
C. The subquery compares orders_count incorrectly; should use SUM instead of AVG
D. The subquery references the outer query correctly; no error

Solution

  1. Step 1: Analyze correlation

    The subquery correctly uses 'c.customer_id' from the outer query in its WHERE clause.
  2. Step 2: Check aggregation and output

    AVG(o.orders_count) is an aggregate that returns a single scalar value, even for customers with multiple orders.
  3. Final Answer:

    The subquery references the outer query correctly; no error -> Option D
  4. Quick Check:

    Subquery must return single value for comparison [OK]
Hint: Ensure subquery returns one value for comparison [OK]
Common Mistakes:
  • Thinking AVG returns multiple rows without GROUP BY
  • Assuming alias 'o' is undefined
  • Believing SUM is needed instead of AVG
5. You want to find all products whose price is higher than the average price of products in the same category. Which query correctly uses a correlated subquery to achieve this?
hard
A. SELECT p.product_name FROM products p JOIN categories c ON p.category = c.id WHERE p.price > c.avg_price
B. SELECT p.product_name FROM products p WHERE p.price > (SELECT AVG(price) FROM products)
C. SELECT p.product_name FROM products p WHERE p.price > (SELECT AVG(price) FROM products WHERE category = p.category)
D. SELECT product_name FROM products WHERE price > ALL (SELECT price FROM products)

Solution

  1. Step 1: Identify correlation condition

    SELECT p.product_name FROM products p WHERE p.price > (SELECT AVG(price) FROM products WHERE category = p.category) uses 'p.category' inside the subquery to calculate average price per category, correlating outer and inner queries.
  2. Step 2: Verify other options

    SELECT p.product_name FROM products p WHERE p.price > (SELECT AVG(price) FROM products) compares to overall average, not per category; the option using JOIN with categories uses a JOIN but no subquery; the option using > ALL compares to all individual prices, not average.
  3. Final Answer:

    SELECT p.product_name FROM products p WHERE p.price > (SELECT AVG(price) FROM products WHERE category = p.category) -> Option C
  4. Quick Check:

    Correlated subquery filters by category [OK]
Hint: Use outer query column inside subquery WHERE for correlation [OK]
Common Mistakes:
  • Using overall average instead of per category
  • Confusing JOIN with subquery
  • Using ALL instead of AVG in subquery