Oracle(3일차) - WHERE(비교연산자, 논리연산자, null검색)

Posted by seunggwon
2009. 6. 22. 13:00 Web Develop Note/Oracle

* 비교연산자
a=b : a는 b와 같다.
a<>b : a와 b는 같지 않다.
a>b : a는 b보다 크다(초과)
a>=b : a는 b보다 크거나 같다(이상)
a<b : a는 b보다 작다(미만)
a<=b : a는 b보다 작거나 같다(이하)

- 예제
1. 급여가 1500이상 2850이하 사원출력

select ename, sal
from emp
where sal>=1500 and sal<=2850
order by sal asc;
혹은
select ename, sal

from emp
where sal between 1500 and 2850
order by sal asc;

*논리연산자
or, and, not

*in(값, 값, ...)

not in(값, 값, ...)

예제
1. 사원명, 부서번호조회
부서번호가 10 또는 20 조회
 select ename, deptno
 from emp
 where deptno=10 or deptno=20 -> 같은 의미로  where deptno in(10, 20)
                                             -> 부정의의미 where deptno not in(10, 20)
 order by deptno asc;

2. 10과 30사이

select ename,deptno
from emp
where deptno not between 10 and 20
order by deptno asc;

*연결연산자 : ||
*함수명(~, ~, ~) parameter, argument, 인수, 인자

concat(표현식, 표현식) 인수가 2개밖에 들어갈수 있다.
현재날짜와 시간조회

*2009/06/22 09:41:30 이런형태로 출력하려면...
to_char(날짜, '날짜시간Format') 중요함...
to_char(sysdate, 'yyyy/mm/dd hh:mi:ss')

select sysdate, to_char(sysdate, 'bc yyyy/mon/dd day hh24:mi:ss am')
from dual;

* alter session set nls_date_format='날짜시간format'

*조건
select ~
from 테이블명
[where 조건]
[order by 정렬기준 정렬방법];

- 데이터타입 : 문자. 날짜 '~'

예제
- allen의 사원번호, 급여, 입사일조회
select empno, sal, hiredate
from emp
where ename='ALLEN';

*소문자를 대문자로 변환하는 함수
upper(~)

예제
select empno, sal, hiredate
from emp
where ename=upper('allen');

*lower(~)
대문자를 소문자로 출력시키는 함수

*앞글자만 대문자로...
initcap(~)

select empno, sal, lower(ename), initcap(ename), hiredate
from emp
where ename=upper('allen');

81/09/28에 입사한 사원명, 입사일, 급여, 커미션 조회
select ename, hiredate, sal, comm
from emp
where hiredate='81/09/28';

- 예제
1. xx번 부서는 ~~~ =>  출력 concat함수 + ~ 사용
select concat(deptno, concat('번 부서 : ', dname))
from dept;
OR
select deptno||concat('번 부서 : ', dname)

from dept;

2. 커미션이 확정된 사원의 사원번호, 사원명, 급여, 커미션 조회
select empno, ename, sal, comm
from emp
where comm is not null;

3. 2번 결과내 급여가 1500이상인 사원번호, 사원명, 급여, 커미션 조회
select empno, ename, sal, comm
from emp
where comm is not null and sal>=1500;

4. 3번의 결과를 급여 오름차순 조회
select empno, ename, sal, comm
from emp
where comm is not null and sal>=1500;
order by sal asc;

5. 10번 또는 20번 부서에 근무하는 사원들의
부서번호, 사원명, 급여, 커미션, bonus를 bonus 오름차순, 동일보너스 내 사원명 오름차순
bonus=기본커미션+300 커미션이 null이면 50을 기본 커미션으로 한다.
select deptno, ename, sal, nvl(comm, 50), nvl(comm, 50)+300 as bonus
from emp
where deptno in(10, 20)
order by bonus asc, ename asc;