使用sql语句查询计算机科学系中选修课程门数大于刘德华同学(也是计算机科学系)所选课程门数的所有的学生姓名的选课门数及加权平均成绩,要求选课门数和加权平均成绩都按降序输出。怎么写
时间: 2023-06-07 14:08:13 浏览: 98
基于java+sql server实现的学生选课、成绩查询系统,优秀毕业设计,新手必看!
5星 · 资源好评率100%
以下是 SQL 语句的实现:
SELECT s.name, COUNT(sc.course_id) AS course_num, SUM(sc.score * c.credit) / SUM(c.credit) AS average
FROM student AS s
JOIN score AS sc ON s.id = sc.student_id
JOIN course AS c ON sc.course_id = c.id
WHERE s.department = '计算机科学系'
GROUP BY s.id
HAVING course_num > (
SELECT COUNT(sc2.course_id)
FROM student AS s2
JOIN score AS sc2 ON s2.id = sc2.student_id
WHERE s2.name = '刘德华'
)
ORDER BY course_num DESC, average DESC
注:该 SQL 语句假设各表结构如下:
student (id, name, department)
score (id, student_id, course_id, score)
course (id, name, credit)
阅读全文