首页 > 解决方案 > 查询 SQL [PSQL]

问题描述

我需要有关 SQL 的帮助...我想知道是否有可能找到拥有最多球队的国家......国家队)...

f1 driver ( id_driver , driver_name , team , country ) 

我设法编写了给我每个国家的团队数量的命令(我认为这是正确的)......我不明白如何以及在哪里使用'max'命令......你能帮我吗?

SELECT (country) ,count(team) 
FROM driver
GROUP BY country;

标签: sqlpsql

解决方案


在该解决方案中,您不需要 max ,只需对结果进行排序:

SELECT (country) ,count(distinct team) 
FROM driver
GROUP BY country
order by count(distinct team) DESC

如果你想要拥有最多球队的国家:

SELECT (country) ,count(distinct team) 
FROM driver
GROUP BY country
order by count(distinct team)  DESC
LIMIT 2

推荐阅读