Problems / Above Category Average Spend / Editorial
(SELECT AVG(price) FROM products p2 WHERE p2.category = p.category)
price >
category
product_id
A correlated subquery re-evaluates once per outer row, which reads naturally but hides a join. The equivalent set-based form — a CTE of per-category averages joined back to products — is exactly what PySpark forces you to write (groupBy("category").agg(F.avg(...)) plus a join), and is usually how the optimizer executes the correlated version anyway.
products
groupBy("category").agg(F.avg(...))
Solve Above Category Average Spend yourself →