首页 > 解决方案 > Print name of all activities with neither maximum nor minimum number of participants

问题描述

How can i solve this question of hackerrank. I am quite confused an unable to move forward with this. enter image description here enter image description here

here is what i have tried

 SELECT DISTINCT ACTIVITY FROM FRIENDS,
 SELECT max(ACTIVITY) AS M
 WHERE M = (SELECT NAME FROM Activities);

标签: mysqlsql

解决方案


window functions如果你的 mysql 版本是 8 或以上,你可以使用

select activity from (select activity, count(*) as cnt,
                             max(count(*)) over () as maximum_cnt,
                             min(count(*)) over () as minimum_cnt
                        from friends group by activity) mytable
where cnt not in (maximum_cnt, minimum_cnt);

推荐阅读