SQL 기초 실무/중급편Ⅰ, Ⅱ

23.09.25. SQL Developer 예제 88 ~ 92번

잇꼬 2023. 9. 28. 15:21
728x90
반응형
SMALL

88. 서브 쿼리를 사용하여 데이터 합치기 

-- 예제88. 다음과 같이 부서테이블에 부서번호별 토탈월급 데이터가 입력되게 하시오.
alter table dept
    add sumsal number(10) ;
    
select * from dept ;

select deptno, sum(sal)
    from emp 
    group by deptno ;
    
merge into dept d
using ( select deptno, sum(sal) as sumsal
            from emp
            group by deptno ) v
on ( d.deptno = v.deptno) 
when matched then
update set d.sumsal = v.sumsal ;

select * from dept ;

-- 예제88_문제1. 부서 테이블에 cnt 라는 컬럼을 추가하고 해당 부서번호의 인원수로 값을 갱신하시오.
alter table dept
    add cnt number(10) ;

select deptno, count(*)
    from emp 
    group by deptno ;
    
merge into dept d
using ( select deptno, count(*) cnt
            from emp 
            group by deptno ) c
on ( d.deptno = c.deptno ) 
when matched then
update set d.cnt = c.cnt ;

select * from dept ;

 

89. 계층형 질의문으로 서열을 주고 데이터 출력하기 ①  

-- 예제89. 다음과 같이 사원 테이블의 서열(level) 을 출력하세요
select rpad(' ', level*3) || ename as ename, sal, level, job 
    from emp 
    start with ename = 'KING' 
    connect by prior empno = mgr ;
    
-- 예제89_문제1. 서열이 2위인 사원들의 이름과 서열과 직업을 출력하세요.
select rpad(' ', level*3) || ename as ename, sal, level, job 
    from emp 
    where level = 2
    start with ename = 'KING' 
    connect by prior empno = mgr ;

 

90. 계층형 질의문으로 서열을 주고 데이터 출력하기 ②

-- 예제90. 서열 순서를 유지한 상태에서 BLAKE와 BLAKE의 팀원들이 출력되지 않게 하시오.
select rpad(' ', level*3) || ename as ename, sal, level, job 
    from emp 
    start with ename = 'KING' 
    connect by prior empno = mgr and ename != 'BLAKE' ;
    
-- 예제90_문제1. 사원 테이블에서 서열 순서대로 이름과 서열과 월급과 직업을 출력하는데 SCOTT과 SCOTT의 팀원과 FORD와 FORD의 팀원들이 출력되지 않게 하세요.
select rpad(' ', level*3) || ename as ename, level, sal, job 
    from emp 
    start with ename = 'KING' 
    connect by prior empno = mgr and ename != 'SCOTT' and ename != 'FORD' ;
/* 같은 실행 결과이나 가독성을 위해 아래 쿼리문을 실행하는 것이 더 좋다. */
select rpad(' ', level*3) || ename as ename, level, sal, job 
    from emp 
    start with ename = 'KING' 
    connect by prior empno = mgr and ename not in ( 'SCOTT', 'FORD' ) ;

 

91. 계층형 질의문으로 서열을 주고 데이터 출력하기 ③

-- 예제91. 서열 순서를 유지한 상태에서 월급이 높은 순서대로 출력하세요.
select rpad(' ', level*3) || ename as employee, level, sal 
    from emp 
    start with ename = 'KING' 
    connect by prior empno = mgr 
    order siblings by sal desc ;
    
-- 예제91_문제1. BLAKE와  BLAKE의 팀원들만 출력하는데 서열을 유지한 상태에서 월급이 낮은 사원부터 출력하세요.
select rpad(' ', level*3) || ename as ename, level, sal 
    from emp 
    start with ename = 'BLAKE' 
    connect by prior empno = mgr
    order siblings by sal asc ;

 

92. 계층형 질의문으로 서열을 주고 데이터 출력하기 ④

-- 예제92. 다음과 같이 자기의 서열순서가 어떻게 되는지 출력하시오.
select ename, sys_connect_by_path(ename, '/') as path
    from emp 
    start with ename = 'KING' 
    connect by prior empno = mgr ;
    
-- 예제92_문제1. 다음과 같이 월급도 같이 출력되게 하세요.
select ename || '(' || sal || ')' , sys_connect_by_path(ename || '(' || sal || ')' , '/') as path 
    from emp 
    start with ename = 'KING' 
    connect by prior empno = mgr ;

 

728x90
반응형
LIST