Problems / Scalar Subquery Filter / Editorial
(SELECT MAX(price) FROM products)
WHERE price = (SELECT MAX(price) FROM products)
product_id
In PySpark, the same idea is expressed by aggregating the max into a one-row DataFrame (products.agg(F.max("price"))), crossJoin-ing it onto every row (a one-row cross join is a cheap broadcast), and filtering where the prices match.
products.agg(F.max("price"))
crossJoin
ORDER BY price DESC LIMIT 1 looks equivalent but silently drops tied rows — here two products share the top price, so LIMIT 1 returns the wrong answer. A scalar-subquery comparison is the tie-safe way to say "all rows at the extreme value", and the same pattern generalizes to minimums, averages, and per-group extremes with correlated subqueries.
ORDER BY price DESC LIMIT 1
LIMIT 1
Solve Scalar Subquery Filter yourself →