题解 | #找出每个学校GPA最低的同学#

找出每个学校GPA最低的同学

https://www.nowcoder.com/practice/90778f5ab7d64d35a40dc1095ff79065

select device_id,university,gpa from user_profile where (university,gpa) in (select university,min(gpa) from user_profile group by university) order by university asc;

方法2:将表a的device_id,university,gpa和表b的university,min(gpa)连接起来找。

SELECT a.device_id,a.university,a.gpa FROM user_profile a

JOIN (SELECT university,min(gpa) gpa FROM user_profile GROUP BY university) b

on a.university=b.university and a.gpa=b.gpa

ORDER BY university;

方法二:窗口函数

1

2

3

4

SELECT device_id,university,gpa FROM

(SELECT device_id,university,gpa,

RANK() over (PARTITION BY university ORDER BY gpa) rk FROM user_profile) a

WHERE a.rk=1;

或者

Select device_id,university,gpa from (

Select device_id,university,gpa,

row_number() over (partition by university order by gpa asc) as a From user_profile) t where t.a=1;

有关窗口函数更多请见https://zhuanlan.zhihu.com/p/92654574

全部评论

相关推荐

不愿透露姓名的神秘牛友
07-25 17:51
点赞 评论 收藏
分享
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务