SQL
Day3
Jimyeung
2021. 7. 17. 11:51
728x90
Day3 Quiz
Q1.emp테이블에서 급여가 1500이상 3000이하이면서 커미션이 Null 이거나 0인 사원을 검색하세요.
select *
from emp
where (sal between 1500 and 3000) and (comm is null or comm = 0);
Q2.emp테이블에서 12월에 입사한 사원을 입사일 기준으로 검색하세요.
select *
from emp
where to_char(hiredate,'MM') = '12';
Q3.EMP테이블에서, 이름(ENAME)이 'S'또는 'A'로 시작하는 사원을 검색하시오.
select empno, ename, sal, deptno
from emp
where ename like 'S%' or ename like 'A%';
Q4.EMP테이블에서, 급여(SAL)보다 커미션(COMM)을 많이 받는 사원을 검색하시오.
select empno, ename, sal, comm,deptno
from emp
where sal < comm ;
Q5.EMP테이블에서 급여(SAL)가 2000보다 작고, 3000보다 많은 사원을 검색하시오.
select empno, ename sal, deptno
from emp
where sal < 2000 or sal >3000;
Q6.EMP테이블에서 커미션(CoMM)을 기준으로 내림차순 정련된 결과를 검색하시오. (단, 커미션이 Null인행은 마지막애 검색)
select empno, ename, sal, comm
from emp
order by comm desc nulls last;
Q7.EMP 테이블에서, 매니저(MGR)가 없는 사원은 'NO MANAGER'의 문자를 다음과 같이 검색하시오.
select empno, ename, job, nvl(to_char(mgr),'no manager')
from emp; //nvl 하나의 타입만 가짐
Q8.EMP테이블에서 DEPTNO, JOB컬럽으로 Grouping 된 급여의 합계를 다음과 같이 검색하시오.
select deptno,
sum(decode(job,'ANALYST',sal)) as ANALYST,
sum(decode(job,'CLERK',sal)) as CLERK,
sum(decode(job,'MANAGER',sal)) as MANAGER,
sum(decode(job,'PRESIDENT',sal)) as PRESIDENT,
sum(decode(job,'SALESMAN',sal)) as SALESMAN
from emp
group by deptno
order by deptno//----------------------rollup(deptno) 사용시 총계 확인가능
Q9.EMP테이블에서, 'JONES'보다 더 많은 급여를 받는 사원을 검색하시오.
select sal
from emp
where ename = 'JONES';
select empno, ename, sal
from emp
where sal >(select sal
from emp
where ename = 'JONES');
Q10. DEPT, EMP 테이블을 사용하여 부서별 급여의 합계를 검색하시오, 근무하는 사원이 없는 부서도 함께 표시합니다.
select d.deptno,dname,sum(sal)
from dept d join emp e
on d.deptno = e.deptno(+)
group by d.deptno, dname
order by d.deptno ;
Q11.DEPT, EMP테이블을 사용하여 각 부서의 소속 사원 유무를 확인하는 검색결과를 만드시오.
select distinct d.deptno,d.dname,loc, decode(e.deptno, null, 'no','yes')as emp
from dept d join emp e
on d.deptno = e.deptno(+)
order by d.deptno;
Q12.countries, employees 테이블을 이용하여, 'Canada'에서 근무 중인 사원 정보를 다음과 같이 검색하시오. 만약 추가적으로 필요한 테이블이 더 있다면 함께사용(department,location)
select e.first_name, e.last_name, e.salary, e.job_id, c.country_name
from employees e,
departments d,
locations l,
countries c
where e.department_id = d.department_id
and d.location_id = l.location_id
and l.country_id = c.country_id
and country_name = 'Canada';
Q13. EMP 테이블에서 1981년도에 입사한 사원들을 입사 월별로 인원수를 검색하시오.
select b.hire, nvl(a.cnt,0) cnt
from (select to_char(hiredate,'yyyy/mm') as hire, count(*) as cnt
from emp
where hiredate between to_date('1981/01/01','yyyy/mm/dd')
and to_date('1982/01/01','yyyy/mm/dd')-(1/86400)
group by to_char(hiredate,'yyyy/mm')) a
,(select '1981/'||lpad(level,2,0) as hire
from dual
connect by level <=12) b
where a.hire(+) = b.hire
order by 1;