hr 테이블 사용
select employee_id, last_name, salary, manager_id
from employees
where manager_id in(100,101,201);
-- (==)
select employee_id, last_name, salary, manager_id
from employees
where manager_id = 100 or manager_id = 101 or manager_id = 201;
select employee_id, last_name, job_id
from employees
where job_id in('SA_REP','IT_PROG');
패턴일치 여부를 비교하는 연산자
select last_name
from employees
where last_name like '_o%'; -- 두번째 글자가 o
select employee_id, last_name, job_id
from employees
where job_id like '%rep'; -- rep로 끝나는
select employee_id, last_name, salary, hire_Date
from employees
where hire_date like '198%'; --1980년도 입사
값이 null인지를 비교해주는 연산자
select employee_id, last_name, manager_id
from employees
where manager_id is null; -- 우리 회사 사장 출력!!
-- 수당을 받지 않는 사원 출력
select employee_id, last_name, salary, commission_pct
from employees
where commission_pct is null;
and VS or
select employee_id, last_name, job_id, salary
from employees
where salary>=10000
and job_id like '%man%';
select employee_id, last_name, job_id, salary
from employees
where salary>=10000
or job_id like '%man%';
select employee_id, last_name, salary, job_id, department_id
from employees
where job_id= 'SA_REP'
or job_id = 'IT_PROG'
and salary>= 10000;
;
and 연산자 먼저 계산됨
select employee_id, last_name, salary, job_id, department_id
from employees
where (job_id= 'SA_REP'
or job_id = 'IT_PROG')
and salary>= 10000;
<--> | ||
---|---|---|
= | <--> | <>, != |
>, >= | <--> | <,<= |
between A and B | <--> | not between A and B A미만 B초과 |
in(=,or) | <--> | not in(<>,and) |
like | <--> | not like |
is null | <--> | is not null |
select employee_id, last_name, salary, department_id
from employees
where salary not between 5000 and 20000;
select employee_id, last_name, job_id
from employees
where job_id not in ('sa_rep', 'it_prog', 'st_man');
select employee_id, last_name, hire_date
from employees
where hire_date not like '19%';
select employee_id, last_name, manager_id
from employees
where manager_id is not null;
-- 사장빼고 전부 출력됨
예제) employees 테이블로부터 커미션을 받는 사원들의 employee_id, last_name, commission_pct, salary를 출력하는 구문을 작성하시오.
select employee_id,last_name, commission_pct, salary
from employees
where commission_pct is not null;
특정 컬럼을 기준으로 정렬된 결과를 출력해 줌.
select employee_id, last_name, salary, department_id
from employees
order by salary desc;
select employee_id, last_name, salary, department_id
from employees
order by salary;
** 표현식 및 컬럼alias 기준으로 정렬가능
select employee_id, last_name, 12*salary as ann_sal
from employees
order by 12*salary desc;
-- (==)
select employee_id, last_name, 12*salary as ann_sal
from employees
order by ann_sal desc;
** 위치 표기법 : 5번째 컬럼 기준으로 정렬 원할 경우 숫자만 적어도 정렬 가능
select employee_id, last_name, salary, job_id, department_id
from employees
where department_id <80
order by 5;
** 다중 컬럼 기준으로 정렬
select last_name, department_id, salary
from employees
order by department_id, salary desc;
- 첫번째 컬럼 기준으로 오름차순 정렬하고, 그안에서 두번째 컬럼 기준으로 내림차순 정렬
select last_name, department_id, salary
from employees
order by department_id desc, salary desc;
연습문제 1) employees 테이블로부터 2000년도에 입사한 모든 사원의 last_name과 hire_date를 출력하시오.
select last_name, hire_date
from employees
where hire_date between '2000-01-01' and '2000-12-31';
--(==)
select last_name, hire_date
from employees
where hire_date like '2000%';
연습문제2) employees 테이블로부터 커미션을 받지 않는 모든 사원의 last_name, salary, commission_pct를 출력하되 salary를 기준으로 내림차순 정렬하시오.
select last_name, salary, commission_pct
from employees
where commission_pct is null
order by salary desc;
현재 DB에 존재하는 테이블을 기존으로 ERD를 그려주는 기능
여러테이블의 데이터를 함께 출력하기 위한 문법
(예제1) employees 테이블과 departments 테이블로부터 employee_id, last_name, salary, department_id, department_name을 함께 출력하시오.
department_id를 기준으로 조인
select employee_id, last_name, salary, employees.department_id, department_name
from employees join employees.departments
on employees.department_id = departments.department_id;