题解 | #SQL 43.注册当天就完成了试卷的名单第三页#

注册当天就完成了试卷的名单第三页

http://www.nowcoder.com/practice/718d36d2667b48faa2168b6c1521816a

明确题意:

找到求职方向为算法工程师,且注册当天就完成了算法类试卷的人,按参加过的所有考试最高得分排名。每页3条,需取出第3页(页码从1开始)的人的信息。


问题分解:

  • 内连接用户信息表和试卷作答表:user_info JOIN exam_record USING(uid)
  • 继续内连接试卷信息表:JOIN examination_info USING(exam_id)
  • 继续内连接每个人的最高分表:
    • 按用户分组,计算分数的max:MAX(score) AS max_score;GROUP BY uid
  • 筛选满足条件的记录:
    • 求职方向为算法工程师:WHERE job = '算法'
    • 算法类试卷:tag = '算法'
    • 注册当天完成的试卷:DATE(register_time)=DATE(submit_time)
  • 选取第三页的3条,即偏移/跳过前6条,取三条:LIMIT 6,3

细节问题:

  • 按最高分降序:ORDER BY max_score DESC

完整代码:

SELECT uid, `level`, register_time, max_score 
FROM user_info
JOIN exam_record USING(uid)
JOIN examination_info USING(exam_id)
JOIN (
    SELECT uid, MAX(score) AS max_score
    FROM exam_record
    GROUP BY uid
) AS t_exam_max_score USING(uid)
WHERE job = '算法' AND tag = '算法' AND DATE(register_time)=DATE(submit_time)
ORDER BY max_score DESC
LIMIT 6,3;
SQL进阶 文章被收录于专栏

SQL进阶step by step

全部评论
此处在最后order by排序之前,应该要使用group by按uid进行分组,因为可能存在某一个用户在注册当天完成了9001与9002两份算法类试卷,会得到两条相同用户的记录,与题意不符。只不过这里刚好不存在这个的数据而已
8 回复 分享
发布于 2022-03-24 23:23
这里用left join可以吗
2 回复 分享
发布于 2023-04-07 10:57 上海
这个很好的解决了最高分问题,很多答案都是错的
2 回复 分享
发布于 2022-07-05 22:06
有没大佬看下我这个为什么自测通过提交不行呢?谢谢! with t1 as (select uid,max(score) max_score from exam_record left join examination_info using(exam_id) group by uid) select uid,level,register_time,max_score from user_info left join t1 using(uid) where uid in (select uid from exam_record left join user_info using(uid) where job='算法' and date_format(register_time,'%Y%m')=date_format(submit_time,'%Y%m')) order by max_score desc limit 6,3
点赞 回复 分享
发布于 01-25 12:20 美国
这也能通过,感觉不严谨。 select a.uid,level,register_time,max(score)over(partition by a.uid) max_score from user_info a left join exam_record b using(uid) left join examination_info c on b.exam_id=c.exam_id where tag='算法' and job='算法' and score is not null order by max_score desc limit 3 offset 6
点赞 回复 分享
发布于 2024-10-07 21:38 福建
您好,有个疑问,如果不区分试卷直接求取用户的最高分数,当该用户既答了算法卷又答了SQL卷且SQL卷最高分,那么这里取到的用户最高分就是无用数据了呀
点赞 回复 分享
发布于 2024-09-12 03:51 广东
膜拜大神
点赞 回复 分享
发布于 2023-08-06 19:34 广西
with t0 as ( select * from exam_record join examination_info using(exam_id) join user_info using(uid) ), t1 as ( select uid from t where job = '算法' and tag = '算法' and date(register_time) = date(submit_time) ) select uid, level, register_time, max(score)over(partition by uid) as max_score from t0 where uid in t1 group by uid,level,register_time order by max_score desc limit 6,3 请问大家这个语句错在哪儿呀 找了半天没找出来
点赞 回复 分享
发布于 2022-10-31 09:52 山东

相关推荐

评论
32
3
分享

创作者周榜

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