1.where절이 앞에절보다 먼저실행된다 << 앞의 얼라이스 별칭쓴것을 where절에서 사용할수없다는 의미임
2집합연산자
* intersect -교차
minus -
union all - select구문으로 조회된결과 모두합침< 중복도 합침이 앞에절보다 먼저실행된다 << 앞의 얼라이스 별칭쓴것을 where절에서 사용할수없다는 의미임
2집합연산자
* intersect -교차
minus -
union all - select구문으로 조회된결과 모두합침< 중복도 합침
union - select구문으로 조회된결과 모두합침< 중복된결과뺴고
--
--집합연산자 (위 아래 column개수가 정확히 일치해야 함)
--집합연산자 union(합집합 중복x) , union all 중복0 , intersect교집합 , minus 차집합,
select employee_id, first_name from employees where hire_date like '04%'
union
select employee_id, first_name from employees where department_id = 20; --micheal,pat 빠졋나봄
select employee_id, first_name from employees where hire_date like '04%'
union all
select employee_id, first_name from employees where department_id = 20; -- union보다 1개더많음
select employee_id, first_name from employees where hire_date like '04%'
intersect
select employee_id, first_name from employees where department_id = 20; -- 교집합 즉 중복값 찾아냄
select employee_id, first_name from employees where hire_date like '04%'
minus
select employee_id, first_name from employees where department_id = 20; -- 처음선언한 행에서 다음선언한행의 중복값만빼서 출력함
--union,union all은 듀얼 테이블로 만들어서 사용하는 경우가 많다.
select employee_id,first_name, salary from employees where department_id = 50
union all
select employee_id,first_name, salary from employees where salary > 100
union all
select 300,'hong',10000 from dual
union all
select 400 , 'asd' , 20000 from dual;
--
sum max min 이런것도있음
--
--그룹함수 sum,avg,min,max,count
select count(*) from employees; -- 전체행수
select sum(salary), min(salary),max(salary), count(salary), trunc(avg(salary)) from employees;
select count(commission_pct) from employees; -- null이 아닌행수
select count(manager_id) from employees;
select avg(salary) from employees; --null이아닌 행수
--group by절
select * from employees;
select job_id from employees; -- job_id다출력
select job_id from employees group by job_id; --job_id그룹별 출력
select job_id, avg(salary) from employees group by job_id; -- job_id그룹별 연봉평균 출력
select department_id, avg(salary) , sum(salary), count(salary) from employees group by department_id;
--주의할점
--그룹핑이 되지 않으면 그룹함수와 일반컬럼을 동시에 사용하지 못함!중요
select count(*), job_id from employees group by job_id;
--group by 절을사용할 때 group절에 묶이지 않으면 다른 컬럼 조회를 할 수없음
select job_id from employees group by department_id;
--2개 이상의 group by절
select * from employees;
select department_id , job_id from employees group by department_id, job_id order by department_id desc; -- 100 50이렇게 나올걸 100머머,100머머,50머머
--이렇게 나온다 먼저 그룹핑된것에서 세부그룹핑 순으로 출력된다
select department_id, job_id , avg(salary)
from employees
group by department_id, job_id
order by department_id;
--group by절에 조건 having (where절은 일반행에 대한 조건)
select department_id ,sum(salary)
from employees
--where sun(salary) > 5000 --안됨
group by department_id
having sum(salary) > 100000;
--job id 별 개수
select job_id, count(*)
from employees
group by job_id
having count(*) > 1
order by job_id;
select department_id from employees;
-- 부서아이디가 50이상인 행을 그룹핑 하고, 그룹평균중 5000이상만 조회,정렬
select department_id, avg(salary)
from employees
where department_id >= 50
group by department_id
having avg(salary) >= 5000
order by avg(salary) desc;
select department_id ,first_name, count(*) over() from employees; -- count(*) 는 전체행개수를 확인하는 구문으로 단독으로쓰거나 그룹형태로만 가능하지만 over()를 통하여 사용하는 경우도있음
--count(*) over() 예외적으로 총행의 수를 붙여서 출력
--연습문제
--사원테이블에서 job_id사원수를 구하시오
--사원테이블에서 job_id별 월급의 평균을 구하세요, 월급의 평군 순으로 내림차순 정렬하세요
select count(*) from employees;
select job_id, avg(salary)
from employees
group by job_id
order by avg(salary);
--문제2
--사원 테이블에서 입사년도 사원수를 구하세요
select to_char(hire_date, 'YYYY'), count(*)
from employees
group by hire_date;
--문제3
--급여가 1000이상인 사원들의 부서별 평균급여를 출력하세요 단 부서 평균급여가 2000이상인 부서만 출력
select department_id, trunc(avg(salary),2)
from employees
where salary >= 1000
group by department_id
having avg(salary) >= 2000;
--문제4 사원테이블에서 commission_pct컬럼이 null이아닌 사람들의
--department_id,salary의 평균,합계 , count를 구합니다
--조건1)월급의 평균은 커미션을 적용시킨 월급입니다.
--조건2)평균은소수 2째 자리에서 절삭하세요
select nvl(to_char(department_id),'부서없음'), trunc(avg(salary + salary * commission_pct),2),sum(salary + salary * commission_pct), count(salary)
from employees
where commission_pct is not null
group by department_id;
--