~/ learn/ comp-378/ cards/ Correlated Subqueries & EXISTS / NOT EXISTS
1 of 6

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.

space flip · ← → navigate · esc to exit
NORMAL ~/memra/library/a0ac3490-1cb0-496f-9e0c-9bdb00816088/flashcard utf-8 LF