SQL练习,需要帮助

时间:2017-11-02 19:48:15

标签: sql

我是SQL的新手并且真的难以接受一个特定的问题,如果有人能花时间帮我解决这个问题,我真的很感激。

问题:向人口最少的大陆(除南极洲外)展示3个最大的国家(按人口计算)。

SELECT name ,
       country.population ,
       continent ,
       SUM(country.population) OVER ( PARTITION BY continent ) continent_population
FROM   country
WHERE  continent != 'Antarctica';

到目前为止,我已经得到了这个问题,这个问题让我得到了每个大陆相应人口的表格,但是我错过了将其缩小到人口最少的大陆。谢谢你的帮助

数据库ER图如下

Database ER diagram

1 个答案:

答案 0 :(得分:0)

我认为您可以订购结果并获得前三名。

SELECT 
       name ,
       country.population ,
       continent ,
       SUM(country.population) OVER ( PARTITION BY continent ) continent_population
FROM   country
WHERE  continent != 'Antarctica'
ORDER BY 
  continent_population ASC, 
  country.population DESC
LIMIT 3;
相关问题