728x90

WHERE 절

비교 연산자

  • =, !=, >, <, >=, <= 연산자 사용
select  first_name
        ,salary
        ,hire_date
from employees
where department_id = 10;

/*
부서번호가 10인 사원의 이름, 급여, 고용일을 구하라
*/
-- 연봉이 15000 이상인 사원들의 이름과 월급 출력
select first_name, salary
from employees
where salary >= 15000;

-- 07/01/01 일 이후에 입사한 사원들의 이름과 입사일 출력
select  first_name || '-' ||last_name 이름
        , hire_date 입사일
from employees
where hire_date > to_date('07/01/01', 'yy/mm/dd');
또는
where hire_date >= '07/01/01';

-- 이름이 Lex인 직원의 연봉 출력

select  first_name || '-' ||last_name 이름
        , salary 입사일
from employees
where first_name = 'Lex' or last_name = 'Lex';

오라클 날짜데이터 비교하는 법 알아보기

https://lookingfor.tistory.com/entry/oracle-todate

조건이 2개 이상일 때, 한 번에 조회하기

  • and : 모든 조건이 성립할 때.
  • or : 하나라도 조건이라도 성립할 때.
-- 연봉이 14000 이상 17000이하인 사원의 이름과 연봉을 구하시오
select  first_name
        ,salary
from employees
where salary >= 14000
and salary <= 17000;

--연봉이 14000 이하이거나 17000 이상인 사원의 이름과 연봉을 출력하세요
select  first_name
        ,salary
from employees
where salary <= 14000
or salary >= 17000;

/*
입사일이 04/01/01 에서 05/12/31 사이의 
사원의 이름과 입사일을 출력하세요
*/

select  first_name || ' ' || last_name
        ,hire_date
from employees
where hire_date >= '04/01/01'
and hire_date <= '05/12/31';

BETWEEN 연산자로 특정 구간 값 출력

  • 작은 값을 앞에, 큰 값을 뒤에 입력한다.
  • 두 값을 모두 포함하는 결과를 출력(경계값을 포함하지 않는 경우 AND를 사용해야 한다.)
  • 느린 연산자에 속함.
-- between 연산자로 특정구간 값 출력하기
select first_name
			 ,salary
from employees
where salary between 14000 and 17000;

IN 연산자로 여러 조건을 검사

select first_name
			 ,last_name
			 ,salary
from employees
where first_name = 'Leena',
first_name = 'Lex',
first_name = 'John';

동일

select first_name
			 ,last_name
			 ,salary
from employees
where first_name in ('Neena', 'Lex', 'John');
-- 연봉이 2100, 3100, 4100, 5100 인 사원의 이름과 연봉을 구하기

select first_name
        ,salary
from employees
where salary in (2100, 3100, 4100, 5100);

Like 연산자로 비슷한 것 찾기

  • % : 임의의 길이의 문자열(공백 문자 가능, 대소문자 구별)
  • _ : 한 글자 길이
  • 게시판 검색 기능 만들 때 사용할 수 있음.
-- Like 연산자로 비슷한 것 찾기
select  first_name
        ,last_name
        ,salary
from employees
where first_name like 'L%';
/*
where first_name like '%L';  // 마지막이 L로 끝나는 경우
where first_name like '%L%'; // 중간에 L이 들어가 있는 경우
*/

-- 이름에 am 을 포함한 사원의 이름과 연봉을 출력하세요
select first_name
        ,salary
from employees
where first_name like '%am';

-- 이름의 두번째 글자가 a 인 사원의 이름과 연봉을 출력하세요
select first_name
        ,salary
from employees
where first_name like '_a%';

-- 이름의 네번째 글자가 a 인 사원의 이름을 출력하세요
select first_name
        ,salary
from employees
where first_name like '___a%';

-- 이름이 4글자인 사원중 끝에서 
-- 두번째 글자가 a인 사원의 이름을 출력하세요
select first_name
        ,salary
from employees
where first_name like '__a_';

NULL

  • 아무런 값도 정해지지 않은 것을 의미한다.(0이 아니다.)
  • 어떠한 데이터 타입에도 사용 가능
  • not null 이나 primary key 속성에는 사용할 수 없다.
  • null을 포함한 산술식은 null

 

select first_name
				,salary
				,commission_pct
				,salary*commision_pct
from employees
where salary between 13000 and 15000;

is null / is not null

select first_name, salary, commission_pct
from employees
where commission_pct is null;
  • where commision_pct = null 이 아니다.
  • where commission_pct is null로 사용한다.
-- 커미션비율이 있는 사원의 이름과 연봉 커미션비율을 출력하세요
select first_name
        ,salary * commission_pct
from employees
where commission_pct is not null;

-- 담당매니저가 없고 커미션비율이 없는 직원의 이름을 출력하세요

select first_name
from employees
where manager_id is null 
and commission_pct is null;
반응형
코드플리