一些国家的人口是其邻国(在同一大陆上)的三倍以上。给国家和大洲。
From https://sqlzoo.net/wiki/SELECT_within_SELECT_Tutorial, question 10
我试过了
SELECT w.name, w.continent
FROM world w
WHERE w.population/3 > (SELECT min(w2.population)
FROM world w2
WHERE w2.continent = w.continent);
这个解决方案似乎有效
SELECT w.name, w.continent
FROM world w
WHERE w.population > (SELECT 3 * MAX(w2.population)
FROM world w2
WHERE w2.continent = w.continent AND
w2.name <> w.name
);
我试图了解解决方案中的逻辑缺陷。 为什么要使用max()而不使用min(),因为我们想比较人口是否是该大陆人口最少的国家的3倍?我们在最小/最大场景中需要w2.name <> w.name为什么?
意思是,同一大陆上第二高的人口必须少于三分之一。 (非洲大陆上的最低人口数量无关紧要。)
The second query takes all countries on the same continent except the one in the outer query (
w2.name <> w.name
), finds the most populous and multiplies by 3. If that's still lower than the population of the country in the outer query, it passes the condition.