vlambda博客
学习文章列表

【Mysql经典例题讲解(四)】

本文接上篇文章继续讲解Mysql例题;

26.查询每门课程的平均成绩,结果按平均成绩降序排列,平均成绩相同时,按课程编号升序排列:

select sc.cid,AVG(score)from sc GROUP BY cidORDER BY AVG(score) desc,cid asc

【Mysql经典例题讲解(四)】

27.查询平均成绩大于等于80的所有学生的学号、姓名和平均成绩

select student.sid,student.sname,t1.avgscorefrom student INNER JOIN (select sc.sid,AVG(sc.score) as avgscore from sc GROUP BY sc.sid HAVING AVG(sc.score) >= 80) as t1 on student.SId = t1.SId

【Mysql经典例题讲解(四)】

28.查询课程名称为「数学」,且分数低于60的学生姓名和分数

select student.sname,t1.scorefrom student INNER JOIN(select sc.sid,sc.score from sc,coursewhere sc.cid = course.cidand course.cname = '数学'and sc.score < 60) as t1 on student.sid = t1.sid

【Mysql经典例题讲解(四)】

29.查询所有学生的课程及分数情况(存在学生没成绩,没选课的情况)

select student.sid,sc.cid,sc.score from Student left join sc on student.sid = sc.sid

【Mysql经典例题讲解(四)】

30. 查询任何一门课程成绩在 70 分以上的姓名、课程名称和分数

select student.sname,course.cname,sc.scorefrom student,sc,coursewhere sc.score >= 70and student.sid = sc.sidand sc.cid = course.cid

【Mysql经典例题讲解(四)】

31.查询存在不及格的课程

select DISTINCT sc.cidfrom scwhere sc.score < 60

【Mysql经典例题讲解(四)】

32.查询课程编号为 01 且课程成绩在 80 分以上的学生的学号和姓名

select sc.sid,student.snamefrom sc,studentwhere sc.cid = "01"and sc.score > 80and sc.sid = student.sid

【Mysql经典例题讲解(四)】

33. 求每门课程的学生人数

select sc.cid,count(*) as 学生人数from scGROUP BY sc.cid

【Mysql经典例题讲解(四)】

34.成绩不重复,查询选修「张三」老师所授课程的学生中,成绩最高的学生信息及其成绩

select student.*,sc.scorefrom student,course,teacher,scwhere course.cid = sc.cidand course.tid = teacher.tidand teacher.tname = '张三'and student.sid = sc.sidORDER BY score descLIMIT 1

35.成绩有重复的情况下,查询选修「张三」老师所授课程的学生中,成绩最高的学生信息及其成绩

select student.*,sc.score from student,teacher,course,sc where teacher.tid = course.tidand sc.sid = student.sidand sc.cid = course.cidand teacher.tname = "张三"and sc.score = ( select Max(sc.score)  from sc,student,teacher,course where teacher.tid = course.tid and sc.sid = student.sid and sc.cid = course.cid and teacher.tname = "张三");