0
0
PostgreSQLquery~20 mins

LATERAL join for correlated subqueries in PostgreSQL - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
LATERAL Join Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
query_result
intermediate
2: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);
A[{"department": "HR", "employee": "Bob"}, {"department": "Engineering", "employee": "Diana"}]
B[{"department": "HR", "employee": "Alice"}, {"department": "Engineering", "employee": "Charlie"}]
C[{"department": "HR", "employee": "Alice"}, {"department": "Engineering", "employee": "Diana"}]
D[{"department": "HR", "employee": "Bob"}, {"department": "Engineering", "employee": "Charlie"}]
Attempts:
2 left
💡 Hint
Look for the employee with the highest salary in each department.
📝 Syntax
intermediate
1: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;
ASELECT d.name, e.name FROM departments d JOIN LATERAL SELECT name FROM employees WHERE department_id = d.id e ON true;
BSELECT d.name, e.name FROM departments d JOIN LATERAL (SELECT name FROM employees WHERE department_id = d.id) e ON true;
CSELECT d.name, e.name FROM departments d JOIN LATERAL (SELECT name FROM employees WHERE department_id = d.id) AS e ON true;
DSELECT d.name, e.name FROM departments d JOIN LATERAL (SELECT name FROM employees WHERE department_id = d.id) e ON d.id = e.department_id;
Attempts:
2 left
💡 Hint
Check if the subquery is properly enclosed in parentheses.
optimization
advanced
2:00remaining
Optimizing a LATERAL join query for performance
You have 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;

Which change will most likely improve performance on large tables?
AAdd an index on employees(department_id, salary DESC)
BRemove the ORDER BY clause inside the subquery
CReplace LATERAL join with a CROSS JOIN
DAdd a WHERE clause filtering departments by name
Attempts:
2 left
💡 Hint
Think about how the database finds the top salary employee per department efficiently.
🔧 Debug
advanced
2:00remaining
Why does this LATERAL join query return no rows?
Given these tables:
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;
AThe lateral subquery must not have a WHERE clause referencing outer table columns
BJOIN LATERAL requires ON condition to compare columns from both tables, ON true is invalid
CNo employees have salary > 100000, so the lateral subquery returns no rows, causing JOIN to behave like INNER JOIN
DThe query is missing GROUP BY clause for aggregation
Attempts:
2 left
💡 Hint
Consider what happens when the lateral subquery returns no rows in a JOIN.
🧠 Conceptual
expert
2:30remaining
Understanding LATERAL join behavior with multiple rows
Consider this query:
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?
A3 rows per department, total rows = number_of_departments * 3
B2 rows per department, total rows = number_of_departments * 2
C1 row per department, total rows = number_of_departments
Dnumber_of_departments rows, but only employees with highest salary
Attempts:
2 left
💡 Hint
LATERAL runs the subquery for each row of the left table and returns all rows from the subquery.