DB/오라클

[ORACLE]조건문,정렬

에크키키 2022. 6. 7. 22:14
where절을 이용한 조건검색

비교연산자

 = 같다
 * !=  <>  ^= 같지않다
< >  <=  >=

select  * 
from employee
where ename='SCOTT';--where 조건문이 참

select  * 
from employee
where ename='scott';--결과없음(문자값은 대소문자 구분함)

select  * 
from employee
where lower(ename)='scott';

select  * 
from employee
where ename=upper('scott');

select  *
from employee
where hiredate < '1981/01/01' ;--'1981'년 1월 1일 이전에 입사'한 사원만 출력 19제거가능

select *
from employee
where dno !=10 ; --같지않다

 

논리연산자

AND OR NOT

우선순위 NOT -> AND -> OR 

 

BETWEEN 연산자

between A and b =and

not between a and b =or

--급여가 1100인 사원의 정보 출력
select *
from employee
where salary=1100;

--급여가 1000~1500사이인 사원의 정보 출력
select *
from employee
where 1000<=salary and salary<=1500;

select *
from employee
where salary between 1000 and 1500;


--급여가 1000미만이거나 1500초과인 사원의 정보 출력
select *
from employee
where 1000>salary or salary >1500;

select *
from employee
where salary not between 1000 and 1500; -- not between -> or

 

4.IN 연산자

--[문제]커미션이 300이거나 500이거나 1400인 사원 검색

select * from employee
where commission in (300,500,1400);

 

5.like 연산자와 와일드카드

% 문자가 없거나 하나 이상의 문자가 어떤 값이 와도 상관없음 

_ 하나의 문자가 어떤 값이 와도 상관없다.

 

select * from employee
where ename like 'F%';--이름이 F로 시작 (예)'F','Fs','FVB글'

select * from employee
where ename like '%M%';--이름에 M 포함

select * from employee
where ename like '%N'; -- 이름이 N으로 끝남

select * from employee
where ename like '_A%';-- _: 하나의 문자가 어떤값이 와도 상관없다. 두번째 문자가 A

select * from employee
where ename not like '%M%'--이름에 'A가 포함되지 않은'사원의 정보 출력

6.NULL 연산자

null ' = ' 연산자로 판단 불가 

is null

is not null

select * from employee
where commission is null;--commission을 받지않는 사원 정보 검색

 

ASC DESC 정렬

ASC 오름차순(생략가능)

DESC 내림차순

--급여가 가장 적은 순부터 출력(이때, 급여가 같으면 commission이 많은 순부터 출력)
select *from employee
order by salary,commission desc;


--급여가 적은 순 부터 출력(이때, 급여가 같으면 commission이 많은 순부터 출력,commission이 같으면 이름을 알파벳순으로 출력)
select *from employee
order by salary asc,commission desc,ename asc;
order by salary,commission desc,ename ;
order by 6 asc,7 desc,2 asc;--index번호:sql은 1부터 시작, java는 0부터시작
order by 6,7 desc,2 ;