본문 바로가기

Data Engineering/Database

[SQL] 데이터 분석 기초 Where 절 : AND, OR, NOT, IN, LIKE, IS NULL, IS NOT NULL

WHERE 절

특정 조건을 불러오고 싶을 때 쓰는 쿼리입니다

  • 비교연산자 
  • between A and B
  • like   -    ➡️ 1개 문자
            %  ➡️ 0개 이상 문자
  • is null, is not null
  • in
  • and, or, not > and가 or 보다 우선순위가 더 높다

  1. select = 사원번호, 급여, 직업을 출력해주세요.
  2. from = 사원테이블에서
  3. where = 급여가 3000이여야 합니다.
select ename, sal, job
from emp
where sal=3000;
  • 두 값 범위를 이용해서 조회할때 between a and b : a,b 모두 포함 ( 문자나 날짜에만 사용 )
select ename, sal
from emp
where sal between 1000 and 3000;
  • LIKE 연산자 = 문자 또는 날짜에서 부분값을 이용해서 조회할때
  1. select * = 모두 출력해주세요
  2. from = 사원테이블에서
  3. where = ename (사원이름) like 'S%' (S가 0개 이상 들어간 사람)
select *
from emp
where ename like 'S%';
  • is not null / is null
  1. select = 사원이름, 급여, 수수료율 출력하세요
  2. from = 사원테이블에서
  3. where = (수수료율) is not null = null 이 아니다
  4. 반대로 is null 은 = null 값을 찾겠다 겠죠?!
select ename, sal, comm
from emp
where comm is not null;
  • and가 or보다 우선순위가 높기 때문에, and 먼저 실행되므로 it에서 7000 이하가 뽑힐 수 있다.
  • 그러므로 우선 뽑고 싶은 부분부터 괄호를 사용한다
select first_name, job_id, salary
from employees
where (job_id = 'IT_PROG' or job_id = 'SA_REP') and salary > 7000;
  • 2개 이상의 조건 중 하나라도 만족하는 값 in
  1. select = 사원이름, 직업, 급여
  2. from = 사원테이블에서
  3. where = (직업이) not in (매니저와 판매원이 아니다)
select ename, job, sal
from emp
where job not in ('MANAGER', 'SALESMAN');
반응형