I was going through SQLZOO “SELECT within SELECT tutorial” and here’s one of the queries that did the job (task 7)
world(name, continent, area, population, gdp)
SELECT w1.name, w1.continent, w1.population
FROM world w1
WHERE 25000000 >= ALL(SELECT w2.population FROM world w2 WHERE w2.continent=w1.continent)
My questions are about effectiveness of such query. The sub-query will run for each row (country) of the main query and thus repeatedly re-populating the ALL list for a given continent.
- Should I be concerned or will Oracle optimization somehow take care of it?
- Can it be reprogrammed without a correlated sub-query?
If you want to rewrite the query without a correalted subquery, here is one way:
I do not imply that this will be faster. Oracle’s optimizer is pretty good and this is a simple overall query, however you write it. Here’s another simplification: