0
0
MySQLquery~20 mins

Correlated subqueries in MySQL - Practice Problems & Coding Challenges

Choose your learning style9 modes available
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
Given the tables Employees(emp_id, dept_id, salary) and Departments(dept_id, dept_name), what is the output of this query?

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);
A
emp_id | salary
2      | 6000
4      | 8000
B
emp_id | salary
1      | 5000
2      | 6000
5      | 5500
C
emp_id | salary
2      | 6000
3      | 7000
4      | 8000
D
emp_id | salary
1      | 5000
3      | 7000
Attempts:
2 left
💡 Hint
Think about the average salary per department and which employees earn more than their department's average.
🧠 Conceptual
intermediate
1:30remaining
Understanding correlated subquery execution
Which statement best describes how a correlated subquery is executed in SQL?
AThe subquery is executed once before the outer query and its result is reused.
BThe subquery is executed once for each row processed by the outer query.
CThe subquery is executed only if the outer query returns no rows.
DThe subquery is executed after the outer query completes.
Attempts:
2 left
💡 Hint
Think about how the subquery depends on values from the outer query.
📝 Syntax
advanced
1: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
ASELECT e1.emp_id FROM Employees e1 WHERE e1.salary > (SELECT AVG(salary) FROM Employees e2 WHERE e2.dept_id = e1.dept_id) ORDER BY e1.emp_id;
BSELECT e1.emp_id FROM Employees e1 WHERE e1.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 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.
optimization
advanced
2:30remaining
Optimizing correlated subquery for performance
You have a query with a correlated subquery that runs slowly:

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?
AUse a UNION ALL instead of the correlated subquery.
BAdd an index on Employees.emp_id to speed up the subquery.
CRewrite the query using a JOIN with a derived table that precomputes average salaries per department.
DRemove the WHERE clause to avoid the subquery.
Attempts:
2 left
💡 Hint
Think about how to avoid running the subquery for each row.
🔧 Debug
expert
3:00remaining
Debugging unexpected results from correlated subquery
Consider this query:

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?
AThe subquery compares salaries from all departments except the current one, so the condition is never true.
BThe subquery is missing a GROUP BY clause causing aggregation errors.
CThe outer query should use < instead of > to compare salaries.
DThe inequality operator != is invalid in correlated subqueries.
Attempts:
2 left
💡 Hint
Think about what the subquery returns and how it compares to the outer salary.