Alright, this one (3a; sample problem with provided answer) has got me scratching my head:
bbc(name, region, area, population, gdp)
3a. Find the largest country in each region:
SELECT region, name, population FROM bbc x WHERE population >= ALL (SELECT population FROM bbc y WHERE y.region = x.region AND population > 0)
I understand the concept of ‘WHERE y.region = x.region‘ when I think about it in terms of the db engine looping over the table entries and matching each x.region with the current y.region (in the nested SELECT)… but wtf does ‘AND population > 0‘ do? It isn’t a right answer without it, but I don’t see how not…
That clause is there just because there is an entry in the Europe table (for the Vatican) which has NULL in the population column. The following works also and I believe is more understandable:
In the MySQL documentation for ALL subqueries, there’s a helpful comment (emphasis theirs):