본문 바로가기
study/Oracle

[Oracle] 4. 대소문자 변환(INITCAP, UPPER, LOWER), 부분문자열(SUBSTR), 문자의 위치값(INSTR), 문자 추가(LPAD, RPAD), 공백 제거(TRIM, LTRIM, RTRIM), 숫자(ROUND, TRUNC, MOD, CEIL, FLOOR, POWER, ABS, SIGN) , 날짜(MONTHS_BETWE..

by 금이패런츠 2022. 3. 4.
728x90
반응형
--1. 교수 테이블에서 급여가 450 이상인 경우는 5%를 인상하고 450 미만인 경우는 10%가 인상되도록
-- 교수번호, 교수이름, 현재급여, 인상예정급여 을 출력하기
-- 인상예정 급여의 내림차순으로 정렬하기
select profno, name,pay, pay*1.05 from professor
where pay >= 450
union
select profno, name,pay, pay*1.1 from professor
where pay < 450
order by 4 desc

-- 2.학생 테이블에 1학년 학생의 이름과 주민번호기준생일,  키와 몸무게를 출력하기. 
--   단 생일(월일만)이 빠른 순서대로 정렬
select name, substr(jumin,1,6) 생일 , height,weight from student
where grade = 1
order by substr(jumin,3,4)

--전체 학년
select name, substr(jumin,1,6) 생일 , height,weight from student
order by substr(jumin,3,4)

-- 3. 교수테이블(professor)급여가 300 이상이면서  보너스(bonus)을 받거나 
--    급여가 450 이상인 교수 이름, 급여, 보너스을 출력하여라.
select name,pay,bonus from professor
where ( pay >= 300 and bonus is not null ) or pay >= 450

select name,pay,bonus from professor
where  pay >= 300 and bonus is not null 
union
select name,pay,bonus from professor
where pay >= 450

-- 4. EMP 테이블에서 부서번호(deptno)로 정렬한 후 부서번호가 같을 경우 급여(sal)가 많은 순으로 정렬하여 
-- 사원번호, 성명, 업무, 부서번호, 급여를 출력하여라.
select empno, ename, job, deptno, sal from emp
order by deptno, sal desc

-- 5. 1학년 학생중 몸무게가 60kg보다 작은 학생과  3학년 학생중 키가 170보다 큰 학생의 
-- 학번, 이름, 키,몸무게를 출력하라.
select studno, name, height, weight from student
where ( grade = 1 and weight < 60 )
  or  ( grade = 3 and height > 170 )

select studno, name, height, weight from student
where ( grade = 1 and weight < 60 )
union
select studno, name, height, weight from student
 where  ( grade = 3 and height > 170 )

-- 6. 교수테이블에서 교수번호, 교수이름, 학과코드, 급여, 보너스, 연봉을 출력하기
--    연봉은 보너스가 있는 경우 pay * 12 + bonus로 계산하고,
--    보너스가 없는 경우는 pay * 12로 계산하여 출력하기
--    단 학과코드로 정렬하고, 연봉이 큰순으로 정렬하기
select profno, name,deptno, pay, nvl(bonus,0), pay * 12 + nvl(bonus,0) 연봉 
from professor
order by deptno, 연봉 desc

select profno, name,deptno, pay, bonus, pay * 12 + bonus 연봉 
from professor
where bonus is not null
union
select profno, name,deptno, pay, bonus, pay * 12
from professor
where bonus is  null
order by deptno, 연봉 desc

-- 7. 학생의 id의 길이가 7개이상 10개 이하인 학생의  학번, 이름, id, id의 글자수를 출력하기
select studno, name, id, length(id) from student
where length(id) between 7 and 10

-- 8. 학생의 생년월일을 '98년 03월 20일' 의 형식으로, 
--  이름과 생년월일을  조회한다.
--  월로 정렬하여 출력하기.
--   단 생년월일은 주민번호(jumin)을 기준으로 한다.
select name, 
   substr(jumin,1,2)||'년'||substr(jumin,3,2)||'월'||substr(jumin,5,2)||'일' 생년월일
from student
order by substr(jumin,3,2)

select name, to_date(substr(jumin,1,6),'yymmdd') from student
select name, to_char(to_date(substr(jumin,1,6),'rrmmdd'),'rrrr-mm-dd') from student

-- 9. EMP 테이블에서 이름이 scott인 사원의
--  사원번호, 성명, 담당업무(소문자로),  담당업무(대문자로), 첫 글자만 대문자로 변환하여 출력하기.
--  scott 사원은 대소문자 상관없이 조회가능하도록 한다
select empno, ename, lower(job),upper(job),initcap(job) from emp
where lower(ename) = lower('scott')

-- 10. EMP 테이블에서 이름의 첫 글자가 ‘K’보다 큰 사원의 사원번호,  이름, 업무를 출력하여라.
--     K는 대소문자 상관없이 조회가능하도록 한다

select empno, ename, job from emp
where lower(substr(ename,1,1)) > lower('k')

--11. EMP 테이블에서 이름이 6자리 이상인 사원의 이름과 업무를  출력하여라.
select ename, job from emp
where length(ename) >= 6
--12. EMP 테이블에서 이름에 L문자를 가진 사원의 사원이름, 직업, 사원이름에서 ‘L’자의 첫 위치 출력하기
select ename, job, instr(ename,'L') from emp
where ename like '%L%'

select ename, job, instr(ename,'L') from emp
where instr(ename,'L') > 0

-- 13. 교수테이블에서 이메일이 있는 교수의 이름, 직책,   email, emailid 를 출력하기
--    emailid는 @앞의 문자를 말한다.
--    email : number1@naver.com
--    emailid : number1
select name, position,email, substr(email,1,instr(email,'@')-1) emailid
from professor

-- 14. 사원테이블에서 사원이름에 *를 왼쪽에 채워 모두 동일한 15개 크기인 이름과 업무와 급여를 출력한다.
select lpad(ename,15,'*'),job,sal from emp
-- 15. 교수 테이블에서 입사일이 1-3월인 모든 교수의  급여를 15% 인상하여 정수로 출력하되 
-- 반올림된 값과   절삭된 값을  출력하기.

select name, pay, hiredate, round(pay*1.15),trunc(pay*1.15) from professor
where to_char(hiredate,'mm') between 1 and 3

select name, pay, hiredate, round(pay*1.15),trunc(pay*1.15) from professor
where substr(hiredate,4,2) between 01 and 03
-- hiredate : '91/01/30'

-- 16. 교수들의 근무 개월 수를 현재 일을 기준으로  계산하되,  
-- 근무 개월 순으로 정렬하여 출력하기. 
--  단, 개월 수의 소수점 이하 버린다
select name, hiredate, trunc(months_between(sysdate,hiredate)) 근무개월
from professor
order by 근무개월

-- 17. 학생의 사용자 아이디에서 문자열의 길이가 7이상인  학생의 이름과  사용자 아이디를 출력 하여라
select name, id from student
where length(id) > = 7
-- 18. 교수테이블에서 교수가 사용하는 email id와   등록된  id가 다른 교수의 이름과 id와 email을    출력하라.
--    emailid는 @앞의 문자를 말한다.
select name,id,email, substr(email,1,instr(email,'@')-1)
from professor
where id != substr(email,1,instr(email,'@')-1)

-- 19. 101번학과, 201번, 301번 학과 교수의 이름과  id를 출력하는데, id는 오른쪽을 $로 채운 후 
--    20자리로 출력하고  동일한 학과의 학생의   이름과 id를 출력하는데, 
--    학생의 id는 왼쪽#으로 채운 후 20자리로 출력하라.
select name, rpad(id,20,'$') id from professor
where deptno in (101,201,301)
union
select name, lpad(id,20,'#') id from student
where deptno1 in (101,201,301)

select name, rpad(id,20,'$') id from professor
where deptno in (101,201,301)
union all
select name, lpad(id,20,'#') id from student
where deptno1 in (101,201,301)

-- 20. 교수테이블에서 교수명과 입사일, 현재연봉 3%인상 후 연봉을 출력하기
--   단 연봉은 pay * 12로 하고, 인상후 연봉은 소숫점 이하 삭제함
select name, hiredate, pay*12 연봉, trunc((pay * 12) * 1.03) 인상연봉 from professor

-- 21. EMP 테이블에서 10번 부서원의 현재까지의 근무 월수를 계산하여  출력하여라.
--     근무월수는 반올림하여 정수로 출력하기
select empno, ename, hiredate,round(months_between(sysdate,hiredate)) from emp
where deptno = 10
728x90
반응형