타닥타닥 개발자의 일상

오라클 JOIN을 이용한 연습문제 풀이 1~11번 본문

코딩 기록/Oracle(SQL develpoer)

오라클 JOIN을 이용한 연습문제 풀이 1~11번

NomadHaven 2021. 12. 23. 22:03


JOIN
두개 이상의 테이블을 연결해서 데이터를 검색하는 방법
보통 두개 이상의 행들과 공통된 값 기본키, 외래키를 사용해서 Join.

기본 키(Primary Key): 테이블에서 중복이 되지 않는 키
외래 키(Foreign Key): 다른 테이블에서 PK, UK인 경우가 많다. 

 

JOIN 종류
    
    inner join         *****몹시 중요
    full outer join
    cross join
    outer join
        left            ***
        right          ***    
    self join          *****몹시 중요

 

 

문제1) 사원들의 이름, 부서번호, 부서명을 출력하라


select e.first_name,
        e.department_id, 
        d.department_name
from employees e, departments d
where e.department_id = d.department_id;

 

 

문제2) 30번 부서의 사원들의 이름,직업,부서명을 출력하라



select 
        d.department_id,
        e.first_name,
        e.job_id, 
        j.job_title,
        d.department_name
from employees e, departments d, jobs j
WHERE e.department_id = d.department_id
        and e.job_id=j.job_id
        and d.department_id=30;

 

문제3) 커미션을 받는 사원의 이름, 직업, 부서번호,부서명을 출력하라



select 
        e.first_name,
        e.job_id, 
        d.department_id,
        d.department_name,
        e.commission_pct*e.salary
from employees e, departments d
where e.department_id = d.department_id
and e.commission_pct is not null ;

 

문제4) 지역번호 2500 에서 근무하는 사원의 이름, 직업,부서번호,부서명을 출력하라



select  
        e.first_name,
        e.job_id, 
        d.department_id,
        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 d.location_id = 2500;

 

문제5) 이름에 A가 들어가는 사원들의 이름과 부서이름을 출력하라



select  
        e.first_name,
        e.job_id, 
        d.department_id,
        d.department_name,
        d.location_id
from employees e, departments d 
where e.department_id = d.department_id
and e.first_name like 'A%'; 

 

문제6) 사원이름과 그 사원의 관리자 이름을 출력하라


SELECT  emp.first_name as "사원명",
        mgr.first_name as "상사명"
FROM employees emp, employees mgr   -- a: 사원    b:상사
WHERE emp.manager_id = mgr.employee_id; 

 

문제7) 사원이름과 부서명과 월급을 출력하는데 월급이 3000 이상인 사원을 출력하라


select  
        e.first_name,
        d.department_name,
        e.salary
from employees e, departments d
where e.department_id = d.department_id 
and e.salary >=3000;

 

문제8) TJ 이란 사원보다 늦게 입사한 사원의 이름과 입사일을 출력하라


select  
        a.first_name,
        a.hire_date,
        b.first_name,
        b.hire_date
from employees a, employees b
where a.first_name='TJ'
and a.hire_date < b.hire_date
order by b.hire_date asc;

 

 

문제9) 급여가 3000에서 5000사이인 사원의 이름과 소속부서명 출력하라


select  
        e.first_name,
        d.department_name,
        e.salary
from employees e, departments d
where e.employee_id = d.department_id 
and e.salary BETWEEN 3000 and 5000;

 

 

문제10) ACCOUNTING 부서 소속 사원의 이름과 입사일 출력하라



select  
        e.first_name,
        e.hire_date,
        d.department_id,
        d.department_name
from  departments d, employees e
where d.department_id = e.department_id
and d.department_name ='Accounting';

 

문제11) 급여가 3000이하인 사원의 이름과 급여, 근무지를 출력하라.


select  
        e.first_name,
        d.location_id,
        e.salary,
        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.salary <=3000;

 

 

 

 

 




 







  










Comments