Challenge - 5 Problems
Correlated Subquery Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ query_result
intermediate2:00remaining
Output of correlated subquery with aggregation
Given the tables Employees(emp_id, dept_id, salary) and Departments(dept_id, dept_name), what is the output of this query?
Assume the following data:
Employees
emp_id | dept_id | salary
1 | 10 | 5000
2 | 10 | 6000
3 | 20 | 7000
4 | 20 | 8000
5 | 10 | 5500
What rows will be returned?
SELECT e.emp_id, e.salary FROM Employees e WHERE e.salary > (SELECT AVG(salary) FROM Employees WHERE dept_id = e.dept_id);
Assume the following data:
Employees
emp_id | dept_id | salary
1 | 10 | 5000
2 | 10 | 6000
3 | 20 | 7000
4 | 20 | 8000
5 | 10 | 5500
What rows will be returned?
MySQL
SELECT e.emp_id, e.salary FROM Employees e WHERE e.salary > (SELECT AVG(salary) FROM Employees WHERE dept_id = e.dept_id);
Attempts:
2 left
💡 Hint
Think about the average salary per department and which employees earn more than their department's average.
✗ Incorrect
The subquery calculates the average salary for the employee's department. For dept 10, average is (5000+6000+5500)/3 = 5500. Employees with salary > 5500 are emp_id 2 (6000). For dept 20, average is (7000+8000)/2 = 7500. Employee with salary > 7500 is emp_id 4 (8000).
🧠 Conceptual
intermediate1:30remaining
Understanding correlated subquery execution
Which statement best describes how a correlated subquery is executed in SQL?
Attempts:
2 left
💡 Hint
Think about how the subquery depends on values from the outer query.
✗ Incorrect
A correlated subquery references columns from the outer query, so it must be executed once per outer row to evaluate the condition.
📝 Syntax
advanced1:30remaining
Identify the syntax error in correlated subquery
Which option contains a syntax error in the correlated subquery?
MySQL
SELECT e1.emp_id FROM Employees e1 WHERE e1.salary > (SELECT AVG(salary) FROM Employees e2 WHERE e2.dept_id = e1.dept_id
Attempts:
2 left
💡 Hint
Check for missing parentheses or semicolons.
✗ Incorrect
Option D is missing the closing parenthesis for the subquery, causing a syntax error.
❓ optimization
advanced2:30remaining
Optimizing correlated subquery for performance
You have a query with a correlated subquery that runs slowly:
Which approach is likely to improve performance?
SELECT e.emp_id FROM Employees e WHERE e.salary > (SELECT AVG(salary) FROM Employees WHERE dept_id = e.dept_id);
Which approach is likely to improve performance?
Attempts:
2 left
💡 Hint
Think about how to avoid running the subquery for each row.
✗ Incorrect
Precomputing averages per department in a derived table and joining avoids repeated subquery execution, improving performance.
🔧 Debug
expert3:00remaining
Debugging unexpected results from correlated subquery
Consider this query:
It returns no rows, but you expect some. What is the most likely reason?
SELECT e1.emp_id, e1.salary FROM Employees e1 WHERE e1.salary > (SELECT MAX(salary) FROM Employees e2 WHERE e2.dept_id != e1.dept_id);
It returns no rows, but you expect some. What is the most likely reason?
Attempts:
2 left
💡 Hint
Think about what the subquery returns and how it compares to the outer salary.
✗ Incorrect
The subquery returns the maximum salary from all other departments. Since this is a single maximum value, it's likely higher than any salary in the current department, so no rows satisfy salary > that max.