Challenge - 5 Problems
LATERAL Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ query_result
intermediate2:00remaining
Output of a LATERAL join with filtering
Given the tables authors and books, what is the output of the following query?
Assume
SELECT a.name, b.title FROM authors a CROSS JOIN LATERAL (SELECT title FROM books WHERE author_id = a.id ORDER BY published_year DESC LIMIT 1) b ORDER BY a.id;
Assume
authors has 3 rows with ids 1, 2, 3 and books has multiple books per author.PostgreSQL
SELECT a.name, b.title FROM authors a CROSS JOIN LATERAL (SELECT title FROM books WHERE author_id = a.id ORDER BY published_year DESC LIMIT 1) b ORDER BY a.id;
Attempts:
2 left
💡 Hint
LATERAL allows the subquery to use columns from the current row of the main query.
✗ Incorrect
The LATERAL subquery selects the latest book per author by ordering books by published_year descending and limiting to 1. The CROSS JOIN LATERAL applies this for each author row, producing the latest book title per author.
📝 Syntax
intermediate2:00remaining
Identify the syntax error in LATERAL usage
Which option contains a syntax error when using LATERAL in PostgreSQL?
PostgreSQL
SELECT a.name, b.title FROM authors a JOIN LATERAL (SELECT title FROM books WHERE author_id = a.id) b ON true;
Attempts:
2 left
💡 Hint
Check the parentheses around the subquery after LATERAL.
✗ Incorrect
Option C is missing parentheses around the subquery after LATERAL, causing a syntax error. LATERAL must be followed by a subquery enclosed in parentheses.
❓ optimization
advanced2:00remaining
Optimizing a query with LATERAL for performance
You have a query that uses LATERAL to find the top 3 recent orders per customer:
Which change will most likely improve performance on large datasets?
SELECT c.customer_id, o.order_id FROM customers c JOIN LATERAL (SELECT order_id FROM orders WHERE customer_id = c.customer_id ORDER BY order_date DESC LIMIT 3) o ON true;
Which change will most likely improve performance on large datasets?
Attempts:
2 left
💡 Hint
Indexes help speed up filtering and ordering.
✗ Incorrect
Adding a composite index on orders(customer_id, order_date DESC) helps the database quickly find the top 3 recent orders per customer, improving query performance.
🔧 Debug
advanced2:00remaining
Debugging unexpected NULL results in LATERAL join
A query uses LEFT JOIN LATERAL to get the latest comment per post:
Some posts have NULL for comment_text even though comments exist. What is the most likely cause?
SELECT p.post_id, c.comment_text FROM posts p LEFT JOIN LATERAL (SELECT comment_text FROM comments WHERE post_id = p.post_id ORDER BY created_at DESC LIMIT 1) c ON true;
Some posts have NULL for comment_text even though comments exist. What is the most likely cause?
Attempts:
2 left
💡 Hint
Check data consistency between posts and comments tables.
✗ Incorrect
If some posts have NULL comment_text despite comments existing, it likely means the comments have post_id values that do not match the posts table due to data inconsistency or mismatch.
🧠 Conceptual
expert3:00remaining
Understanding LATERAL subquery execution order
Consider the query:
Which statement best describes how the LATERAL subquery executes?
SELECT t1.id, t2.val FROM t1 JOIN LATERAL (SELECT val FROM t2 WHERE t2.ref_id = t1.id ORDER BY val DESC LIMIT 1) t2 ON true;
Which statement best describes how the LATERAL subquery executes?
Attempts:
2 left
💡 Hint
LATERAL allows the subquery to reference columns from the outer query row.
✗ Incorrect
LATERAL subqueries execute once per row of the outer query (t1), using that row's columns (t1.id) to filter the inner query (t2). This allows correlated filtering per row.