Type the four-table sales chain with a grouped SUM:
Type the four-table sales chain with a grouped SUM:
Answer
SELECT c.name, SUM(ol.qty) AS units FROM customers c JOIN orders o ON o.customer_id = c.customer_id JOIN order_lines ol ON ol.order_id = o.order_id JOIN products p ON p.product_id = ol.product_id GROUP BY c.name;
customers→orders→order_lines→products is the standard sales chain: three ON conditions reach four tables; GROUP BY c.name aggregates per customer.