1. 程式人生 > >python學習筆記(72) MySQL練習題

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然後在外面呼叫

 

查詢所有同學的學號、姓名、選課數、總成績:

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