环境如day03
-- 5. 查询每位老师所教学生的人数及姓名
SELECT teacher.`tno`,teacher.`tname`,GROUP_CONCAT(student.sname) AS sname,COUNT(student.`sno`) AS 人数
FROM teacher JOIN course ON teacher.`tno`=course.`tno`
JOIN sc ON course.`cno`=sc.`cno`
JOIN student ON student.`sno`=sc.`sno`
GROUP BY teacher.`tno`;
-- 6. 统计每位老师所教课程的平均分
SELECT teacher.`tno`,teacher.`tname`,course.`cname`,AVG(sc.score) AS 平均成绩
FROM teacher JOIN course ON teacher.`tno`=course.`tno`
JOIN sc ON sc.`cno`=course.`cno`
GROUP BY teacher.`tno`;
-- 7. 统计每位老师所教课程不及格的学生名
SELECT teacher.`tno`,teacher.`tname`,GROUP_CONCAT(student.`sname`) AS 'sname'
FROM teacher JOIN course ON teacher.`tno`=course.`tno`
JOIN sc ON course.`cno`=sc.`cno`
JOIN student ON sc.`sno`=student.`sno`
WHERE sc.`score`<60
GROUP BY teacher.`tno`;
-- 8. 查询平均成绩大于60分的同学的学号和平均成绩;
SELECT student.`sno`,student.sname,AVG(sc.`score`) AS 平均分
FROM student JOIN sc ON student.`sno`=sc.`sno`
GROUP BY student.`sno`
HAVING AVG(sc.`score`) > 60;
-- 9. 查询所有同学的学号、姓名、选课数、总成绩
SELECT student.`sno`,student.`sname`,COUNT(sc.`score`) AS 选课数,SUM(sc.`score`) AS 总成绩
FROM student JOIN sc ON student.`sno`=sc.`sno`
GROUP BY student.`sno`;
-- 10. 查询各科成绩最高和最低的分:以如下形式显示:课程ID,最高分,最低分
SELECT course.`cname`,MAX(sc.`score`) AS 'max',MIN(sc.`score`) AS 'min'
FROM course JOIN sc ON course.`cno`=sc.`cno`
GROUP BY course.`cname`;
-- 11. 查询每门课程被选修的学生数
SELECT course.`cname`,COUNT(sc.`sno`) AS 学生数量
FROM course JOIN sc ON course.`cno`=sc.`cno`
GROUP BY course.`cno`;
-- 12. 查询出只选修了一门课程的全部学生的学号和姓名
SELECT student.`sno`,student.`sname`,COUNT(sc.`cno`) AS '课程门数'
FROM student JOIN sc ON student.`sno`=sc.`sno`
GROUP BY student.`sno`
HAVING COUNT(sc.`cno`)=1;
-- 13. 查询选修课程门数超过1门的学生信息
SELECT student.`sno`,student.`sname`,COUNT(sc.`cno`) AS '课程门数'
FROM student JOIN sc ON student.`sno`=sc.`sno`
GROUP BY student.`sno`
HAVING COUNT(sc.`cno`)>1;
-- 15. 查询平均成绩大于85的所有学生的学号、姓名和平均成绩
SELECT student.sno,student.`sname`,AVG(sc.`score`)
FROM student JOIN sc ON student.`sno`=sc.`sno`
GROUP BY student.`sno`
HAVING AVG(sc.`score`)>85;
-- 16. 统计每门课程:优秀(85分以上),良好(70-85),一般(60-70),不及格(小于60)的学生列表
SELECT CONCAT(course.`cname`,' 85+'),GROUP_CONCAT(student.`sno`,'_',student.`sname`,':',sc.`score`)
FROM sc JOIN student ON sc.`sno`=student.`sno`
JOIN course ON sc.`cno`=course.`cno`
WHERE sc.`score`>85
GROUP BY course.`cname`
UNION ALL
SELECT CONCAT(course.`cname`,' 70-85'),GROUP_CONCAT(student.`sno`,'_',student.`sname`,':',sc.`score`)
FROM sc JOIN student ON sc.`sno`=student.`sno`
JOIN course ON sc.`cno`=course.`cno`
WHERE sc.`score` BETWEEN 70 AND 85
GROUP BY course.`cname`
UNION ALL
SELECT CONCAT(course.`cname`,' 60-70'),GROUP_CONCAT(student.`sno`,'_',student.`sname`,':',sc.`score`)
FROM sc JOIN student ON sc.`sno`=student.`sno`
JOIN course ON sc.`cno`=course.`cno`
WHERE sc.`score` BETWEEN 60 AND 70
GROUP BY course.`cname`
UNION ALL
SELECT CONCAT(course.`cname`,' <60'),GROUP_CONCAT(student.`sno`,'_',student.`sname`,':',sc.`score`)
FROM sc JOIN student ON sc.`sno`=student.`sno`
JOIN course ON sc.`cno`=course.`cno`
WHERE sc.`score` <60
GROUP BY course.`cname`;
-- 17. 统计各位老师,所教课程的及格率
SELECT teacher.tname,course.cname,
COUNT(CASE WHEN sc.score>60 THEN 1 END)/COUNT(sc.score) AS '百分率'
FROM teacher
JOIN course ON teacher.tno=course.tno
JOIN sc ON course.cno=sc.cno
GROUP BY teacher.tno,course.cno;
-- 16. 统计每门课程:优秀(85分以上),良好(70-85),一般(60-70),不及格(小于60)的学生列表
SELECT course.`cname`,GROUP_CONCAT(CASE WHEN sc.`score` >=85 THEN 1 END,student.`sno`,'_',student.`sname`,':',sc.`score`)AS '85+',
GROUP_CONCAT(CASE WHEN sc.`score` <85 AND sc.`score` >=70 THEN 1 END,student.`sno`,'_',student.`sname`,':',sc.`score`) AS '70-85',
GROUP_CONCAT(CASE WHEN sc.`score` <70 AND sc.`score` >=60 THEN 1 END,student.`sno`,'_',student.`sname`,':',sc.`score`) AS '60-70',
GROUP_CONCAT(CASE WHEN sc.`score` <60 THEN 1 END,student.`sno`,'_',student.`sname`,':',sc.`score`) AS '<60'
FROM sc JOIN student ON sc.`sno`=student.`sno`
JOIN course ON sc.`cno`=course.`cno`
GROUP BY course.`cname`;