if I wanna compare sale price of each product type with the sale price of each item within corresponding product type, here is the code to get it:
SELECT product_type, product_name, sale_price
FROM Product AS P1
WHERE sale_price > (SELECT AVG(sale_price)
FROM Product AS P2
WHERE P1.product_type = P2.product_type
GROUP BY product_type );
I can't understand subquery code for 'where P1.product_type = P2.product_type'. For the WHERE outside the subquery, it should be used to filter single row. However, how could it get a single result in subquery?