Databases · Middle · Question 31 of 75
What does LEFT JOIN LATERAL allow?
SELECT u.name, o.*
FROM users u
LEFT JOIN LATERAL (
SELECT * FROM orders WHERE orders.user_id = u.id
ORDER BY created_at DESC LIMIT 3
) o ON true;- 1Joining tables side by side without a condition
- 2Joining only on indexed columns
- ✓Correct answer: A subquery in
FROMthat can reference columns of earlier tables, here the 3 latest orders per user
Explanation
It's a clean way to write 'top N per group'. With an index on (user_id, created_at) each lookup is cheap.