db

JIN·2023년 4월 13일
0
post-thumbnail


show tables;

-- ex1) employees, departments테이블로 test1 뷰로 만드시오
-- [조건1] employees, departments 테이블을 조인하여 쿼리를 만드시오
-- [조건2] 급여가  7000이상이면 '고급'  3000이상이면 '중급'  3000미만이면 '초급'으로 grade 컬럼으로 만드시오
--          (case when이용)
-- [조건3]   
--                last_name    department_name    salary       grade
--              -----------------------------------------------------
--                 King            Executive       24000        고급

select *
from employees;

create or replace view test1(last_name, department_name, salary, grade)
as
select e.last_name, d.department_name, e.salary,
case
	when e.salary >= 7000
	then '고급'
	when e.salary >= 3000
	then '중급'
	else '초급'
end grade
from employees e left join departments d
using(department_id);

-- 솔루션
-- select last_name, department_name, salary,
-- 	case when salary>=7000 then '고급'
-- 		when salary>=3000 then '중급'
--         else '초급'
-- 	end as 'grade'
-- from employees
-- left join departments using(department_id)
-- order by 4 desc;



     
select * from tab;
select * from test1;
drop table test1;
-- ----------------------------------------------------------------------------------------------
-- ex2) departments, locations2테이블로 test2부로 만드시오
--
--       departments(location_id) , locations2(loc_id)      
--      부서ID    부서명             도시
--      -----------------------------------------------------
--      60	      IT	             Southlake
--      50	     Shipping	         South San Francisco
--      10	     Administration	     Seattle

create or replace view test2(부서ID, 부서명, 도시)
as
select d.department_id 부서ID, d.department_name 부서명, l.city 도시
from departments d left join locations l
using(location_id);

drop table test2;

create table test2
as select department_id as "부서ID", department_name as "부서명", city as "도시"
    from departments
    left join locations2 on(location_id = loc_id);

select * from test2;

-- ----------------------------------------------------------------------------------------------
-- 내가 풀었음 정답까지 확인완료
-- ex3) employees테이블로 test3뷰로 만드시오
-- [조건1] self 조인을 이용하여 사원과 관리자를 연결하시오
-- [조건2] 모든 사원을 표시하시오
--
--        사원번호   사원이름      관리자
--        ----------------------------------
--        101          Kochhar       King   
create or replace view test3(사원번호,사원이름, 관리자)
as
select e.employee_id 사원번호, e.first_name 사원이름, m.last_name 관리자
from employees e left join employees m -- 그냥 조인했었음. 그냥 조인하면 안됨 1개 빠짐..널때문에..
on e.manager_id = m.employee_id;
-- 반드시 모든 직원 다 들어가야되니까 LEFT JOIN!!!

select * from test3;

-- ----------------------------------------------------------------------------------------------
-- ex4)employees테이블로 test4뷰로 만드시오
-- [조건1] 업무 id가 'SA_MAN'또는 ‘SA_REP'이면 'Sales Dept' 그 외 부서이면 'Another'로 표시
-- [조건2] case when을 이용하여 완성하시오
--        직무          분류
--       --------------------------
--       SA_MAN      Sales Dept
--       SA_REP      Sales Dept
--       IT_PROG      Another

-- create or replace view test4
-- as 
select job_id as "직무",
case job_id when 'SA_MAN' then 'Sales Dept'
					when 'SA_REP' then 'Sales Dept'
                    else 'Another'
                    end "분류"
from employees;
            

0개의 댓글