0
0
PostgreSQLquery~20 mins

Correlated subqueries execution model in PostgreSQL - 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 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);
A[{"name": "Alice", "salary": 5000}, {"name": "Bob", "salary": 6000}, {"name": "Eve", "salary": 5500}]
B[]
C[{"name": "Charlie", "salary": 4000}, {"name": "Diana", "salary": 4500}]
D[{"name": "Bob", "salary": 6000}, {"name": "Diana", "salary": 4500}]
Attempts:
2 left
💡 Hint
Think about how the subquery calculates the average salary per department and compares each employee's salary to it.
🧠 Conceptual
intermediate
1:30remaining
Understanding correlated subquery execution frequency
In a correlated subquery, how often is the inner subquery executed during the execution of the outer query?
AOnce for each row processed by the outer query
BOnly if the outer query returns no rows
COnce for the entire outer query
DTwice for each row processed by the outer query
Attempts:
2 left
💡 Hint
Think about how the inner query 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 usage?
PostgreSQL
SELECT e1.name FROM employees e1 WHERE salary > (SELECT AVG(salary) FROM employees e2 WHERE e2.dept_id = e1.dept_id);
ASELECT e1.name FROM employees e1 WHERE salary > (SELECT AVG(salary) FROM employees e2 WHERE e2.dept_id == e1.dept_id);
BSELECT e1.name FROM employees e1 WHERE salary > (SELECT AVG(salary) FROM employees e2 WHERE e2.dept_id = e1.dept_id);
C;)di_tped.1e = di_tped.2e EREHW 2e seeyolpme MORF )yralas(GVA TCELES( > yralas EREHW 1e seeyolpme MORF eman.1e TCELES
DELECT 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.
optimization
advanced
2: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?
ASELECT name FROM employees e WHERE salary > 7000;
BSELECT name FROM employees e WHERE (SELECT COUNT(*) FROM employees WHERE dept_id = e.dept_id AND salary > 7000) > 0;
CSELECT name FROM employees e WHERE EXISTS (SELECT 1 FROM employees WHERE dept_id = e.dept_id AND salary > 7000);
DSELECT name FROM employees e WHERE dept_id IN (SELECT dept_id FROM employees WHERE salary > 7000);
Attempts:
2 left
💡 Hint
EXISTS stops scanning as soon as it finds a matching row, COUNT(*) counts all matching rows.
🔧 Debug
expert
2:30remaining
Debugging unexpected results in correlated subquery with NULLs
Given the query:
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?
AThe outer query filters out NULL salaries automatically, so they never appear.
BThe subquery returns NULL if all salaries in the department are NULL, causing the comparison to fail.
CThe AVG aggregate ignores NULL salaries, so employees with NULL salaries are excluded from comparison.
DThe correlated subquery syntax is invalid when NULL values exist.
Attempts:
2 left
💡 Hint
Consider how AVG behaves with all NULL values and how NULL comparisons work in SQL.