#1.查询课程编号为“01”的课程比“02”的课程成绩高的所有学生的学号(重点) select a.s_id,a.s_score '01',b.s_score '02',c.s_name from (select * from score where c_id='01') a inner join (select * from score where c_id='02') b on a.s_id= b.s_id inner join student c on c.s_id=a.s_id where a.s_score>b.s_score
#2.查询平均成绩大于60分的学生的学号和平均成绩(简单,第二道重点) select s_id,avg(s_score) from score group by s_id having avg(s_score)>60 GROUP BY必须得配合聚合函数来用,分组之后你可以计数(COUNT),求和(SUM),求平均数(AVG)等。 一般情况下,使用group by,所查询的字段,通常为统计值,及group by 后面的字段
#3、查询所有学生的学号、姓名、选课数、总成绩(不重要) select a.s_id,a.s_name(有些数据库不合法),count(b.c_id), sum(b.s_score) from student as a left join score as b on a.s_id=b.s_id group by a.s_id 更严谨的做法: select a.s_id,a.s_name,count(b.c_id), sum(b.s_score) from student as a left join score as b on a.s_id=b.s_id group by a.s_id,a.s_name Null值返回0 select a.s_id,a.s_name,count(b.c_id), sum(case when b.s_score is null then 0 else b.s_score end) from student as a left join score as b on a.s_id=b.s_id group by a.s_id,a.s_name
#4、查询姓“猴”的老师的个数(不重要) select count(t_id) from teacher where t_name like '猴%'