본문 바로가기

Database(DB)/MySQL(2024 version)

MySQL(2) - SELECT 조회, 중복 제거, 별칭, 정렬, 조건, 연산자

728x90
반응형

<목차>

1) DML - SELECT

   1-1) 조회

   1-2) 중복 제거

   1-3) 별칭

   1-4) 정렬

   1-5) 조건

   1-6) 연산자

2) 예제 풀이

 

 

 

 

1) DML - SELECT

1-1) 조회

 

 

1-2) 중복 제거

 

1-3) 별칭

 

 

1-4) 정렬

 

 

1-5) 조건

 

 

1-6) 연산자

 

 

 

 

 

 

 

 

 

 

2) 예제 풀이

-- select 절
-- from 절
-- where 절 : 조건절을 이용해 필터 적용하는데 사용되는 키워드

-- [SQL 문]
-- SELECT 절
-- FROM 절
-- WHERE 절
-- GROUP BY 절
-- HAVING 절
-- ORDER BY 절

-- 작성 순서
-- SELECT, FROM, WHERE, GROUP BY, HAVING, ORDER BY
-- 동작, 실행 순서
-- FROM, WHERE, GROUP BY, HAVING, SELECT, ORDER BY

 

 

-- 중복을 제거하는 distinct
select distinct * from employees;

 

 

 

select
	distinct department_id
from
	employees
;

 

 

 

-- 0. 연산자
select
	10 + 2,
	10 - 2,
	10 * 2,
	10 / 2,
	10 % 2
from dual;   -- dual : 가상의 연습용 테이블을 의미함

 

 

 

-- 1. 부등호, 등호(=, >=, <=, >, <, !=, <>)
select
	first_name, salary
from employees
where
	salary = 24000   -- Java식 표현은 사용 불가함(10 % 2 == 0)
;

 

 

 

select * from employees where salary >= 15000;

 

 

 

select * from employees where salary <= 5000;

 

 

 

select * from employees where salary > 15000;

 

 

 

select * from employees where salary < 3000;

 

 

 

select * from employees where salary != 24000;

 

 

 

select * from employees where salary <> 24000;

 

 

 

-- 2. 논리 연산자 AND, OR
select
	employee_id, department_id
from
	employees
where
	department_id = 20
	or
	department_id = 30
;

 

 

 

select
	employee_id, salary
from employees e
where
	salary >= 12000
	and
	salary <= 17000
;

 

 

 

-- 3. 사이의 값, 범위 BETWEEN A AND B
select
	employee_id, salary
from employees e
where
	salary between 12000 and 17000
;

 

 

 

-- 4. 부정 NOT
select
	employee_id, salary
from employees e
where
	salary
	not between 12000 and 17000
;

 

 

 

-- 5. 날짜 선택
select * from employees e
where
	hire_date > '2000-01-01'  -- 날짜값을 문자열로 변환한 뒤 연산자를 통해 비교가 가능함!
;

 

 

 

select * from employees e
where 
	hire_date between 
	'1999-01-01'
	and
	'2000-01-01'
;

 

 

 

-- 6. LIKE % _ (와일드 카드 문자) 문자열 패턴 매칭 조건

-- 등호 연산자 "="는 숫자, 날짜와 같은 데이터 유형을 비교할 때 사용
select * from employees
where first_name = 'Neena';

 

 

 

-- LIKE 연산자는 문자열 데이터를 검색하고 패턴 매칭할 때 사용
-- % : 0개 이상의 문자 패턴 지정
select * from employees e
where first_name like 'J%';

 

 

 

select * from employees
where first_name like '%am%';

 

 

 

-- _ : 정확히 한 문자와 일치하는 문자 패턴 지정
select * from employees e
where
	first_name like 'P____';

 

 

 

select * from employees
where
	first_name like '_oh_';

 

 

 

select * from employees
where 
	first_name like '_a%';

 

 

 

-- 7. IN(이 중에 있는 혹은 포함되는)
select
	employee_id, manager_id
from
	employees
where 
	manager_id in (100, 101)
;

 

 

 

select
	employee_id, manager_id
from
	employees
where 
	manager_id not in (100, 101)
;

 

 

 

select 
	employee_id, job_id
from
	employees e
where 
	job_id in
	('IT_PROG', 'HR_REP')
;

 

 

 

-- 8. IS NULL
select 
	employee_id, commission_pct
from employees e
where 
	commission_pct is null 
;

 

 

 

select 
	employee_id, commission_pct
from employees e
where 
	commission_pct is not null 
;

 

 

 

 

예제 문제

-- 1. 연봉이 12000 이상되는 직원들의 LAST_NAME 및 연봉을 조회한다.
select
	last_name, salary
from employees
where salary >= 12000
;

 

 

 

-- 2. 사원번호가 176인 사람의 LAST_NAME과 부서 번호를 조회한다.
select 
	last_name, department_id
from employees
where employee_id = 176
;

 

 

 

-- 3. 연봉이 5000에서 12000의 범위 의외인 사람들의 LAST_NAME 및 연봉을 조회한다.
select
	last_name, salary
from employees
where
	salary
	not between 5000 and 12000
;

/*
	다른 방법
	salary < 5000
	or
	salary > 12000
*/

 

 

 

-- 4. 1997/10/01일부터 1998/01/01 사이에 고용된 사원들의 LAST_NAME, 사번, 고용일자를 조회한다.
select 
	last_name, employee_id, hire_date
from employees
where 
	hire_date between '1997-10-01' and '1998-01-01'
;

 

 

 

-- 5. 20번 및 50번 부서에서 근무하는 모든 사원들의 LAST_NAME 및 부서 번호를 알파벳 순으로 조회한다.
select 
	last_name, department_id
from employees
where
	department_id in (20, 50)
order by last_name
;

 

 

 

-- 6. 20번 및 50번 부서에 근무하며, 연봉이 5000 ~ 12,000 사이인 사원들의 LAST_NAME 및 연봉을 조회한다.
select
	last_name, salary
from employees
where 
	department_id in (20, 50)
	and
	salary between 5000 and 12000
;

 

 

 

-- 7. 2000년도에 고용된 사람들의 LAST_NAME 및 고용일을 조회한다.
select 
	last_name, hire_date
from employees
where hire_date like '2000%';

 

 

 

-- 8. 매니저가 없는 사람들의 LAST_NAME 및 JOB_ID를 조회한다.
select
	last_name, job_id
from employees
where manager_id is null;

 

 

 

-- 9. 커미션을 버는 모든 사원들의 LAST_NAME, 연봉 및 커미션을 조회한다.
-- 연봉 역순, 커미션 역순차로 정렬한다.
select 
	last_name, salary, commission_pct
from employees
where commission_pct is not null
order by
	salary desc,
	commission_pct desc
;

 

 

 

-- 10. LAST_NAME 의 네번째 글자가 a 인 사원들의 LAST_NAME을 조회한다.
select 
	last_name
from employees
where last_name like '___a%';