表 student : id (学号),name(姓名)
表 course: id(课程号),name(课程名称),cteacher(授课老师)
表 sc:sid(学号),cid(课程号),score(分数)
请写出下面几个sql
1:找出所有选修了‘王刚’老师所授课的学生学号和姓名
2:找出有一门成绩低于60分的学生的平均成绩
3:找出选修了课程1和课程2,并且课程1的分数比课程2的分数高的学生姓名
表 course: id(课程号),name(课程名称),cteacher(授课老师)
表 sc:sid(学号),cid(课程号),score(分数)
请写出下面几个sql
1:找出所有选修了‘王刚’老师所授课的学生学号和姓名
2:找出有一门成绩低于60分的学生的平均成绩
3:找出选修了课程1和课程2,并且课程1的分数比课程2的分数高的学生姓名
解决方案 »
- 如何去除重复(两个或更多字段相同)数据?
- 关于使用Database Configuation Assistant的问题。
- 请教一个存储过程输出参数的问题
- 按月统计数据,不满一月的按星期统计的sql问题
- 空值和null
- 高手们能给我看看我的这个备份脚本和恢复脚本正确吗?
- 如何批量导出sql文件
- 小妹向oracle高手求助!oracle initialization or shutdown in progress
- 请教牛人,如何用 sql语句实现?
- 如何将这条在SQL Server下的查询语句转换为Oracle的查询语句?
- 谈谈左、右、内连接的区别
- Oracle10g无归档,某些表中部分数据误删,保留联机日志,能否回滚?
select a.sid,b.name
from sc a,student b,course c
where a.sid=b.id
and a.cid=c.id
and c.cteacher='王刚';2:
select sid,avg(score) from sc a
where exist (
select sid from sc b where score<60
and a.sid=b.sid)
group by sid3.
select a.sid,b.name
from(
select sid,cid,score
from sc
where exists (
select id from course where name in ('课程1','课程2') and cid=id)) a,
student b,
sc c
where a.sid=b.id
and a.cid=c.id
and decode(c.name,'课程1',a.score,0)>decode(c.name,'课程2',a.score,0)没实际测,试试吧
select * from student
where
EXISTS
(
select * from sc
where
sc.sid=student.id and
EXISTS
(
select * from course
where course.cteacher='王刚'
and course.id=sc.cid
)
)
select a.sid,b.name
from(
select sid,cid,score
from sc
where exists (
select id from course where name in ('课程1','课程2') and cid=id)) a,
student b,
sc c
where a.sid=b.id
and a.cid=c.id
and decode(c.name,'课程1',a.score,0)>decode(c.name,'课程2',a.score,0) 这个没有看懂是什么意思? 能给解释一下么?
decode(c.name,'课程1',a.score,0)>decode(c.name,'课程2',a.score,0) 这是条件,我没测,不知这样写的结果对不对.
这样作为条件的话,并不能比较出课程1与课程2哪个成绩高啊?
只是不会写第三个SQL
1:找出所有选修了‘王刚’老师所授课的学生学号和姓名
select a.id,a.name from student a,course b,sc c
where a.id=c.id and b.id=c.cid
and b.cteacher=‘王刚’2:找出有一门成绩低于60分的学生的平均成绩
select sid,avg(score) from sc
where sid in
(
select sid from
(select sid ,count(*) sum from sc where score < 60)
where sum =1)
group by sid;3:找出选修了课程1和课程2,并且课程1的分数比课程2的分数高的学生姓名
select name from student
where id in ( select a.id from student a,course b,sc c
where a.id=c.id and b.id=c.cid
and b.name in ('课程1','课程2'))
and id in ( select id from
(select c.sid as id,c.score as score from sc c,course b where b.id=c.cid and b.name='课程1') d,
(select c.sid as id,c.score as score from sc c,course b where b.id=c.cid and b.name='课程1') e
where d.id =e.id and d.score > e.score )没有实际测试
厉害,
改了改第三个,如下select a.name, from
(select sid,
sum(decode(b.name, '课程1', score, 0)) score1,
sum(decode(b.name, '课程2', score, 0)) score2
from
sc a, course b
where a.cid=b.id
and
(b.name in ('课程1','课程2'))
group by sid
having count(1)=2) b,
student a
where b.score1>b.score2 and a.id=b.sid;没有测试,自己试试哟。
这个地方是什么意思?