Skip to content

Instantly share code, notes, and snippets.

@mikehins
Last active November 15, 2022 12:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mikehins/5da1dba8c42bf94c7e731d22b895e331 to your computer and use it in GitHub Desktop.
Save mikehins/5da1dba8c42bf94c7e731d22b895e331 to your computer and use it in GitHub Desktop.
# https://sqlfordevs.com/for-each-loop-lateral-join
SELECT customers.*, recent_sales.*
FROM customers
LEFT JOIN LATERAL (
SELECT *
FROM sales
WHERE sales.customer_id = customers.customer_id
ORDER BY created_at DESC
LIMIT 3
) AS recent_sales ON true;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment