SQL's LATERAL keyword allows subqueries in the FROM clause to reference columns from tables listed earlier in the same query — similar to correlated subqueries but used in the FROM list rather than WHERE. A simple student-courses example demonstrates the error that occurs without LATERAL and how adding the keyword resolves it. A more practical customer-orders example shows how LATERAL can replace a CTE-based approach to find orders exceeding each customer's average spend, resulting in a more compact and readable query. Performance trade-offs are noted: LATERAL executes the subquery per outer row, so it's not always optimal, but it's valuable when passing outer table columns into subqueries is necessary.

15m watch time
3 Impressions