Oracle 集合操作

SQL结果集的集合操作:并集、交集、差集

image.png

A.UNION并集

1.UNION:去除重复记录

结果中的总记录数可能 < employees的记录数+job_history的记录数

  1. SELECT employee_id, job_id
  2. FROM employees
  3. UNION
  4. SELECT employee_id, job_id
  5. FROM job_history;

2.UNION ALL 保留重复记录

结果中的总记录数一定 = employees的记录数+job_history的记录数

  1. SELECT employee_id, job_id, department_id
  2. FROM employees
  3. UNION ALL
  4. SELECT employee_id, job_id, department_id
  5. FROM job_history
  6. ORDER BY employee_id;

B.INTERSECT交集

  1. SELECT employee_id, job_id
  2. FROM employees
  3. INTERSECT
  4. SELECT employee_id, job_id
  5. FROM job_history;

C.MINUS差集

  1. SELECT employee_id,job_id
  2. FROM employees
  3. MINUS
  4. SELECT employee_id,job_id
  5. FROM job_history;