상세 컨텐츠

본문 제목

집합 연산자 (합집합, 차집합, 교집합 등)

DataBase/Oracle

by H_Develop 2022. 6. 8. 22:48

본문

두테이블에서 select 문의 컬럼_명, 컬럼_위치, 컬럼_수가 동일해야한다.
컬렴_명이 같지 않다면, alios를 활용하여 억지로라도 같게 해주어야 한다.
INION(합집합), UNION ALL, MINUS(차집합),  INTERSECT(교집합)

UNION 합집합

 

select employee_id, department_id from employees
where salary > 10000
UNION
select employee_id, department_id from employees
where department_id = 100
// 합집합을 얘기함, salary 10000이상인 사람들과 department_id=100인 사람들 모두 
// 두 조건중 하나라도 충족하면 출력

// select 와 from 은 동일하게 하여야한다.
// salary가 만불인 이상인 사람과, department_id 가 100인 사람들
// 둘 중 하나의 조건만 만족해도 가능

그냥 select employee_id, department_id from employees
where salary > 10000 and department_id = 100
// 이것은 두 조건을 충족하는 값만 추출(교집합)

select employee_id, salary, department_id from employees
where salary > 10000
UNION all
select employee_id, salary, department_id from employees
where department_id = 100
order by employee_id
// UNINON all을 하면 중복된 데이터 값도 같이 나옴. / employee_id가 108번인 애 처럼
// 그래서 잘 사용하지 않음. 중복 데이터는 좋지 않음.

EMPLOYEE_ID SALARY DEPARTMENT_ID
100			24000  90
101			17000  90
102			17000  90
108			12000  100
108			12000  100



MINUS 차집합

 

select employee_id, salary, department_id from employees
where salary > 10000
MINUS
select employee_id, salary, department_id from employees
where department_id = '100'

// salary 가 10000 초과이며, department_id 가 100인 부서를 제외한 나머지를 출력.



INTERSECT 교집합

select employee_id, salary, department_id from employees
where salary > 10000
INTERSECT
select employee_id, salary, department_id from employees
where department_id = '100'

// salary 가 10000 초과이며, department_id 도 100 부서인 사람을 출력

'DataBase > Oracle' 카테고리의 다른 글

계층적 질의  (0) 2022.06.09
GROUP BY 함수 확장(group by rollup, group by cube)  (0) 2022.06.09
SUB QUERY  (0) 2022.06.08
JOIN (Oracle JOIN, ANSI JOIN, INNER JOIN, OUTER JOIN, SELF JOIN)  (0) 2022.06.08
AUTO INCREMENT (SEQUENCE)  (0) 2022.06.08

관련글 더보기