oracle笔记set运算符及练习

/*

set运算符及练习
将多个查询用 SET 操作符连接组成一个新的查询
1.UNION/UNION ALL (可以理解成数学中的取并集)

UNION 取并集的时候,会把2个集合的相同元素去重(会去掉重复的元素)
UNION ALL 取并集的时候,不会把2个集合的相同元素去重(实际上就是把2个集合中的所有元素都取到)

2.INTERSECT (可以理解成数学中的取交集)
INTERSECT 操作符返回两个结果集的交集

3.MINUS (可以理解成数学中的取差集)
MINUS操作符:返回两个结果集的差集,比如:集合A{2,6,8,9,12,29}和集合B{8,9,17,23,25,29}的差集是{2,6,12}(即把A集合里的与B集合中都有的元素剔除,剩下的元素就是差集了)

排序:ORDER BY (以前学过的老知识了,就是排序)

*/
--
CREATE TABLE employees01
AS
SELECT * FROM employees emp
WHERE emp.department_id IN (70, 80)
--
CREATE TABLE employees02
AS
SELECT * FROM employees emp
WHERE emp.department_id IN (80, 90)

--
SELECT * FROM employees01
--
SELECT * FROM employees02

/*
UNION 操作符返回两个查询的结果集的并集

UNION ALL 操作符返回两个查询的结果集的并集。对于两个结果集的重复部分,不去重


*/

--注意:要一一对应(字段个数,字段顺序,字段数据类型等等要一一对应)
SELECT emp01.employee_id, emp01.department_id
FROM employees01 emp01 --可以理解成数学中的集合A
UNION --UNION可以理解成数学中的操作符,如 A并B (会去重,把公有的部分去重)
SELECT emp02.employee_id, emp02.department_id
FROM employees02 emp02 --可以理解成数学中的集合B


--注意:要一一对应(字段个数,字段顺序,字段数据类型等等要一一对应)
SELECT emp01.employee_id, emp01.department_id
FROM employees01 emp01 --可以理解成数学中的集合A
UNION ALL --UNION可以理解成数学中的操作符,如 A并B (不会去重)
SELECT emp02.employee_id, emp02.department_id
FROM employees02 emp02 --可以理解成数学中的集合B

--
SELECT emp01.employee_id AS emp_id, emp01.department_id --取别名(一般别名取在前面这个集合)
FROM employees01 emp01 
UNION ALL 
SELECT emp02.employee_id, emp02.department_id dep_id --这里也取了一个别名(一般在后面这个集合中取别名是没效果的)
FROM employees02 emp02 
ORDER BY emp_id ASC -- 可以使用别名排序(如果不写ORDER BY排序的话,默认是按照SELECT后面的第一个字段排序的)

--

SELECT emp01.employee_id AS emp_id, emp01.department_id 
FROM employees01 emp01 
INTERSECT --取交集
SELECT emp02.employee_id, emp02.department_id dep_id 
FROM employees02 emp02 
ORDER BY emp_id ASC

--
SELECT emp01.employee_id AS emp_id, emp01.department_id 
FROM employees01 emp01 --(70和80号部门)
MINUS --取差集(所以结果为70号部门)
SELECT emp02.employee_id, emp02.department_id dep_id 
FROM employees02 emp02 --(80和90号部门) 
ORDER BY emp_id ASC

--
SELECT emp01.employee_id AS emp_id, emp01.department_id 
FROM employees01 emp01 
UNION 
SELECT emp02.employee_id, emp02.department_id dep_id 
FROM employees02 emp02 
ORDER BY 1 ASC --按照第1列排序

--
SELECT emp01.employee_id AS emp_id, emp01.department_id 
FROM employees01 emp01 
UNION 
SELECT emp02.employee_id, emp02.department_id dep_id 
FROM employees02 emp02 
ORDER BY 2 ASC --按照第2列排序

--如下是为了保证字段顺序,字段个数,字段数据类型一致,一一对应
SELECT emp01.employee_id AS emp_id, emp01.department_id, to_char(NULL)
FROM employees01 emp01 
UNION 
SELECT to_number(NULL), dep.department_id, dep.department_name
FROM departments dep

--
SELECT emp01.employee_id AS emp_id, emp01.department_id, to_char(NULL)
FROM employees01 emp01 
UNION 
SELECT 666, dep.department_id, dep.department_name
FROM departments dep

--
SELECT emp01.employee_id AS emp_id, emp01.department_id, '呵呵测试'
FROM employees01 emp01 
UNION 
SELECT to_number(NULL), dep.department_id, dep.department_name
FROM departments dep

--
SELECT 'i want to'
FROM dual
UNION
SELECT 'at hometown'
FROM dual 
UNION
SELECT 'study and work'
FROM dual --默认按照第1列排序(升序)

--增加一列
SELECT 'i want to' AS "my dream", 1 "我是第2列"
FROM dual
UNION
SELECT 'at hometown', 3
FROM dual 
UNION
SELECT 'study and work', 2
FROM dual 
ORDER BY 2 ASC --按照第2列排序(升序)

--COLUMN my_dummy noprint (这句话的意思不打印/不显示这一列,这是一个plsql)
SELECT 'i want to' AS "my dream", 1 my_dummy
FROM dual
UNION
SELECT 'at hometown', 3
FROM dual 
UNION
SELECT 'study and work', 2
FROM dual 
ORDER BY 2 ASC --按照第2列排序(升序)
--

/*

练习题

*/


--查询部门的部门号,其中不包括job_id是”ST_CLERK”的部门号

--方式1使用MINUS解决
SELECT department_id 
FROM departments
MINUS
SELECT department_id 
FROM employees
WHERE job_id = 'ST_CLERK' 

--方式2,使用子查询解决
SELECT dep.department_id
FROM departments dep
WHERE dep.department_id NOT IN (
SELECT DISTINCT emp.department_id  --加不加DISTINCT(去重)都可以
FROM employees emp
WHERE emp.job_id = 'ST_CLERK'
)



--查询20, 90, 70号部门的job_id,department_id并且department_id按20, 90, 70的顺序排列

SELECT emp.job_id, emp.department_id
FROM employees emp
WHERE emp.department_id IN (20, 90, 70)
--
SELECT emp.job_id, emp.department_id, 1 third_dummy --如果不想显示这列,可以使用COLUMN third_dummy noprint这句话
FROM employees emp
WHERE emp.department_id IN (20)
UNION
SELECT emp.job_id, emp.department_id, 2
FROM employees emp
WHERE emp.department_id IN (90)
UNION
SELECT emp.job_id, emp.department_id, 3
FROM employees emp
WHERE emp.department_id IN (70)
ORDER BY 3 ASC --按照第3列排序


--查询所有员工的last_name ,department_id 和department_name
--注意:要一一对应(字段个数,字段顺序,字段的数据类型)
SELECT emp.last_name, emp.department_id, to_char(NULL)
FROM employees emp
UNION 
SELECT to_char(NULL), dep.department_id, dep.department_name
FROM departments dep

 

你可能感兴趣的:(oracle笔记set运算符及练习)