1. 조건 연산자
조건 연산자는 특정한 조건에 맞는 데이터를 추출하기 위해 사용되는 연산자이다.
where 키워드를 통해 조건을 만들 수 있다.
-- 1. 부서가 d003이 아닌 매니저 사원들의 사원번호와 부서번호를 가져온다.
select emp_no, dept_no
from dept_manager
where dept_no <> 'd003';
-- 2. 급여액이 150000 이상인 사원의 사원번호, 급여액을 가져온다.
select emp_no, salary
from salaries
where salary >= 150000;
-- 3. 급여액이 40000 이하인 사원의 사원번호, 급여액을 가져온다.
select emp_no, salary
from salaries
where salary <= 40000;
-- 4. 1986년 이후에 입사한 사원들의 사원번호, 고용일, 이름, 성을 가저온다.
select emp_no, hire_date, first_name, last_name
from employees
where hire_date >= '1986-01-01';
-- 5. 1990년 이후부터 매니저로 근무하고 있는 사원들의 사원번호, 부서번호, 매니저 시작날짜를 가져온다.
select emp_no, dept_no, from_date
from dept_manager
where from_date >= '1990-01-01';
-- 6. 1990년 이전에 입사한 사원들의 사원번호, 입사일을 가져온다.
select emp_no, hire_date
from employees
where hire_date < '1990-01-01';
2. 논리 연산자
논리 연산자는 2개 이상의 조건을 가진 조건문을 작성할 떄 사용하는 연산자이다.
- and: 조건을 모두 만족해야함.
- or: 조건 중 하나만 만족해도 됨.
- not: 조건을 반대로 적용함.
- between: 컬럼의 범위를 조건으로 사용할 때 사용. 컬럼명 between **이상 and **이하
- in: 컬럼의 특정한 값들을 조건으로 지정할 때 사용. 컬럼명 in (값1, 값2)
-- 1. d001 부서의 매니저 중, 1990년 이후부터 매니저인 사원의 사원번호, 부서번호, 매니저 시작 날짜를 가져온다.
select emp_no, dept_no, from_date
from dept_manager
where dept_no = 'd001' and from_date >= '1990-01-01';
-- 2. 1990년 이후에 입사한 남자 사원의 사원번호, 성별, 입사일을 가져온다.
select emp_no, gender, hire_date
from employees
where hire_date >= '1990-01-01' and gender = 'M';
-- 3. 1990년 이후부터 60000 이상의 급여를 받는 사원의 사원번호, 급여, 급여 수령 날짜를 가져온다.
select emp_no, salary, from_date
from salaries
where from_date >= '1990-01-01' and salary >= 60000;
-- 4. d001부서와 d002부서 매니저의 사원번호, 부서번호를 가져온다.
select emp_no, dept_no
from dept_manager
where dept_no = 'd001' or dept_no = 'd002';
-- 5. 직함이 Staff인 사원과 Engineer인 사원의 사원번호, 직함을 가저온다.
select emp_no, title
from titles
where title = 'Staff' or title = 'Engineer';
-- 6. d003부서의 매니저가 아닌 매니저의 사원번호, 부서번호를 가져온다.
select emp_no, dept_no
from dept_manager
where not dept_no = 'd003';
-- 7. 급여가 60000 이상 70000 이하인 사원의 사원번호, 급여를 가져온다.
select emp_no, salary
from salaries
where salary between 60000 and 70000;
-- 8. 1989년 이후에 매니저를 시작한 d001부서와 d002부서 매니저의 사원번호, 부서번호를 가져온다.
select emp_no, dept_no, from_date
from dept_manager
where from_date >= '1989-01-01' and dept_no in ('d001', 'd002');
3. like 연산자
like는 다양한 문자열 조건식을 만들 때 사용한다.
_는 글자하나를 의미한다. %는 글자 수와 상관 없이 모든 글자를 의미한다.
-- 1. 이름이 Tommaso인 사원의 사원번호, 이름을 가져온다.
select emp_no, first_name
from employees
where first_name = 'Tommaso';
-- 2. 이름이 A로 시작하는 사원의 사원번호, 이름을 가져온다.
select emp_no, first_name
from employees
where first_name like 'A%';
-- 3. 이름의 마지막 글자가 s로 끝나는 사원의 사원번호, 이름을 가져온다.
select emp_no, first_name
from employees
where first_name like '%s';
-- 4. 이름의 두번째 글자가 i인 사원의 사원번호, 이름을 가져온다.
select emp_no, first_name
from employees
where first_name like '_i%';
-- 5. 이름이 다섯 글자인 사원들의 사원번호, 이름을 가져온다.
select emp_no, first_name
from employees
where first_name like '_____';
-- 6. 이름에 o가 포함되어 있는 사원의 사원번호, 이름을 가져온다.
select emp_no, first_name
from employees
where first_name like '%o%';
-- 7. 이름에 O가 포함되어 있는 사원의 사원번호, 이름을 가져온다. 단, 마지막 글자는 o가 아닌 사원.a
select emp_no, first_name
from employees
where first_name like '%O%' and not first_name like '%O';
'DataBase > MySQL' 카테고리의 다른 글
서브 쿼리문 (0) | 2021.08.03 |
---|---|
join문 (0) | 2021.08.02 |
숫자 함수, 문자열 함수, 날짜 함수, 그룹 함수, Group by & Having 절 (0) | 2021.08.01 |
산술 연산자, distinct 연산자, order by (0) | 2021.07.27 |
DataBase (0) | 2021.06.21 |