日期:2014-05-18  浏览次数:20400 次

一个查询题,求论坛大湿指点小弟一二
--题目,查询所有C语言成绩比其所在班级平均成绩高的学生 

select *from sc
select *from course
select* from student
select sid,sname from student
where sid in
(select sid from sc
where cid in
(select cid from course where cname='c')
--group by cid having score>avg(score)
)
--小弟初学数据库,以上自己水平写出来的--

下面是数据库中的三个表


sc表
scid sid cid score
----------- ----------- ----------- ----------------------
1 1 1 90
2 1 2 60
3 2 1 80
4 2 3 50

(4 行受影响)

course表
cid cname
----------- --------------------------------------------------
1 java
2 sql
3 c

(3 行受影响)
student表
sid sname ssex sage enterTime address tid
1 张三 男 30 1981-12-1220:44:11.200 湖北襄阳 1
2 张小三 男 30 1981-12-120:44:11.200 湖北襄阳 1
3 李四 女 20 1991-12-120:44:11.200 湖北枣阳 2
4 李小四 男 27 1991-12-120:44:11.200 湖北锦州 3
后面还有部分上表内容我就不贴出来了
求论坛的前辈们指点一下小弟
 


------解决方案--------------------
SQL code
select sid,sname from student
where sid in
(select a. sid from sc a inner join course b on a.cid=b.cid
where b.cname='c' and a.score>(select avg(a.score) from sc a inner join course b on a.cid=b.cid where b.cname='c'))