Help me understand this: In the sqlzoo tutorial for question 3a ("Find the largest country in each region"), why does attaching 'AND population > 0' to the nested SELECT statement make this correct?
A:
No it doesn't. Largest country has a priori nonzero population.
It's like checking if a largest book has any pages in it.
Denis Valeev
2010-09-21 21:48:41
I think that's the point of @Hamster's question - that part of the clause is in the example given on the SQLzoo site
sasfrog
2010-09-21 21:52:46
Also the query returns different results if you omit the "> 0" clause.
Alex Reitbort
2010-09-21 21:54:03
Yeah Russia appears to be missing without it. I can't understand why that could be happening.
Hamster
2010-09-21 21:55:01
+2
A:
The reason is because the:
AND population > 0
...is filtering out the null row for the region "Europe", name "Vatican", which complicates the:
WHERE population >= ALL (SELECT population
FROM ...)
...because NULL isn't a value, so Russia won't be ranked properly. The ALL operator requires that the value you were comparing to be greater or equal to ALL the values returned from the subquery, which can never happen when there's a NULL in there.
My query would've been either:
SELECT region, name, population
FROM bbc x
WHERE population = (SELECT MAX(population)
FROM bbc y
WHERE y.region = x.region)
...or, using a JOIN:
SELECT x.region, x.name, x.population
FROM bbc x
JOIN (SELECT y.region,
MAX(y.population) AS max_pop
FROM bbc y
GROUP BY y.region) z ON z.region = x.region
AND z.max_pop = x.population
OMG Ponies
2010-09-21 22:01:46
@Hamster: Yep, otherwise you'd have seen other regions drop out too. The ALL operator requires that the value you were comparing to be greater or equal to ALL the values returned from the subquery, which can never happen when there's a NULL in there.
OMG Ponies
2010-09-21 22:11:13
Is there also a NOT NULL option as well? That would have made a lot more sense, maybe.
Hamster
2010-09-21 22:14:46
@Hamster: Yes, you could change `population > 0` to `population IS NOT NULL`
OMG Ponies
2010-09-21 22:15:52