11.
찾는 항목:관리자보다 먼저 입사한 사원의 이름 및 입사일, 관리자이름, 입사일
-> 후배가 먼저 승진한 사원의 이름, 입사일, 선배보다 먼저 승진한 사원 이름, 입사일
선배보다 먼저 승진한 사원의 이름, 입사일,
후배가 먼저 승진한 사원의 이름, 입사일 ---------->employees
-> 자체조인

select worker.last_name, worker.hire_date, manager.last_name, manager.hire_date
from employees worker, employees manager
where worker.hire_date < manager.hire_date and
worker.manager_id = manager.employee_id;

10.
찾는 항목:Davies보다 늦게 입사한 사원의 이름과 입사일
Davies의 입사일, Davies보다 늦게 입사한 사원의 이름, 입사일 : employees
-> 자체조인

select worker.last_name, worker.hire_date
from employees davies, employees worker
where davies.last_name = 'Davies' and worker.hire_date > davies.hire_date
order by worker.hire_date;

8.
찾는 항목:사원의 이름, 부서번호, 이 사원과 동일한 부서에서 근무하는 사원
사원의 이름, 부서 번호 : employees, 동일한 부서의 사원이름:employees
-> 자체조인
select woker.last_name, woker.department_id, colleage.last_name
from employees woker, employees colleage
where woker.department_id = colleage.department_id 
and woker.employee_id <> colleage.employee_id;

7.
6번 문제를 수정해서 관리자가 없는 사원도 출력해라.
정렬: 사원번호를 기준으로(오름차순:사번이 작은--> 사번이 큰)
-> 6번 조인(자체조인(일종의 등가조인)) + 해당하지 않은 것 --> 포괄조인
select w.last_name 사원이름, w.employee_id 사번, 
m.last_name 관리자이름, m.employee_id "관리자 사번"
from employees w, employees m
where w.manager_id = m.employee_id(+)
order by w.employee_id;

6.
찾는 항목:사원의 이름, 사원번호, 관리자의 이름, 관리자의 사번
사원의 이름,사원번호, 관리자의 이름, 관리자의 사번 : employees
-> 자체조인
select w.last_name 사원이름, w.employee_id 사번, 
m.last_name 관리자이름, m.employee_id "관리자 사번"
from employees w, employees m
where w.manager_id = m.employee_id;

5.
찾는 항목:사원의 이름, 업무코드, 부서번호, 부서이름
조건:Toronto에 근무하는
사원의 이름:employees, 업무코드:employees, 부서번호:employees,departments
부서이름:departments, 도시이름:locations
employees와 departments에 공통데이터:department_id(부서번호)
departments와 locations에 공통데이터:location_id(부서위치번호)
->등가조인
select e.last_name, e.job_id, d.department_id, d.department_name
from employees e, departments d, locations l
where e.department_id = d.department_id and d.location_id = l.location_id
and lower(l.city) = 'toronto';

4.
찾는 항목:사원의 이름, 부서이름
조건 : 이름에 a가 포함된
사원의 이름:employees, 부서이름:departments
employees테이블과 departments에 공통 데이터가 있나? 부서번호(department_id)
-> 등가조인
select last_name, department_name
from employees e, departments d
where e.department_id = d.department_id and last_name like '%a%';

3.
찾는 항목:사원의 이름, 부서이름, 부서위치, 도시이름
조건 : 커미션을 받는->커미션이 있는->
없는 걸 찾는 연산자: is null
있는 걸 찾는 연산자: is not null
사원의 이름 : employees, 부서이름,부서위치:departments, 도시이름:locations
employees와 departments에 공통의 데이터가 있는가?
- 있다. department_id(부서번호)
departments와 locations에 공통의 데이터가 있는가?
- 있다. location_id(위치번호)
있으면 : 등가조인, 없으면 : 나머지에서 찾는다.

select e.last_name, d.department_name, d.location_id, l.city
from employees e, departments d, locations l
where e.department_id = d.department_id and d.location_id = l.location_id
and e.commission_pct is not null;



SQL 15개 명령어
DML(삽입,삭제,변경,조회) + DDL(테이블과 관련) + DCL(권한과 관련)
Data Manipulation Language+Data Definition Language+Data Control Language

4장 조인(join) : 개념: 가상으로 분리된 테이블을 합친 후에 검색
1.등가조인 2.비등가조인 3.자체조인 4.포괄조인

Posted by webpage
,