You misunderstood the EXISTS
operator: it returns TRUE
for each row of product
where the subquery returns at least one row (which your subquery does for every single row, because a MAX(...)
will return something if there's at least one row).
To rewrite the query with EXISTS
you need to check that there does not exist an item with a higher sale price, like this:
SELECT *
FROM product p
WHERE NOT EXISTS (
SELECT *
FROM product pp
WHERE p.sale_price < pp.sale_price
)
The logic here is that if there does not exist a product pp
with a higher sale_price
, then p
must be a product with the highest sale_price
.