oracle嵌套查询

试验目的:
掌握嵌套查询的使用方法,进一步加深对SELECT语句的理解。
关系模式如下:
student(sno,sname,ssex,sage,sdept)
Course(cno,cname,cpno ,ccredit)
SC(sno,cno,grade)

S(SNO,SNAME,STATUS,CITY)
P(PNO,PNAME,COLOR,WEIGHT)
J(JNO,JNAME,CITY)
SPJ(SNO,PNO,JNO,QTY)

嵌套查询试验(1)
要求使用比较(如:“=”,“>”等)谓词完成。

与“刘晨”年龄相同的女学姓名
select *
from student 
where ssex='女' and sage=
(select sage
from student
where sname='刘晨');
年龄最大的男生姓名
select *
from student
where ssex='男' and sage>=all
(select sage
 from student
 );
与“刘晨”不是一个系的学生姓名
select *
from student
where sdept not in
(select sdept
from student
where sname='刘晨');
查询“IS”系年龄最大的学生
select *
from student
where  sdept='IS' and sage > all
(select sage
from student
);

嵌套查询试验(2)
要求同时使用IN及EXITS谓词分别完成。

查询没有选择任何课程的学生
select *
from student
where not exists
(select *
from course,sc
where sc.sno=student.sno and course.cno=sc.cno);
查询没有选择1号课的学生
select *
from student
where not exists
(select *
from course,sc
where sc.sno=student.sno and course.cno=sc.cno and sc.cno='1');
查询没有选择“数据库”课程的学生
select *
from student
where not exists
(select *
from course,sc
where sc.sno=student.sno and course.cno=sc.cno and course.cname='数据库');
查询“200215121”号学生没有选择的课程
select *
from course
where not exists
(select *
from sc,student
where sc.sno=student.sno and course.cno=sc.cno and student.sno='200215121');
没有参与选课的男生的学生姓名
select *
from student
where ssex='男'and not exists
(select *
from sc,course
where sc.sno=student.sno and course.cno=sc.cno );






没有使用“P2”零件的工程名称
select *
from j
where not exists
(select *
from spj
where spj.jno=j.jno and spj.pno='p2');
没有使用“S2”供应商零件的工程名称
select *
from j
where not exists
(select *
from spj
where spj.jno=j.jno and spj.sno='s2');
查询没有使用了红色零件的工程编号和工程名称
select *
from j
where not exists
(select *
from spj,p
where spj.jno=j.jno and p.pno=spj.pno and p.color='红');
没有人选修的课程
select *
from course
where not exists
(select *
from sc,student
where sc.sno=student.sno and course.cno=sc.cno );

嵌套查询试验(3)
查询选择了所有课程的学生学号和姓名;
select *
from student
where not exists
(select  *
 from Course
 where not exists
(select *
 from sc
 where  sno=student.sno and cno=Course.cno ));
查询使用了全部零件的工程编号和工程名称

select *
from j x
where  exists
(select *
 from j y
where not exists
(select *
 from spj
 where y.jno=spj.jno and x.jno=spj.jno));
*查询使用了全部红色零件的工程编号和工程名称
select *
from j 
where  exists
(select *
 from p
where p.color='红'and not exists
(select *
 from spj
 where j.jno=spj.jno and p.pno=spj.pno));

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值