首页 > 代码库 > mysql 回顾小练习

mysql 回顾小练习

Student(id,sname,age,sex) 学生表
Course(id,cname,t_id) 课程表
SC(s_id,c_id,score) 成绩表
Teacher(id,Tname) 教师表
问题:
1.查询“001”课程比“002”课程成绩高的所有学生的学号;

select aa.id from (select s.id,SC.score sc1 from student s ,Course c,SC  where s.id=SC.s_id and c.id=SC.c_id and c.id=1) aa,(select s.id,SC.score sc2 from student s ,Course c,SC  where s.id=SC.s_id and c.id=SC.c_id and c.id =2) bb where aa.id=bb.id and aa.sc1>bb.sc2


2.查询平均成绩大于60分的同学的学号和平均成绩;

select s.id ,avg(SC.score) from student s,SC where s.id=SC.s_id GROUP BY s.id HAVING avg(SC.score)>60


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

select s.id,s.sname,count(c.id) XUKESHU,sum(SC.score) ZONGCHENGJI from student s,SC,Course c where s.id=SC.s_id and c.id=SC.c_id GROUP BY s.id


4.查询姓“李”的老师的个数;

select count(t.id) from Teacher t where t.tname like"%"


5.查询没学过“叶平”老师课的同学的学号、姓名;

select s.id from student  s where s.id  not in (select s.id from student s , Teacher t ,Course c ,SC where SC.s_id=s.id and SC.c_id=c.id and c.t_id=t.id  and c.id =1)


6.查询学过“001”并且也学过编号“002”课程的同学的学号、姓名;

select aa.id,aa.sname from (select s.id,s.sname from student s , Teacher t ,Course c ,SC where SC.s_id=s.id and SC.c_id=c.id and c.t_id=t.id  and c.id =1 ) aa,(select s.id,s.sname from student s , Teacher t ,Course c ,SC where SC.s_id=s.id and SC.c_id=c.id and c.t_id=t.id  and c.id =2) bb where aa.id=bb.id


7.查询不同老师所教不同课程平均分从高到低显示

select avg(SC.score),t.tname,c.cname from SC, Teacher t,Course c where SC.c_id=c.id and c.t_id=t.id GROUP BY t.tname order by avg(SC.score) desc


8.查询各科成绩前三名的记录:(不考虑成绩并列情况)

select s.id,s.sname,c.cname,SC.score from student s , Teacher t ,Course c ,SC where SC.s_id=s.id and SC.c_id=c.id and c.t_id=t.id  and c.id =1 order by SC.score desc limit 3


9.查询男生人数

select count(s.id),s.sex from student s where s.sex=""


10.查询同名同性学生名单,并统计同名人数

select count(s.id),s.sname from student s GROUP BY s.sname HAVING count(s.id)>1

mysql 回顾小练习