What different?
RIGHT:
select distinct maker, price from product
inner join printer
on product.model = printer.model
where color = 'y' and price <= (select min(price) from printer where color = 'y')
WRONG:
select distinct maker, price from product
inner join printer
on product.model = printer.model
where color = 'y' and price <= all (select distinct price from printer where color = 'y')
I know using “min” is better in performance.
But could anyone explain what wrong and different in the result?
Tables structures:
Product(maker, model, type)
Printer(code, model, color, type, price)
neikel
ALL is a short form for expanding the comparison operator against ALL rows, and combining the conditions with AND.
ANY is a short form for expanding the comparison operator against ALL rows, and combining the conditions with OR.
Specifically,
expands to, assuming the subquery returns 4 rows
When any of those resove to NULL, the result is false because NULL is not comparable using
<=. MIN doesn’t have this problem (because MIN skips NULLs) except for the edge case where there is NO result row, in which case<= (select MIN..)can also give you an unexpected result.Using ALL in to perform a test against a nullable column should almost always be qualified with a filter, e.g.