-- 문제1) EMPLOYEES 테이블에서 Kochhar의 급여보다 많은 사원의 정보를 사원번호,이름,담당업무,급여를 출력하라.
-- 문제2) EMPLOYEES 테이블에서 급여의 평균보다 적은 사원의 사원번호,이름,담당업무,급여,부서번호를 출력하여라.
-- 문제3) EMPLOYEES 테이블에서 100번 부서의 최소 급여보다 최소 급여가 많은 다른 모든 부서를 출력하라
-- 문제4) 업무별로 최소 급여를 받는 사원의 정보를 사원번호,이름,업무,부서번호를 출력하여라.
-- 단 업무별로 정렬하여라.
-- 서브쿼리 사용 이유 : 그룹으로 묶을경우 그룹컬럼과 그룹함수밖에 못쓰기 때문에.. 서브쿼리를 그룹으로 묶어주고
-- 메인쿼리에선 모든 컬럼을 불러줄 수 있다.
-- 문제5) EMPLOYEES 과 DEPARTMENTS 테이블에서 업무가 SA_MAN 사원의 정보를 이름,업무,부서명,근무지를 출력하라.
-- 문제6) EMPLOYEES 테이블에서 (가장 많은 사원)을 갖는 MANAGER의 사원번호를 출력하라.
-- 문제1) EMPLOYEES 테이블에서 Kochhar의 급여보다 많은 사원의 정보를 사원번호,이름,담당업무,급여를 출력하라.
select employee_id , first_name , job_id , salary
from employees e
where salary > (
select salary
from employees
where last_name = 'Kochhar'
);
-- 문제2) EMPLOYEES 테이블에서 급여의 평균보다 적은 사원의 사원번호,이름,담당업무,급여,부서번호를 출력하여라.
select employee_id , first_name , job_id , salary , department_id
from employees e
where salary <= (
select avg(salary)
from employees
);
-- 문제3) EMPLOYEES 테이블에서 100번 부서의 최소 급여보다 최소 급여가 많은 다른 모든 부서를 출력하라
select min(salary)
from employees
where department_id = 100;
select department_id , min(salary)
from employees
group by department_id
having min(salary) > (
select min(salary)
from employees
where department_id = 100
);
-- 문제4) 업무별로 최소 급여를 받는 사원의 정보를 사원번호,이름,업무,부서번호를 출력하여라.
-- 단 업무별로 정렬하여라.
-- 서브쿼리 사용 이유 : 그룹으로 묶을경우 그룹컬럼과 그룹함수밖에 못쓰기 때문에.. 서브쿼리를 그룹으로 묶어주고
-- 메인쿼리에선 모든 컬럼을 불러줄 수 있다.
select job_id , min(salary)
from employees
group by job_id;
select employee_id , first_name , job_id , department_id
from employees
where (job_id, salary) in (
select job_id , min(salary)
from employees
group by job_id
);
-- 문제5) EMPLOYEES 과 DEPARTMENTS 테이블에서 업무가 SA_MAN 사원의 정보를 이름,업무,부서명,근무지를 출력하라.
select e.first_name , j.job_title , e.department_id , l.city
from employees e
inner join departments d
on e.department_id = d.department_id
inner join jobs j
on e.job_id = j.job_id
inner join locations l
on d.location_id = l.location_id
where e.job_id = 'SA_MAN'
-- 문제6) EMPLOYEES 테이블에서 (가장 많은 사원)을 갖는 MANAGER의 사원번호를 출력하라.
select manager_id ,count(*) as emp_count
from employees e
where manager_id is not null
group by manager_id;
select max(emp_count)
from (
select manager_id ,count(*) as emp_count
from employees e
where manager_id is not null
group by manager_id
) as emp;
select count(manager_id)
from employees
group by manager_id
having count(manager_id) = (
select max(emp_count)
from (
select manager_id ,count(*) as emp_count
from employees e
where manager_id is not null
group by manager_id
) as emp
);
'백엔드 > DB' 카테고리의 다른 글
| [DB] AUTOCOMMIT (0) | 2024.04.26 |
|---|---|
| [MariaDB] MariaDB 학습 - 데이터 입력 (0) | 2023.07.10 |
| [MariaDB] MariaDB 학습 - 실습문제 - 조인 (0) | 2023.07.10 |
| [MariaDB] MariaDB 학습 - 서브쿼리 (0) | 2023.07.10 |
| [MariaDB] MariaDB 학습 - 조인 (0) | 2023.07.10 |