DB/오라클 SQL문

25. 서브쿼리(쿼리안에 또다른 쿼리 담김) | 문제 7개

DEV-HJ 2022. 2. 23. 10:33
반응형

--professor에서 조교수의 bonus를 100만원으로 인상하시오
update professor set bonus=100 where position='조교수';

--dept2에서 서울본사, 포항지사로 변경하시오
update dept2 set area='서울본사' where area='서울지사';
update dept2 set area='포항지사' where area='포항본사';

rollback;


--서브쿼리...쿼리안에 또다른 쿼리 담김
--select 컬럼1, 컬럼2
--from 테이블 
--where 조건연산자(select 컬럼명 from 테이블 where 또 다른조건);
--메인쿼리 (서브쿼리)

--Q. emp에서 'scott' 보다 급여를 많이 받는 사람의 이름과 급여를 출력하라
select ename,sal
from emp 
where sal>(select sal from emp where ename='SCOTT');

--서브쿼리에는 order by 못 옴
--Q2. emp에서 이름이 WARD인 사람의 MGR과 같은값을 가진 사람의 이름,mgr,급여를 구하시오
select ename, MGR,sal
from emp
where MGR=(select MGR from emp where ename='WARD');

--Q3. emp에서 평균연봉보다 더 많이 받는 사람들의 전체목록 출력하기
select * from emp where sal>(select avg(sal) from emp);

--Q4. professor ...조교수의 평균급여 (pay)보다 급여가 낮은 사람의 교수번호, 이름, 급여를 구하시오
select profno, name, pay 
from professor 
where pay<(select avg(pay) from professor where position='조교수');

--Q.5 student에서 4학년의 평균키보다 큰사람의 이름, 아이디, 키를 구하시오
select name, id, height
from student
where height>(select avg(height) from student where grade=4);

--Q6. emp에서 s로 시작하는 사람과 같은부서 사람들의 부서번호와 이름을 출력하시오
select deptno 부서번호, ename 이름
from emp 
where deptno IN (select deptno from emp where ename like 'S%');

--두가지 조건의 서브쿼리
--Q7. professor 에서 심슨교수와 같은 입사일에 입사한 교수들 중에서 조인형 교수보다 월급을 적게받는 교수의 이름,급여,입사일 출력
--이렇게 서브쿼리 줄 일은 잘 없지만
select name, pay, hiredate
from professor
where hiredate=(select hiredate from professor where name='심슨')
and pay<(select pay from professor where name='조인형');






반응형