Type the correlated-subquery skeleton (per-group average comparison):
Type the correlated-subquery skeleton (per-group average comparison):
Answer
SELECT ol.order_id, ol.product_id, ol.qty FROM order_lines ol WHERE ol.qty > ( SELECT AVG(ol2.qty) FROM order_lines ol2 WHERE ol2.product_id = ol.product_id);
The ol2.product_id = ol.product_id line is the correlation: it ties the inner average to the outer row’s product, so the average is recomputed per product.