쿼리를 할 때도 연산자를 활용할 수 있는데
이 모든 연산자를 이용한 쿼리 예제를 살펴보고자 한다.
select [조건을 출력하는데 필요한 컬럼]
from [테이블 명]
where [컬럼에 대한 조건]
select ename,deptno,sal from emp where sal<=2000 and sal>=1000; //1
select ename”이름”,deptno”부서번호”,sal"급여" from emp where sal<=2000 and sal>=1000; //2
select ename, sal from emp where sal between 1000 and 2000; //3
SELECT ENAME"이름",JOB"직업" FROM EMP WHERE JOB='SALESMAN' OR JOB='CLERK';
select ename, sal from emp where sal < 1000 or sal > 2000; //1
// 2. where not 이용
select ename "이름"[-> 별칭을 준 것이다.], deptno "부서번호", sal "급여"
from emp where not(sal<=2000 and sal >= 1000);
select saname || '의 급여는 ' || sapay || '이다' "연결연산자" from sawon;
null 값은 비교연산을 할 수 없다.
select ename,comm from emp where comm = NULL;(에러)
위의 쿼리는 에러가 나는데 null 값은 비교연산을 할 수 없기 때문이다.
null값은 비교연산이 불가하다 때문에 다음과 같이 추출해야 한다.
null은 is
로 연산을 해줘야 한다.
select ename,comm from emp where comm is null; //1. (성공)
select ename, nvl(comm, 0) comm from emp; // 2. nvl(컬럼명, 대체값) : 널대체 함수
다음과 같이 is null을 사용하던가 nvl을 이용하여 null값을 모두 0으로 대체하여 추출한다.
nvl() 명령을 사용하여 null값을 0으로 대체할 수 있다.
select ename, nvl(comm[null값이 들어있는 컬럼명],0) comm from emp;
select ename, sal, nvl(comm, 0) comm, sal+nvl(comm, 0) "총급여" from emp;
null은 비교연산을 사용할 수 없으므로 is no null을 이용하여 null값을 제외 할 수 있다.
select * from emp where work is not null;
혹은 null값을 0으로 대체 한다.
select ename, nvl(comm, 0) comm from emp;
null은 데이터 중에서 가장 큰 값으로 인식된다.
오라클에서는 와일드 카드(%)를 사용한 like 연산을 통해 특정 문자를 추출 할 수 있다.
select ename from emp where ename like 'A%';
언더바(_)를 사용하여 검색할 문자열의 자릿수를 결정할 수 있다.
select ename, from emp where ename like '_L%';
select ename, from emp where ename like '%L%L%';
select ename, from emp where ename like 'A%' or ename like '_D%';
INSTR 함수는 대소문자를 구별하여 문자를 검색한다.
select * from emp where instr(ename, 'LL') > 0
INSTR 함수는 특정 문자열을 찾은 위치를 정수형으로 반환하는데,
문자열을 찾으면 1이상, 못 찾으면 0을 반환해 준다.