python学习笔记(72) MySQL练习题

查询平均成绩大于60分的同学的学号和姓名和平均成绩:

SELECT

  B.student_id,

  student.sname,

  B.ccc

from

(select studuent_id,avg(num) as ccc from score GROUP BY student_id HAVING avg(num)>60 ) as B

left join

student on B.student_id = student.sid;  # 聚合函数要先as然后在外面调用

查询所有同学的学号、姓名、选课数、总成绩:

扫描二维码关注公众号,回复: 4307779 查看本文章

SELECT

  score.student_id,

  student.sname,

  count(student_id),

  sun(num)

from score LEFT JOIN student on  score.student_id = student.sid

GROUP BY score.student_id

查询没选过“李平老师”课的同学学号、姓名:

SELECT

  student.sid,

  student.sname

FROM

  students

WHERE

  sid NOT IN (

    SELECT

      student_id

    FROM

      score

    WHERE

      course_id IN

        SELECT course.cid FROM course LEFT JOIN teacher ON course.cid = teacher.tid

        WHERE teacher.tname = "李平老师"

      )

    GROUP BY

      student_id

)

查询生物课程比物理课程成绩高的所有学生的学号:

select A.student_id from 

  (select score.sid,score.student_id,course.name,score.num from score LEFT JOIN course on score.course_id = course.cid where course.cname = "生物") as A

INNER JOIN

  (select score.sid,score.student_id,course.name,score.num from score LEFT JOIN course on score.course_id = course.cid where course.cname = "物理") as B

on A.student_id = B.student_id

where A.num > B.num

猜你喜欢

转载自www.cnblogs.com/farion/p/10046540.html