/*
* union
* distinct 将合并后的结果集进行去重;(数据量大的时候影响性能)
* all 保留结果集中的重复记录
* 默认是distinct
* */select e.emp_id
from excellent_emp e
where e."year"=2018uniondistinctselect
e.emp_id
from excellent_emp e
where e."year"=2019;
交集
-- 2018和2019连续获得优秀员工称号的员工/*
* intersect 返回两个查询结果中的共同部分,
* 即同时出现在第一个查询结果和第二个查询结果中的数据
* distinct 将合并后的结果集进行去重
* all 保留结果集中的重复记录
* */select
e.emp_id
from
cps.public.excellent_emp e
where
e."year"=2018intersectdistinctselect
e2.emp_id
from
cps.public.excellent_emp e2
where
e2."year"=2019;
差集
/*
* 2019年获得优秀员工称号的新晋优秀员工
* except:返回出现在第一个查询结果中,但不在第二个查询结果中的数据
* distinct 将合并后的结果集进行去重
* all 保留结果集中的重复记录
* 默认是distinct
*/select
e.emp_id
from
excellent_emp e
where
e."year"=2019exceptselect
e2.emp_id
from
excellent_emp e2
where
e2."year"=2018;