数据库作业

数据库作业7集合查询基于派生表的查询数据更新1.插入数据空值的处理视图集合查询select语句查询结果是元组的集合,所以多个select语句的结果可进行集合操作。集合操作主要包括并操作union、交操作intersect和差操作except。3.64 查询计算机科学系的学生及年龄不大于19岁的学生select *from Studentwhere Sdept='CS'union//两个条件取并集select *from Studentwhere Sage<=19;注:系统
摘要由CSDN通过智能技术生成

集合查询

select语句查询结果是元组的集合,所以多个select语句的结果可进行集合操作。集合操作主要包括并操作union、交操作intersect和差操作except。

3.64 查询计算机科学系的学生及年龄不大于19岁的学生

select *
from Student
where Sdept='CS'
union//两个条件取并集
select *
from Student
where Sage<=19;

注:系统会自动去掉重复元组,若要保留重复元组可用union all操作符

3.65 查询选修了课程1或者选修了课程2的学生

select Sno
from SC
where Cno='1'
union//两个条件取并集
select Sno
from SC
where Cno='2';

3.66 查询计算机科学系的学生与年龄不大于19岁的学生的交集

select *
from Student
where Sdept='CS'
intersect//两个条件取交集
select *
from Student
where Sage<=19;

它等价于

select *
from Student
where Sdept='CS' and Sage<=19;

3.67 查询既选修了课程1又选修了课程2的学生

select Sno
from SC
where Cno='1'
intersect//两个条件取交集
select Sno
from SC
where Cno='2';

也可以表示为

select Sno
from SC
where Cno='1' and Sno in(
select Sno
from SC
where Cno='2');

3.68 查询计算机科学系中年龄大于19岁的学生的差集

select *
from Student
where Sdept='CS'
except//两个条件取差集
select *
from Student
where Sage<=19;

基于派生表的查询

子查询不仅可以出现在where子句中,还可以出现在from子句中,这时子查询生成的临时派生表成为主查询的查询对象

  • 3.57也可这样做
select Sno,Cno
from SC,
(select Sno,Avg(Grade)
from SC 
group by Sno) as Avg_sc(avg_sno,avg_grade)//派生表
where SC.Sno=Avg_sc.avg_sno 
  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值