[MySQL] WHERE 절과 ORDER BY 절

– WHERE

  • where 절 (조건절)
select * from employees;
  • employees 테이블엣 90 번 부서에 소속된 사원들을 출력하시오
select employee_id, last_name, salary, job_id, department_id
from employees
where department_id = 90;
  • employees 테이블에서 last_name 이 whalen 인 사원 출력하시오
select employee_id, last_name, salary, job_id, department_id
from employees
where last_name = 'whalen'
  • employees 테이블에서 hire_date(입사일) 가 1996년 02월 17일 인 사원 출력하시오
select employee_id, lats_name, salary, job_id, department_id, hire_date
from employees
where hire_date = '1996-02-17';
  • employees 테이블에서 급여를 3000 이하로 받는 사원만 출력하시오
select employee_id, last_name, salary, job_id, department_id, hire_date
from employees
where salary <= 3000;
  • 비교연산자 종류
  • 단일행 비교연산자 : =, >, >=, <, <=, <>, !=
  • 추가 비교연산자 : between, in, like, is null

  • [비교연산자 1] between A and B
  • A(하한값) 이상 B(상한값) 이하 의 값을 비교해주는 연산자로 범위 검색 시 활용 됨.
select employee_id, last_name, salary
from employees
where salary between 2500 and 3500;

-- (==)

select employee_id, last_name, salary
from employees
where salary >= 2500 and salary <= 3500;

select employee_id, last_name, hire_date, department_id
from employees
where hire_date between '1995-01-01' and '1996-12-31'

select employee_id, last_name, hire_date, department_id
from employees
where last_name between 'abel' and 'dehaan';
  • [비교연산자 2] in
  • (=, OR)의 성격을 내포한 비교연산자로 우변에 값 리스트가 올 수 있는 다중행 비교연산자
select employee_id, last_name, manager_id, department_id
from employees
where manager_id in (100, 101, 201);

select employee_id, last_name, salary
from employees
where last_name in ('bell', 'king');
  • [비교연산자 3] like
  • 패턴 일치 여부를 비교해 주는 연산자
  • like 비교연산자 패턴 작성 시 활용되는 기호
    • % : 0개 또는 여러 개의 문자가 올 수 있음.
    • _ : 반드시 1개의 문자가 와야함

  • a로 시작되는 문자열 : ‘a%’
  • a가 포함된 문자열 : ‘%a%’
  • a로 끝나는 문자열 : ‘%a’
  • 두번째 문자가 a 인 문자열 : ‘_a%’
  • 끝에서 세번째 문자가 a인 문자열 : ‘%a___’
select employee_id, last_name, salary
from employees
where last_name like 's%';

select employee_id, last_naem, salary, job_id
from employees
where job_id like '%it%';
  • (예제) employees 테이블에서 last_name 에 순서, 개수, ‘a’ 와 ‘e’ 가 모두 포함된
  • 사원들의 employee_id, last_neme, salary, job_id
select employee_id, last_name, salary, job_id
from employees
where last_name like '%a%'
and last_name like '%b%';

-- (==)

select employee_id, last_name, salary, job_id
from employees
where last_name like '%a%b%'
or last_name like '%b%a%';
  • [비교연산자 4] is null
  • null 값을 비교해 주는 연산자
select * from employees
where manager_id is null;

select employee_id, last_name, salary, commission_pct
from employees
where commission_pct is null;

select employee_id, last_name, department_id
from employees
where department_id is null;
  • 논리연산자 종류 : and, or, not

  • [논리연산자 1] and
  • where 절에 여러 조건문 작성 시 모두 만족해야 되는 경우 사용
  • or보다 and가 우선순위가 높음
select employee_id, last_nmae, job_id, salary
from employees
where salary >= 10000 and job_id like '%man%';
  • [논리연산자 2] or
  • where 절에 여러 조건문 작성 시 하나이상 만족하면 되는 경우 사용
select employee_id, last_name, job_id, salary
from employees
where salary >= 10000 or job_id like '%man%';
  • [논리연산자 3] not
  • 부정(반대)을 의미하는 논리연산자로 비교연산자와 조합으로 활용됨.
  • = <–> <>, !=
  • >, >= <–> <, <=
  • between A and B : A이상 B이하 <–> not between A and B: A 미만 B 초과
  • in : (=, or) <–> not in (!=, and)
  • like <–> not like
  • is null <–> is not null
  • 급여가 3000미만 15000 초과인 사원들 출력하시오
select employee_id, last_name, salary, department_id
from employees
where salary not between 3000 and 15000;
  • job_id 가 ac_account, it_prog, sa_rep 가 아닌 업무 담당자 출력하시오
select employee_id, last_name, job_id, salary
from employees
where job_id, not in ('ac_account', 'it_prog', 'sa_rep');
  • last_name에 ‘a’ 가 포함되어 있지 않은 사원 출력하시오
select employee_id, last_name, job_id, salary
from employees
where last_name not like '%a%';
  • 커미션을 받는 사원들 출력하시오
select employee_id, last_name, salary, commission_pct
from employees
where commission_pct is not null;

– order by 절 (정렬)

  • [문법] select 컬럼명1, 컬럼명2, 컬럼명3, …
  • from 테이블명
  • where 조건문
  • order by 컬럼명 [asc | desc];
select last_name, job_id, department_id, hire_date
from employees
where department_id < 100
order by hire_date desc;
  • order by 절에 column alias 사용 가능
select employee_id, last_name, salary * 12 as annsal
from employees
order by annsal desc;
  • order by 절에 위치표기법 사용 가능함
select employee_id, last_name, salary, department_id, job_id
from employees
order by 4;
  • 여러 컬럼을 기준으로 정렬하기
select employee_id, last_name, salary, department_id
from employees
order by department_id, salary desc;

2 thoughts on “[MySQL] WHERE 절과 ORDER BY 절”

Leave a Comment