我可以使用这个子查询来解决这个 SQL 问题,而不必使用 GROUP BY 吗?
对于每个大陆,显示人口至少为 1000 万的大陆和国家数量。
world(continent, population, name)
SELECT x.continent,
count(x.name) as Total
from world AS x
WHERE 10000000 <= ALL (select y.population
from world AS y
WHERE y.continent=x.continent
)
如果
just personal interest
参见示例
create table world(continent varchar(30), population int, country varchar(30));
insert into world values
('Cont1',1500000000,'Country1-1')
,('Cont1',1500000000,'Country1-2')
,('Cont1', 10000,'Country1-3')
,('Cont2', 10000,'Country2-1')
,('Cont2', 100000,'Country2-2')
,('Cont2', 1000000,'Country2-3')
,('Cont2', 10000000,'Country2-4')
,('Cont3', 1000000,'Country3-1')
;
select distinct continent
,(select count(*) from world w2 where w2.continent=w.continent and population>=10000000) cnt
from world w
大陆 | cnt |
---|---|
续1 | 2 |
续2 | 1 |
续3 | 0 |