[Oracle DB SQL] HR스키마 시나리오 예제

장경수·2023년 4월 13일
  1. employees에 관한 모든 정보 출력
select * from employees

  1. employees에 대한 frist name, last name, salary 출력
select first_name, last_name, salary from employees;

  1. 문자열끼리 더하기 ( '||' 기호 사용 )
    3-1. employees에서 first name과 last name의 문자열을 더하고 salary도 함께 출력하기
select first_name || ' ' || last_name, salary from employees;

3-2. 위의 코드에서 컬럼이름을 name으로 바꾸기

select first_name || ' ' || last_name as name, salary from employees;

문자열을 더할 떄 ' '을 쓰지 않으면 문자들이 붙어서 출력된다

select first_name || last_name, salary from employees;


집계 함수

  1. 직원들이 받는 월급 중 가장 많은 월급 구하기
select max(salary) from employees;

  1. 직원들 중 가장 최근에 입사한 날짜 구하기
select max(hire_date) from employees;

  1. 직원들이 받는 월급 중 가장 낮은 월급 구하기
select min(salary) from employees;

  1. 직원들 중 가장 먼저 입사한 날짜 구하기
select min(hire_date) from employees;

  1. 전체 직원 수 구하기
select count(*) from employees;

  1. 월간 급여로 지출되는 총 금액 구하기
select sum(salary) from employees;

  1. 직원들의 평균 월급 구하기
select avg(salary) from employees;

  1. 직원들의 평균 월급 구하기 (소수점 이하 자리는 버리기)
select floor(avg(salary)) from employees;

두개 이상의 컬럼을 조회하면서 집계함수를 사용하는 예시

  1. 직책별 최대 연봉 오름차순으로 구하기
select max(salary), job_id from employees
	group by job_id
    order by max(salary);

  1. IT부서의 first_name, salary, job_id 구하기
select first_name, salary, job_id from employees
	where job_id = 'IT_PROG';

  1. 각 직책별 인원 수 내림차순으로 정렬하기
select job_id, count(*) from employees
	group by job_id
    order by count(*) desc;

  1. 각 부서별 최소 월급 내림차순으로 정렬하기
select department_id, min(salary) from employees
	group by department_id
    order by min(salary) desc;


join에 대해 알아보기

  1. 모든 직원의 department_id, first_name, department_name, salary 구하기
E.department_id, E.first_name, D.department_name, E.salary 
	from employee E
	join department D
    	on D.department_id = E.department.id;

총 직원은 107명이지만 프리랜서는 부서가 없기 때문에 결과에서 빠지게 된다.

  1. employees와 departments에 대한 모든 정보 구하기
E.*, D.*
	from employees E
    join department D
    	on E.department_id = D.department_id;

급여가 10000이상이고 사원번호, 이름, 급여, 부서번호, 부서명을 포함해서 구하기 (부서번호를 오름차순으로 하고 그 부서에 맞게 급여 내림차순으로 출력)

select
	E.employee_id as 사원번호,
    E.first_name || ' ' || E.last_name as 이름,
    E.salary as 급여,
    D.department_id as 부서번호,
    D.department_name as 부서명
    	from employees E
        join departments
    		on E.department _id = D.department_id
        where
        	E.salary >= 10000
        order by 
        	E.department_id, E.salary desc;
 =               

profile
coding is my life

0개의 댓글