SQL语言学习随手记——union、intersect、except

本文介绍了SQL中的三种集合运算:并运算(union)、交运算(intersect)和差运算(except)。通过具体例子展示了如何使用这些运算来获取不同学期开设课程的数据。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.并运算——union

e.p.

列出在2009年秋季开课,或在2010年春季开课或两个学期都开课的所有课程:

(select course_id from section where semester = 'Fall' and year = 2009)

  union

(select course_id from section where semester = 'Spring' and year = 2010);

注:union运算自动去除重复,如果想保留所有重复,就必须用union all 代替 union。


2.交运算——intersect

e.p.

列出在2009年秋季和2010年春季同时开课的所有课程集合:

(select course_id from section where semester = 'Fall' and year = 2009)

 intersect

(select course_id from section where semester = 'Spring' and year = 2010);

注:intersect运算自动去除重复,如果想保留所有重复,就必须用intersect all 代替 intersect。


3.差运算——except

e.p.

列出在2009年秋季学期开课,但不在2010年春季学期开课的所有课程集合:

(select course_id from section where semester = 'Fall' and year = 2009)

 except

(select course_id from section where semester = 'Spring' and year = 2010);

注:except运算自动去除重复,如果想保留所有重复,就必须用except all 代替except

       except运算——从其第一个输入中输出所有不出现在第二个输入中的元祖,也即它执行集差操作。



评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值