题解 | #找出每个学校GPA最低的同学#
找出每个学校GPA最低的同学
http://www.nowcoder.com/practice/90778f5ab7d64d35a40dc1095ff79065
注意按学校升序(默认)排序
-- 子查询
SELECT a.device_id
,a.university
,a.gpa
from user_profile a
where gpa in (
select min(b.gpa)
from user_profile b
where a.university = b.university )
order by a.university
-- 子查询进阶
SELECT a.device_id
,a.university
,a.gpa
from user_profile a
where (university,gpa) in (
select b.university -- 不是纯聚合字段,需要配合group by
,min(b.gpa)
from user_profile b
group by b.university )
order by a.university
-- 使用all子查询
SELECT a.device_id
,a.university
,a.gpa
from user_profile a
where gpa <= all (
select b.gpa
from user_profile b
where a.university = b.university )
order by a.university 