SQL 题: 求总分排名前三的学生。三张表: -- Create the course tableCREATE TABLE course (   id INT AUTO_INCREMENT PRIMARY KEY,   name VARCHAR(200) NOT NULL);-- Create the student tableCREATE TABLE student (   id INT AUTO_INCREMENT PRIMARY KEY,   name VARCHAR(200) NOT NULL);-- Create the score tableCREATE TABLE score (   id INT AUTO_INCREMENT PRIMARY KEY,   course_id INT NOT NULL,   stu_id INT NOT NULL,   score INT NOT NULL);-- Insert sample data into the course tableINSERT INTO course (name) VALUES('Math'),('Science'),('History');-- Insert sample data into the student tableINSERT INTO student (name) VALUES('John Smith'),('Alice Johnson'),('Bob Davis');-- Insert sample data into the score tableINSERT INTO score (course_id, stu_id, score) VALUES(1, 1, 95),(1, 2, 88),(1, 3, 75),(2, 1, 92),(2, 2, 89),(2, 3, 78),(3, 1, 87),(3, 2, 91),(3, 3, 79);我们先来看一下学生的分数情况:select student.name `name`,course.name course,score.score score from score,course,studentwhere score.stu_id=student.id and course.id=score.course_id;要求一: 求总分最高的学生,返回学生的姓名和总分# 1. 求解出最高总分select SUM(score) from score group by stu_id order by SUM(score) desc limit 1;# 2. 查询出所有总分等于该值的学生id和总分select stu.id id,SUM(score) score from student stu,score sc where stu.id=sc.stu_idgroup by stu.id having SUM(sc.score) = (select SUM(score) from score group by stu_id order by SUM(score) desc limit 1);# 3. 查询出总分排名第一的学生的相关信息select stu.name name,t.score from student stu inner join (select stu.id id,SUM(score) score from student stu,score sc where stu.id=sc.stu_idgroup by stu.id having SUM(sc.score) = (select SUM(score) from score group by stu_id order by SUM(score) desc limit 1)) t where t.id=stu.id;注意点: 可能存在多个学生总分都是最高的聚合函数必须配合group by使用当使用group by时,select查询字段只能是分组列或者聚合函数要求二: 求解单科最高的学生信息# 1. 求解每一门课程的最高分select course_id c_id,MAX(score) score from score group by course_id;# 2. 查询每一课得到最高分的学生信息select sc.stu_id,c.c_id,c.score from score sc inner join (   select course_id c_id,MAX(score) score from score group by course_id ) c on sc.course_id = c.c_id and sc.score=c.score;  # 3. 补充返回用户名和课程名等信息select stu.name 学生名,course.name 课程名,c.score 单科最高分数 from ( ( score sc inner join (   select course_id c_id,MAX(score) score from score group by course_id ) c on sc.course_id = c.c_id and sc.score=c.score ) inner join student stu on sc.stu_id=stu.id ) inner join course on course.id=sc.course_id;注意点:多表JOIN怎么写要求三: 求解总分排名前三的学生信息# 1. 查询出前三的总分select distinct SUM(score) from score group by stu_id order by SUM(score) desc limit 3; # 2. 查询出所有总分等于该值的学生id和总分select stu.id id,SUM(score) score from student stu,score sc where stu.id=sc.stu_idgroup by stu.id having SUM(sc.score) in (select distinct SUM(score) from score group by stu_id order by SUM(score) desc limit 3);
点赞 12
评论 1
全部评论

相关推荐

03-15 00:45
已编辑
中国科学院大学 Java
问的很简单都秒了,但是面试官没开摄像头,疑似kpi,无后续。--------------------3/14更新,3/12通知给了口头offer,3/13发了意向书,已拒。一面(35min)(25/3/6)(无后续)    1、自我介绍    2、介绍一下你的那个Python相关项目(本科毕设,web系统+算法模型提供部分接口)    3、Java面向对象有哪些特点呢?详细说一下。    4、介绍一下hashmap;为什么要把链表转换为红黑树呢?红黑树查找的时间复杂度?1.7和1.8的区别。    5、介绍一下concurrentHashmap。    6、synchronized锁和Lock锁有什么区别?    7、公平锁的一个底层是怎么实现的呢?    8、线程池的核心参数、拒绝策略、提交一个任务执行流程?    9、spring有哪些特点?(ioc/aop)    10、spring中对于循环依赖是怎么解决的?    11、MySQL和redis的区别?    12、MySQL的索引结构是什么?    13、MySQL的事务有哪些特性?怎么保证?    14、MySQL的默认隔离级别?可重复读是怎么做到的呢?    15、介绍一下MVCC和快照读readview。    16、一般在什么场景下会使用redis?    17、对于大量的请求,如果此时缓存中还没有写入数据怎么办?    18、介绍一下redis实现的分布式锁。    19、有用过es和mongo DB吗?(知道,没用过)    20、消息中间件用过吗?说一下你的使用场景?    21、一个场景,如果说有一个接口响应的比较慢,如果说让你排查,你会怎么去排查?(上下游接口、大key问题,只答了两,后面试官补充)    无手撕,反问业务。
胖墩墩的查理在学c语言:哥们我是五号面的 流程差不多
查看21道真题和解析
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务