4/19 , 4/20 , 4/21 필기

konut ko·2023년 4월 19일
1

더존비즈온5기

목록 보기
15/46
post-thumbnail

join

비등가 조인(non-equi)

사원의 등급 (하나의 컬럼으로 매핑 안되요) >> 컬럼 2개
-- 비등가 조인(non-equi) 1:1
-- 문법은 등가와 동일
join 테이블A on 테이블B on A.컬럼 between B.컬럼1 and B.컬럼2

select * -- e.empno, e.ename, d.grade, e.sal
from emp e join salgrade d 
on e.sal between d.losal and d.hisal;

자기참조

서브쿼리

서브쿼리 종류

대량 데이터 삽입

--TIP)
create table temp4(id number);
create table temp5(num number);

desc temp4;
desc temp5;
 
insert into temp4(id) values(1);
insert into temp4(id) values(2);
insert into temp4(id) values(3);
insert into temp4(id) values(4);
insert into temp4(id) values(5);
insert into temp4(id) values(6);
insert into temp4(id) values(7);
insert into temp4(id) values(8);
insert into temp4(id) values(9);
insert into temp4(id) values(10);
commit;
         
select * from temp4;
--대량 데이터 삽입하기
select * from temp5;
--temp4 테이블에 모든 데이터를 temp5  넣고 싶어요
--insert into 테이블명(컬럼리스트) values ...
--insert into 테이블명(컬럼리스트) select 절 *****************^^
-------------------------------------------------?????????????????????????????????????????????????????????????
insert into temp5(num)
select id from temp4; --대량 데이터 삽입

기타

복습

like

-- 3. 이름에 'A'가 들어가는 사원들의 이름과 부서이름을 출력하라.

select e.ename, d.dname
from emp e join dept d
on e.deptno = d.deptno
where ename like '%A%';

오전 퀴즈

-- 6. 커미션이 책정된 사원들의 사원번호, 이름, 연봉, 연봉+커미션,
-- 급여등급을 출력하되, 각각의 컬럼명을 '사원번호', '사원이름',
-- '연봉','실급여', '급여등급'으로 하여 출력하라.
--(비등가 ) 1 : 1 매핑 대는 컬럼이 없다

select empno as "사원번호", ename as "사원이름", sal as "연봉", (sal+comm) as "실급여", grade as "급여등급"
from emp e join salgrade s on e.sal between s.losal and s.hisal 
where e.comm is not null;


-- 7. 부서번호가 10번인 사원들의 부서번호, 부서이름, 사원이름,
-- 월급, 급여등급을 출력하라.

select d.deptno, d.dname, e.ename, e.sal, s.grade
from emp e left join dept d on e.deptno = d.deptno
                join salgrade s on e.sal between s.losal and s.hisal
where d.deptno = 10;

-- 8. 부서번호가 10번, 20번인 사원들의 부서번호, 부서이름,
-- 사원이름, 월급, 급여등급을 출력하라. 그리고 그 출력된
-- 결과물을 부서번호가 낮은 순으로, 월급이 높은 순으로
-- 정렬하라.
select * from emp ;
select * from dept;
select * from salgrade;

select * 
from emp e left join dept d on e.deptno = d.deptno
                join salgrade s on e.sal between s.losal and s.hisal
where d.deptno in(10,20)
order by d.deptno asc, e.sal desc;

Quiz

문제

자기 부서의 평균 월급보다 더 많은 월급을 받는 사원의 사번, 이름, 부서번호, 부서별 평균 월급 출력하세요

풀이1 - 인라인 뷰

select a.empno, a.ename, a.deptno, a.sal, b.avgsal 
from emp a join (select deptno, trunc(avg(sal), 0) as avgsal 
				from emp 
                group by deptno) b 
			on a.deptno = b.deptno
where a.sal > b.avgsal;

풀이2 - 인라인 뷰

select a.empno, a.ename, a.deptno, a.sal, b.avgsal
from emp a
    ,(select  d.deptno, trunc(avg(sal)) as avgsal
    from emp e left join dept d on e.deptno = d.deptno
    group by d.deptno) b
where a.deptno = b.deptno and a.sal > b.avgsal;

4/20 목 캡쳐들


4/21 금 캡쳐들

profile
보초딩코라 틀린 내용 있을 수도 있습니다. 댓글 지적 환영

0개의 댓글