Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"WHERE X > ALL SELECT MAX" vs "WHERE X > SELECT MAX"

Tags:

sql

I came across multiple queries which use the all quantor for a max subquery:

Is there any difference between

SELECT name FROM bbc
 WHERE population > ALL
       (SELECT MAX(population)
          FROM bbc
         WHERE region = 'Europe')
   AND region = 'South Asia'

and

SELECT name FROM bbc
 WHERE population > 
       (SELECT MAX(population)
          FROM bbc
         WHERE region = 'Europe')
   AND region = 'South Asia'

?

like image 876
user695652 Avatar asked Sep 29 '22 08:09

user695652


1 Answers

SELECT MAX is an aggregate operation and, therefore, your subquery will select a single row.

Applying ALL to a single row will have no effect.

If your subquery returned multiple rows, the non-ALL version would result in an error. Also note that when using ALL, you could remove the MAX from the subquery and you'd get correct results (presumably with the same performance characteristics).

like image 56
StilesCrisis Avatar answered Oct 02 '22 16:10

StilesCrisis