日期:2014-05-17  浏览次数:20864 次

求大神指导,sql查询语句问题
有下列表
class(班级表):
classno(primary key), classname

student(学生表):
stuno(primary key), stuname, score , classno


1、 列出'李明'所在班级中每个学生的姓名与班级号(假设学生没有重名)
             Select stuname,classno From student where classno = (Select classno From student where stuname =’李明’)这是我自己做的,不知道对不对

2、 对于student表中的分数至少有2人高于本班级平均水平的班级,列出班级号,学生数,平均分数,按班级号升序排序
求解
3、 在student表全体学生中低于自己分数至少有5人的学生,列出其班级号,姓名,分数,以及分数少于自己的人数
求解

------解决方案--------------------
1是正确的
2:select b.classno,max(stus),max(b.avgs) from 
(select classno,stuname,avg(score)avgs from student group by classno,stuname)a
inner join 
(select classno,avg(score) avgs,count(*)stus from student group by classno)b
on a.classno=b.classno
where a.avgs>b.avgs
group by b.classno having count(*)>1
order by b.classno

3:没读懂。。
------解决方案--------------------
额。。你的student表分数不包含科目啊。。

--2:
select b.classno,max(stus),max(b.avgs) from student a
inner join 
(select classno,avg(score) avgs,count(*)stus from student group by classno)b
on a.classno=b.classno
where a.score>b.avgs
group by b.classno having count(*)>1
order by b.classno

--3:
select classno,stuname,score,num from (
select *,(select count(*) from student where a.stuno<>stuno and a.score>score) num
from student a 
)b where num>5