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 filtering employees by department average salary
Consider the tables employees(emp_id, name, dept_id, salary) and departments(dept_id, dept_name). What is the output of the following query?
SELECT name, salary FROM employees e WHERE salary > (SELECT AVG(salary) FROM employees WHERE dept_id = e.dept_id);
PostgreSQL
CREATE TABLE employees(emp_id INT, name TEXT, dept_id INT, salary INT); INSERT INTO employees VALUES (1, 'Alice', 10, 5000), (2, 'Bob', 10, 6000), (3, 'Charlie', 20, 4000), (4, 'Diana', 20, 4500), (5, 'Eve', 10, 5500); SELECT name, salary FROM employees e WHERE salary > (SELECT AVG(salary) FROM employees WHERE dept_id = e.dept_id);
Attempts:
2 left
💡 Hint
Think about how the subquery calculates the average salary per department and compares each employee's salary to it.
✗ Incorrect
The subquery calculates the average salary for each employee's department. For dept_id 10, average is (5000+6000+5500)/3 = 5500; Bob (6000 > 5500) yes, Alice (5000 < 5500) no, Eve (5500 = 5500) no. For dept_id 20, average is (4000+4500)/2 = 4250; Diana (4500 > 4250) yes, Charlie (4000 < 4250) no. Output is Bob and Diana, matching option D.
🧠 Conceptual
intermediate1:30remaining
Understanding correlated subquery execution frequency
In a correlated subquery, how often is the inner subquery executed during the execution of the outer query?
Attempts:
2 left
💡 Hint
Think about how the inner query depends on values from the outer query.
✗ Incorrect
A correlated subquery references columns from the outer query, so it must be re-evaluated for each row of the outer query to get the correct result.
📝 Syntax
advanced1:30remaining
Identify the syntax error in correlated subquery
Which option contains a syntax error in the correlated subquery usage?
PostgreSQL
SELECT e1.name FROM employees e1 WHERE salary > (SELECT AVG(salary) FROM employees e2 WHERE e2.dept_id = e1.dept_id);
Attempts:
2 left
💡 Hint
Check the comparison operator used inside the subquery.
✗ Incorrect
In SQL, the equality operator is a single equals sign '='. Using '==' causes a syntax error.
❓ optimization
advanced2:00remaining
Optimizing correlated subqueries with EXISTS
Which query is more efficient for checking if an employee's department has any employees with salary greater than 7000?
Attempts:
2 left
💡 Hint
EXISTS stops scanning as soon as it finds a matching row, COUNT(*) counts all matching rows.
✗ Incorrect
EXISTS is more efficient because it returns true as soon as it finds one matching row, avoiding counting all rows. COUNT(*) counts all rows, which is slower. Option C filters employees with salary > 7000, not checking department. Option C uses IN which can be less efficient and may return duplicates.
🔧 Debug
expert2:30remaining
Debugging unexpected results in correlated subquery with NULLs
Given the query:
Some employees with NULL salaries are missing from the results unexpectedly. What is the cause?
SELECT name FROM employees e WHERE salary > (SELECT AVG(salary) FROM employees WHERE dept_id = e.dept_id);
Some employees with NULL salaries are missing from the results unexpectedly. What is the cause?
Attempts:
2 left
💡 Hint
Consider how AVG behaves with all NULL values and how NULL comparisons work in SQL.
✗ Incorrect
If all salaries in a department are NULL, AVG returns NULL. Comparing salary > NULL yields UNKNOWN, so the row is excluded. This causes employees with NULL salaries to be missing unexpectedly.