首页 > 试题广场 >

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

[编程题]找出每个学校GPA最低的同学
  • 热度指数:432835 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 256M,其他语言512M
  • 算法知识视频讲解
题目:现在运营想要找到每个学校gpa最低的同学来做调研,请你取出每个学校的最低gpa。

示例:user_profile
id device_id gender age university gpa active_days_within_30
question_cnt
answer_cnt
1 2138 male 21 北京大学 3.4 7 2 12
2 3214 male 复旦大学 4 15 5 25
3 6543 female 20 北京大学 3.2 12 3 30
4 2315 female 23 浙江大学 3.6 5 1 2
5 5432 male 25 山东大学 3.8 20 15 70
6 2131 male 28 山东大学 3.3 15 7 13
7 4321 female 26 复旦大学 3.6 9 6 52
根据示例,你的查询结果应参考以下格式,输出结果按university升序排序:
device_id
university
gpa
6543
北京大学
3.2
4321 复旦大学 3.6
2131 山东大学
3.3
2315 浙江大学
3.6

示例1

输入

drop table if exists user_profile;
CREATE TABLE `user_profile` (
`id` int NOT NULL,
`device_id` int NOT NULL,
`gender` varchar(14) NOT NULL,
`age` int ,
`university` varchar(32) NOT NULL,
`gpa` float,
`active_days_within_30` int ,
`question_cnt` int ,
`answer_cnt` int 
);
INSERT INTO user_profile VALUES(1,2138,'male',21,'北京大学',3.4,7,2,12);
INSERT INTO user_profile VALUES(2,3214,'male',null,'复旦大学',4.0,15,5,25);
INSERT INTO user_profile VALUES(3,6543,'female',20,'北京大学',3.2,12,3,30);
INSERT INTO user_profile VALUES(4,2315,'female',23,'浙江大学',3.6,5,1,2);
INSERT INTO user_profile VALUES(5,5432,'male',25,'山东大学',3.8,20,15,70);
INSERT INTO user_profile VALUES(6,2131,'male',28,'山东大学',3.3,15,7,13);
INSERT INTO user_profile VALUES(7,4321,'male',28,'复旦大学',3.6,9,6,52);

输出

device_id|university|gpa
6543|北京大学|3.2
4321|复旦大学|3.6
2131|山东大学|3.3
2315|浙江大学|3.6
用了子查询和窗口函数结合的写法,感觉适用性更广一点
select device_id,university,gpa from 
(select device_id,university,gpa,dense_rank( ) over(partition by university order by gpa) num from user_profile
) info
where num=1

发表于 2025-12-04 17:31:47 回复(0)
select t1.device_id,t1.university,t1.gap from user_profile as t1
join
(select university,min(gap) as gap from user_profile
group by university) as t2
on t1.university = t2.university and t1.gap = t2.gap
order by university;
发表于 2025-12-04 13:42:05 回复(0)
select device_id,university,gpa
from
user_profile
where (university,gpa)
  in (select university,min(gpa) as gpa
from user_profile
group by university
)
order by university asc;
发表于 2025-11-22 15:27:02 回复(0)
-- 为每个学校开个窗口,然后从窗口里取出gpa最低的同学

select device_id
    , university
    , gpa
from (
    select device_id
        , university
        , gpa
        , row_number() over(partition by university order by gpa asc) as rk
    from
        user_profile
    ) as a
where rk = 1
;
发表于 2025-10-29 18:42:34 回复(0)
既然是窗口函数的题当然要使用窗口函数来做啦
select
device_id,university,gpa
from
(
select
device_id,university,gpa,
dense_rank() over(partition by university order by gpa) as rk
from
user_profile) as t
where rk = 1

发表于 2025-10-23 15:32:31 回复(0)
with rank_students as(
    select
        device_id,university,gpa
        row_number() over (partition by university order by gpa ASC) as rank
    from user_profile
)
select
    device_id,university,gpa
from rank_students
where rank=1
order by university
哪错了呢?
发表于 2025-10-13 15:12:44 回复(2)
select device_id, university, gpa
from user_profile as up
where gpa = (
    select MIN(gpa)
    from user_profile
    where university = up.university
)
order by university

关联子查询就行
发表于 2025-10-04 12:30:35 回复(0)
应该没有人像我这样写吧🤣🤣🤣
select u1.device_id, u1.university,u1.gpa
from user_profile u1
inner join user_profile u2 
on u1.university = u2.university
where u1.gpa < u2.gpa
union all 
select u.device_id, u.university,u.gpa
from user_profile u,
(select university,count(university) as count from user_profile group by university having count = 1) t
where u.university = t.university
order by university;


发表于 2025-08-28 02:04:27 回复(1)
select device_id
,university
,gpa
from (select device_id
,university
,gpa
,rank()over(partition by university order by gpa) posn
from user_profile
) cc
where cc.posn=1
发表于 2025-08-27 15:25:15 回复(0)
select device_id,
university,
gpa
from(
select device_id,
university,
gpa,
rank() over(partition by university order by gpa) as rnk
from user_profile) u
where rnk=1
利用窗口函数对各个学校的gpa进行排名,随后取排名为1的即可
发表于 2025-08-13 14:14:24 回复(0)
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;

发表于 2025-07-16 17:19:29 回复(0)
select device_id, university, gpa
from 
    (select device_id, university, gpa, 
    rank()over(partition by university order by gpa) as rk
    from user_profile) as a1
where rk = 1
发表于 2025-07-13 23:57:51 回复(0)
select device_id, university, gpa
from
(select device_id, university, gpa, rank()over(partition by university order by gpa asc) ord
from user_profile) as rk
where rk.ord=1
发表于 2025-07-11 20:53:20 回复(0)
WITH ranked AS (
    SELECT *,
        ROW_NUMBER() OVER (PARTITION BY university ORDER BY gpa) AS rnk
    FROM user_profile
)
SELECT 
    device_id,
    university,
    gpa
FROM ranked
WHERE rnk = 1;
子查询+窗口函数解法
发表于 2025-07-01 10:17:23 回复(0)
select
    device_id,
    university,
    gpa
from(
    select
        device_id,
        university,
        gpa,
        row_number() over (
            partition by university
            order by gpa
        ) as rnk # rank 是关键字,避免使用
    	# 窗口函数,在不改变原有行数的情况下,为每一行数据添加一个计算出来的列(这里新列命名为 rnk)
    from user_profile
) as ranked_users # FROM 子句中的子查询必须有别名
where rnk=1
order by university

发表于 2025-06-14 16:53:30 回复(0)
select device_id,university,gpa
from
(select device_id,university,gpa,
row_number() over (partition by university order by gpa) as rak
from user_profile) a
where rak=1
发表于 2025-06-05 14:27:09 回复(0)
请问这样哪里错了呀
select device_id, university, gpa
from(
    select *, rank() over (partition by university order by gpa) as rank
    from user_profile
    ) a
where a.rank=1
发表于 2025-05-22 17:49:44 回复(0)
with urank as (select *,dense_rank() over(partition by university order by gpa asc)  gparank from user_profile )
select device_id,university,gpa from urank where gparank =
发表于 2025-05-17 11:07:36 回复(0)
select u1.device_id, u1.university,u1.gpa
from user_profile as u1
join (select university,min(gpa) as min_gpa
from user_profile
group by university)as u2
on u1.university=u2.university and u1.gpa=u2.min_gpa
order by u1.university
为什么最后一定要加上一个order by u1.university,不是很懂,与没有大神指导下

发表于 2025-05-14 00:44:30 回复(0)