Fundamental Notes/Data Base

[DB] Quiz1_1 해답

콩콩댕 2009. 4. 6. 15:21
반응형
quiz1_1 해답

1. 같은 업무(job)를 하는 사람의 수가 3명 넘는 경우,  업무와 인원수를  출력하라.
 
   SELECT JOB, COUNT(*)
          FROM EMP
          GROUP BY JOB
          HAVING COUNT(*) > 3;


2. 입사년도가  60년대인 사원들의 수와 평균 임금을 구하시오.

select count(*), avg(sal) from emp where substr(hiredate,1,2) between '60' and '69';
select count(*), avg(sal) from emp where substr(hiredate,1,1) ='6';
select count(*), avg(sal) from emp where hiredate between '60/01/01' and '69/12/31';
select count(*), avg(sal) from emp where hiredate like '6%';


3.각 부서이름은 10번 부서 ACCOUNTING , 20번 부서 RESEARCH,   30번 부서 SALES,  40번 부서 OPERATIONS이다.
  case 문을 써서 emp 들의 부서 번호에 따라  다음과 같이 출력되도록 하시오
(단 부서번호가 null 인 경우는 제외하도록 하시오.)
ACCOUNTING   RESEARCH   SALES    OPERATIONS  total
select count(case when deptno=10  then 1 end) as accounting,
       count(case when  deptno=20  then 1 end)  as research,
       count(case when  deptno=30  then 1 end) as sales,
       count(case when deptno=40  then 1 end)  as operations,
       count(*) as total
from emp
where deptno is not null;

select count(decode(deptno, 10,1)) accounting,
       count(decode(deptno, 20,1)) research,
       count(decode(deptno, 30,1)) sales,
       count(decode(deptno, 40,1)) operations,
       count(*) total
from emp
where deptno is not null;

====================================================

quiz1_2 해답


1. 같은 부서에서 같은업무(job)를 하는 사람의 수가 2명 넘는 경우,  부서번호와, 업무와 인원수를
          출력하라.
 
   SELECT DEPTNO,JOB, COUNT(*)
          FROM EMP
          GROUP BY DEPTNO, JOB
          HAVING COUNT(*) >2;

2. ename 에 대문자이든 소문자이든 'fr'이나 'f_' 들어간 사람의 이름을 대시오.

select ename
from emp
where upper(ename) like'%FR%' or upper(ename) like '%F\_%' escape '\';

select ename
from emp
where lower(ename) like'%fr%' or lower(ename) like '%f\_%' escape '\';

3. case 문과 decod문 을 써서 각 부서에서 81년도에서 85년도 사이에 입사한 사람들의 수를
다음과 같이 출력하도록 하시오.
81년입사    82년입사    83년입사    84년입사    85년입사    total


select count(case when to_char(hiredate,'yy')='81' then 1 end) as "81년입사",
       count(case when to_char(hiredate,'yy')='82' then 1 end) as "82년입사",
       count(case when to_char(hiredate,'yy')='83' then 1 end) as "83년입사",
       count(case when to_char(hiredate,'yy')='84' then 1 end) as "84년입사",
       count(case when to_char(hiredate,'yy')='85' then 1 end) as "85년입사",
       count(*) as total
from emp
where to_char(hiredate,'yy') between '81' and '85';

select count(decode(to_char(hiredate,'yy'),'81',1)) "81년입사",
       count(decode(to_char(hiredate,'yy'),'82',1)) "82년입사",
       count(decode(to_char(hiredate,'yy'),'83',1)) "83년입사",
       count(decode(to_char(hiredate,'yy'),'84',1)) "84년입사",
       count(decode(to_char(hiredate,'yy'),'85',1)) "85년입사",
       count(*) total
from emp
where to_char(hiredate,'yy') between '81' and '85';