Bird
0
0

How can you combine LATERAL joins with aggregate functions to find the average order amount per customer, including customers with no orders?

hard📝 Application Q9 of 15
PostgreSQL - Joins in PostgreSQL
How can you combine LATERAL joins with aggregate functions to find the average order amount per customer, including customers with no orders?
AUse INNER JOIN LATERAL with orders grouped by customer
BUse LEFT JOIN LATERAL with a subquery that aggregates orders per customer
CUse CROSS JOIN LATERAL without aggregation
DUse a subquery in SELECT without LATERAL
Step-by-Step Solution
Solution:
  1. Step 1: Include customers with no orders

    LEFT JOIN ensures all customers appear even if no matching orders.
  2. Step 2: Use LATERAL with aggregation

    The subquery aggregates orders per customer to calculate average amount.
  3. Final Answer:

    Use LEFT JOIN LATERAL with a subquery that aggregates orders per customer -> Option B
  4. Quick Check:

    LEFT JOIN LATERAL + aggregation = include all with averages [OK]
Quick Trick: LEFT JOIN LATERAL with aggregate subquery includes all rows [OK]
Common Mistakes:
  • Using INNER JOIN excludes customers without orders
  • Using CROSS JOIN LATERAL ignores join conditions
  • Not aggregating inside LATERAL subquery

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PostgreSQL Quizzes