0
0
SQLquery~20 mins

Correlated subquery execution model in SQL - 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

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.