Challenge - 5 Problems
LATERAL Join Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ query_result
intermediate2:00remaining
Output of LATERAL join with correlated subquery
Given the tables departments and employees, what is the output of this query?
SELECT d.name, e.name
FROM departments d
JOIN LATERAL (SELECT name FROM employees WHERE department_id = d.id ORDER BY salary DESC LIMIT 1) e ON true
ORDER BY d.id;
PostgreSQL
CREATE TABLE departments (id INT PRIMARY KEY, name TEXT); CREATE TABLE employees (id INT PRIMARY KEY, name TEXT, department_id INT, salary INT); INSERT INTO departments VALUES (1, 'HR'), (2, 'Engineering'); INSERT INTO employees VALUES (1, 'Alice', 1, 50000), (2, 'Bob', 1, 60000), (3, 'Charlie', 2, 70000), (4, 'Diana', 2, 65000);
Attempts:
2 left
💡 Hint
Look for the employee with the highest salary in each department.
✗ Incorrect
The LATERAL join runs the subquery for each department row. It selects the employee with the highest salary in that department. For HR, Bob has the highest salary (60000). For Engineering, Charlie has the highest salary (70000).
📝 Syntax
intermediate1:30remaining
Identify the syntax error in LATERAL join usage
Which option contains a syntax error when using LATERAL join in PostgreSQL?
PostgreSQL
SELECT d.name, e.name FROM departments d JOIN LATERAL (SELECT name FROM employees WHERE department_id = d.id) e ON true;
Attempts:
2 left
💡 Hint
Check if the subquery is properly enclosed in parentheses.
✗ Incorrect
Option A misses parentheses around the subquery after LATERAL, which is required syntax. The others correctly use parentheses or aliasing.
❓ optimization
advanced2:00remaining
Optimizing a LATERAL join query for performance
You have this query:
Which change will most likely improve performance on large tables?
SELECT d.name, e.name
FROM departments d
JOIN LATERAL (SELECT name FROM employees WHERE department_id = d.id ORDER BY salary DESC LIMIT 1) e ON true;
Which change will most likely improve performance on large tables?
Attempts:
2 left
💡 Hint
Think about how the database finds the top salary employee per department efficiently.
✗ Incorrect
An index on employees(department_id, salary DESC) helps the database quickly find the highest salary employee per department without scanning all employees.
🔧 Debug
advanced2:00remaining
Why does this LATERAL join query return no rows?
Given these tables:
Why does this query return no rows?
departments(id, name), employees(id, name, department_id, salary INT)
Why does this query return no rows?
SELECT d.name, e.name
FROM departments d
JOIN LATERAL (SELECT name FROM employees WHERE department_id = d.id AND salary > 100000) e ON true;
Attempts:
2 left
💡 Hint
Consider what happens when the lateral subquery returns no rows in a JOIN.
✗ Incorrect
No employees have salary > 100000, so for every department the lateral subquery returns no rows. Since it's a JOIN (inner join), departments without matching employees are excluded, resulting in no rows.
🧠 Conceptual
expert2:30remaining
Understanding LATERAL join behavior with multiple rows
Consider this query:
How many rows will this query return if each department has 3 employees?
SELECT d.name, e.name
FROM departments d
JOIN LATERAL (SELECT name FROM employees WHERE department_id = d.id LIMIT 2) e ON true
ORDER BY d.id, e.name;
How many rows will this query return if each department has 3 employees?
Attempts:
2 left
💡 Hint
LATERAL runs the subquery for each row of the left table and returns all rows from the subquery.
✗ Incorrect
The subquery limits to 2 employees per department, so for each department row, 2 employee rows are joined, resulting in 2 rows per department.