๐Ÿ’ [ORACLE]_23.02.24

โ€์ „ํฌ์ฃผยท2023๋…„ 2์›” 24์ผ
0
  • CMD ์ƒ SQLPLUS ์ ‘์† ์ˆ˜ ์Šคํฌ๋ฆฝํŠธ ์—ฐ๋™ ๋ฐฉ๋ฒ•

    ๐Ÿ”ฝ ๋ช…๋ น์–ด ์ž…๋ ฅ

    sqlplus ora1/oracle_4U@localhost:1521/xepdb1
    conn ora1/oracle_4U@localhost:1521/xepdb1
    @C:\adsql_labs\ํŒŒ์ผ๋ช…

  • decode ํ•จ์ˆ˜

๐Ÿงก 3day ๋ณต์Šต ๋ฌธ์ œ


-- 3day ๋ณต์Šต ๋ฌธ์ œ SOL 

--1. ์‚ฌ์›์ด๋ฆ„, ์›”๊ธ‰, ์ปค๋ฏธ์…˜, ์ˆ˜๋ น์•ก(์›”๊ธ‰ + ์ปค๋ฏธ์…˜)์„ ์ถœ๋ ฅํ•˜์‹œ์˜ค.
--  (์ˆ˜๋ น์•ก ๊ณ„์‚ฐ์‹œ ์ปค๋ฏธ์…˜์ด ์ ์šฉ๋˜์ง€ ์•Š๋Š” ์‚ฌ์›์˜ ๊ฒฝ์šฐ๋Š” ์ปค๋ฏธ์…˜์„ 
--   ์ผ๊ด„์ ์œผ๋กœ 100์„ ์ ์šฉํ•ฉ๋‹ˆ๋‹ค.)

select ename, sal, comm, sal+nvl(comm,100)
from emp;


--2. ์ด๋ฆ„, ์ปค๋ฏธ์…˜์„ ์ถœ๋ ฅํ•˜๋Š”๋ฐ ์ปค๋ฏธ์…˜์ด null ์ธ ์‚ฌ์›๋“ค์€ no comm ์ด๋ž€ 
--   ๋ฌธ์ž์—ด๋กœ ์ถœ๋ ฅ๋˜๊ฒŒํ•˜์‹œ์˜ค.


select ename, nvl(to_char(comm),'no_comm') comm
from emp;
 

--3. ์ด๋ฆ„, ์ž…์‚ฌํ•œ ๋…„๋„(4์ž๋ฆฌ), ์›”๊ธ‰, ๋ณด๋„ˆ์Šค๋ฅผ ์ถœ๋ ฅํ•˜๋Š”๋ฐ
--   ๋ณด๋„ˆ์Šค๊ฐ€ ์ž…์‚ฌํ•œ ๋…„๋„๊ฐ€ 1981 ๋…„๋„๋ฉด 5000 ์„ ์ถœ๋ ฅํ•˜๊ณ 
--   ๋‚˜๋จธ์ง€ ์‚ฌ์›๋“ค์€ 0 ์„ ์ถœ๋ ฅํ•˜์‹œ์˜ค.


 select  ename, to_char(hiredate,'RRRR'),  sal,
   decode( to_char(hiredate,'RRRR'),'1981', 5000, 0) ๋ณด๋„ˆ์Šค
  from  emp;
--
 select  ename, to_char(hiredate,'RRRR'),  sal,
   CASE WHEN to_char(hiredate,'RRRR') = '1981' THEN 5000
   ELSE 0 
   END ๋ณด๋„ˆ์Šค
  from  emp;

--
select  ename, to_char(hiredate,'RRRR'),  sal,
   case to_char(hiredate,'RRRR')
      when '1981' then 5000
      else 0 end ๋ณด๋„ˆ์Šค
  from  emp;
  
--4. ์ด๋ฆ„, ์›”๊ธ‰๊ณผ ๋ณด๋„ˆ์Šค๋ฅผ ์ถœ๋ ฅํ•˜๋Š”๋ฐ 
--   ์›”๊ธ‰์ด 3000 ์ด์ƒ์ด๋ฉด ๋ณด๋„ˆ์Šค๋ฅผ 6000 ์„ ์ถœ๋ ฅํ•˜๊ณ  
--   ์›”๊ธ‰์ด 3000  ๋ณด๋‹ค ์ž‘์œผ๋ฉด ๋ณด๋„ˆ์Šค๋ฅผ 0 ์„ ์ถœ๋ ฅํ•˜์‹œ์˜ค.


select  ename, sal, 
            case  when sal >= 3000  then  6000
                  else  0  end  as  bonus
from  emp;


--5. ์ด๋ฆ„, ์ปค๋ฏธ์…˜, ๋ณด๋„ˆ์Šค๋ฅผ ์ถœ๋ ฅํ•˜๋Š”๋ฐ ๋ณด๋„ˆ์Šค๊ฐ€ 
--   ์ปค๋ฏธ์…˜์ด null ์ด๋ฉด 7000 ์„ ์ถœ๋ ฅํ•˜๊ณ  
--   ์ปค๋ฏธ์…˜์ด null ์ด ์•„๋‹ˆ๋ฉด  0 ์„ ์ถœ๋ ฅํ•˜์‹œ์˜ค.


 select  ename, comm,
        case  when comm is null  then  7000
              else   0  end  as bonus
   from  emp;



--6. ์ด๋ฆ„, ์›”๊ธ‰ , ์ง์—…, ๋ณด๋„ˆ์Šค๋ฅผ ์ถœ๋ ฅํ•˜๋Š”๋ฐ
--         ์ง์—…์ด SALESMAN ์ด๊ณ  ์›”๊ธ‰์ด 1000 ์ด์ƒ์ด๋ฉด ๋ณด๋„ˆ์Šค๋ฅผ 9000 ์„ ์ถœ๋ ฅํ•˜๊ณ  
--         ์ง์—…์ด ANALYST ์ด๊ณ  ์›”๊ธ‰์ด 2500 ์ด์ƒ์ด๋ฉด  ๋ณด๋„ˆ์Šค๋ฅผ 8000 ์„ ์ถœ๋ ฅํ•˜๊ณ  
--         ๋‚˜๋จธ์ง€ ์‚ฌ์›๋“ค์€ 0 ์„ ์ถœ๋ ฅํ•˜์‹œ์˜ค.


 select  ename, sal, job, 
  case  when  ( job='SALESMAN' and sal >= 1000)  then 9000
        when  ( job='ANALYST'  and sal >= 2500)  then 8000
        else  0  end  as bonus
from  emp;


--7. ๋ถ€์„œ๋ฒˆํ˜ธ, ๋ถ€์„œ๋ฒˆํ˜ธ๋ณ„ ์ตœ๋Œ€์›”๊ธ‰์„ ์ถœ๋ ฅํ•˜๋Š”๋ฐ
--        ๋ถ€์„œ๋ฒˆํ˜ธ๋ณ„ ์ตœ๋Œ€์›”๊ธ‰์ด ๋†’์€๊ฒƒ ๋ถ€ํ„ฐ ์ถœ๋ ฅํ•˜์‹œ์˜ค.



select  deptno, max(sal)
 from   emp
 group  by deptno
 order  by  max(sal)  desc;



--8. ์œ„์˜ ๊ฒฐ๊ณผ์—์„œ ๋ถ€์„œ๋ฒˆํ˜ธ 20๋ฒˆ์€ ์ œ์™ธํ•˜๊ณ  ์ถœ๋ ฅํ•˜์‹œ์˜ค.

-- ๋ถ€์„œ๋ฒˆํ˜ธ 20์ด ์•„๋‹Œ ๊ฒƒ๋“ค์„ ์„ ๋ณ„ํ•ด ๊ทธ๋ฃนํ•‘ 

select  deptno, max(sal)
 from   emp
where deptno !=20
 group  by deptno
 order  by  max(sal)  desc;
 
-- ์•„๋ž˜ HAVING ์ ˆ์€ ์‹คํ–‰ ์ˆœ์„œ๊ฐ€ WHERE๊ณผ ์ƒ์ดํ•œ ์ ์— ์œ ์˜ 
-- ๊ทธ๋ฃนํ•‘ํ•œ ํ›„ ๋ถ€์„œ๋ฒˆํ˜ธ 20์ด ์•„๋‹Œ ๊ฒƒ๋“ค์„ ์ œ์™ธ 

select  deptno, max(sal)
 from   emp
--where deptno !=20
 group  by deptno
 HAVING DEPTNO !=20 
 order  by  max(sal)  desc;

--9. ์ง์—…, ์ง์—…๋ณ„ ํ† ํƒˆ์›”๊ธ‰์„ ์ถœ๋ ฅํ•˜๋Š”๋ฐ
--         ์ง์—…์ด SALESMAN ์ธ ์‚ฌ์›๋“ค์„ ์ œ์™ธํ•˜๊ณ  ์ถœ๋ ฅํ•˜๊ณ 
--         ์ง์—…๋ณ„ ํ† ํƒˆ์›”๊ธ‰์ด ๋†’์€๊ฒƒ๋ถ€ํ„ฐ ์ถœ๋ ฅํ•˜๋Š”๋ฐ 
--         ์ง์—…๋ณ„ ํ† ํƒˆ์›”๊ธ‰์„ ์ถœ๋ ฅํ• ๋•Œ์— ์ฒœ๋‹จ์œ„๋ฅผ ๋ถ€์—ฌํ•ด์„œ
--         ์ถœ๋ ฅํ•˜์‹œ์˜ค.


select job, to_char(sum(sal),'99,999') "SUM(SAL)"
from emp
where job != 'SALESMAN'
group by job
order by sum(sal) desc;


--10. ๋ถ€์„œ๋ฒˆํ˜ธ, ๋ถ€์„œ๋ฒˆํ˜ธ๋ณ„ ํ† ํƒˆ์›”๊ธ‰์„ ์ถœ๋ ฅํ•˜์‹œ์˜ค.
--
select deptno, sum(sal)
 from  emp
 group  by deptno;



--11. ์œ„์˜ ๊ฒฐ๊ณผ๋ฅผ ๊ฐ€๋กœ๋กœ ์ถœ๋ ฅํ•˜์‹œ์˜ค.
--
--
-- ์ด ๋ฌธ์ œ๋Š” emp ํ…Œ์ด๋ธ”์— ๋ถ€์„œ๋ฒˆํ˜ธ๊ฐ€ 10๋ฒˆ 20๋ฒˆ 30๋ฒˆ์ด
--  ์žˆ๋‹ค๋Š”๊ฒƒ์„ ์•ˆ๋‹ค๋ผ๋Š” ๊ฐ€์ •ํ•˜์— SQL ์„ ์ž‘์„ฑํ•˜๋ฉด ๋จ

select sum(decode(deptno,10,sal,0)) "10",
       sum(decode(deptno,20,sal,0)) "20",
       sum(decode(deptno,30,sal,0)) "30"
from emp;


--12. ์ง์—…, ๋ถ€์„œ๋ฒˆํ˜ธ, ์ง์—…๋ณ„ ๋ถ€์„œ๋ฒˆํ˜ธ๋ณ„ ํ† ํƒˆ์›”๊ธ‰์„ ์ถœ๋ ฅํ•˜์‹œ์˜ค.

select job,
sum(decode(deptno,10,sal)) as "10",
sum(decode(deptno,20,sal)) as "20",
sum(decode(deptno,30,sal)) as "30"
from emp
group by job;

๐Ÿงก SQL_PRAC ์‹ค์Šต


--1) 
SELECT  E.EMPLOYEE_ID, E.DEPARTMENT_ID, D.DEPARTMENT_ID, D.LOCATION_ID 
FROM EMPLOYEES E JOIN DEPARTMENTS D 
ON E.DEPARTMENT_ID = D.DEPARTMENT_ID;

--2)
SELECT D.DEPARTMENT_ID, D.DEPARTMENT_NAME, L.CITY
FROM DEPARTMENTS D JOIN LOCATIONS L 
ON D.LOCATION_ID = L.LOCATION_ID
WHERE DEPARTMENT_ID IN (20, 50);

SELECT *
FROM LOCATIONS;

--3) 
SELECT E.LAST_NAME, D.DEPARTMENT_NAME, L.CITY 
FROM EMPLOYEES E JOIN DEPARTMENTS D 
ON E.DEPARTMENT_ID = D.DEPARTMENT_ID
JOIN LOCATIONS L
ON D.LOCATION_ID = L.LOCATION_ID;

--4) 
SELECT E1.LAST_NAME E_NAME, E2.LAST_NAME M_NAME
FROM EMPLOYEES E1 
JOIN EMPLOYEES E2
ON E1.EMPLOYEE_ID = E2.MANAGER_ID;

--5) 
SELECT E.LAST_NAME, E.DEPARTMENT_ID, D.DEPARTMENT_NAME
FROM EMPLOYEES E
LEFT OUTER JOIN DEPARTMENTS D
ON E.DEPARTMENT_ID = D.DEPARTMENT_ID;

--6) 
SELECT D.DEPARTMENT_ID, E.LAST_NAME, E.SALARY
FROM EMPLOYEES E 
JOIN DEPARTMENTS D 
ON E.DEPARTMENT_ID = D.DEPARTMENT_ID;

--7) 
SELECT D.DEPARTMENT_ID, E.LAST_NAME, E.SALARY
FROM EMPLOYEES E 
RIGHT OUTER JOIN DEPARTMENTS D 
ON E.DEPARTMENT_ID = D.DEPARTMENT_ID
AND SALARY >= 10000
ORDER BY DEPARTMENT_ID;
-- WHERE, AND ์ฐจ์ด -> ๋ช…๋ น์–ด ์ฒ˜๋ฆฌ ์ˆœ์„œ๊ฐ€ OUTER ์กฐ์ธ ๋จผ์ €, ํ•„ํ„ฐ๋ง ์ง„ํ–‰ (๋ถ€์„œ์— ๋Œ€ํ•œ ๋‚ด์šฉ์ด ํ•„ํ„ฐ๋ง) 
-- AND ์—ฐ์‚ฐ ์‹œ ์กฐ๊ฑด ์‹คํ–‰ ํ›„ OUTER JOIN ์‹คํ–‰

--8) ๋น„์กฐ์ธ ์กฐ๊ฑด์œผ๋กœ ๋‹ค์‹œ ํ’€๊ธฐ 
SELECT E.LAST_NAME, E.JOB_ID, D.DEPARTMENT_ID, D.DEPARTMENT_NAME 
FROM EMPLOYEES E 
JOIN DEPARTMENTS D 
ON E.DEPARTMENT_ID = D.DEPARTMENT_ID
JOIN LOCATIONS L 
ON D.LOCATION_ID = L.LOCATION_ID 
WHERE L.CITY = 'Toronto'; 

--9) 
SELECT E1.LAST_NAME "EMPLOYEE", E1.EMPLOYEE_ID "EMP#", E2.LAST_NAME, E2.MANAGER_ID "Mgr#"
FROM EMPLOYEES E1 
LEFT OUTER JOIN EMPLOYEES E2 
ON E1.MANAGER_ID = E2.EMPLOYEE_ID;


--10) ๋น„์กฐ์ธ ์กฐ๊ฑด
SELECT E.LAST_NAME, E.JOB_ID, D.DEPARTMENT_NAME, E.SALARY, J.GRADE_LEVEL
FROM EMPLOYEES E , DEPARTMENTS D , JOB_GRADES J 
WHERE E.DEPARTMENT_ID = D.DEPARTMENT_ID
AND  E.SALARY
BETWEEN J.LOWEST_SAL AND J.HIGHEST_SAL; 

-- ๋น„์กฐ์ธ ์กฐ๊ฑด์€ ๋ฐ˜๋“œ์‹œ AND๋กœ ์—ฐ๊ฒฐํ•ด์•ผํ•จ (WHERE์€ ์•ˆ๋จ) 

--11) ?? 
@C:\ADSQL_labs\emp_kind

select e.empno, e.ename, e.emp_kind, k1.office_sal, k2.sal
from emp_kind e, emp_kind1 k1, emp_kind2 k2
where e.empno=k1.empno(+)
and e.empno=k2.empno(+)
order by e.emp_kind;


--12) ๋ชจ๋ฅด๊ฒ ์Œ -> ์…€ํ”„ ์กฐ์ธ์œผ๋กœ ํ•ด๊ฒฐ (+. ๋น„๋™๋“ฑ ์กฐ์ธ ํ™œ์šฉ) 
SELECT E.EMPNO, E.ENAME, E.SAL, J.SAL "JONES SALARY"
FROM EMP E
JOIN EMP J 
ON E.EMPNO = J.EMPNO  
WHERE E.SAL > J.SAL ;
 
select e.empno, e.ename, e.sal, j.sal as "Jones's Salary"
from emp e, emp j
where upper(j.ename)='JONES'
and j.sal < e.sal;




--13) ๋ชจ๋ฅด๊ฒ ์Œ 
SELECT DISTINCT(D.DEPTNO) DEPTNO, D.DNAME, D.LOC, 
    CASE WHEN ENAME IS NOT NULL THEN 'YES' ELSE 'NO' END EMP 
FROM DEPT D
LEFT OUTER JOIN EMP E 
ON D.DEPTNO = E.DEPTNO;

--SOL 
select d.deptno, d.dname, d.loc, decode(count(e.deptno),0,'NO','YES') as emp
from dept d, emp e
where d.deptno=e.deptno(+)
group by d.deptno, d.dname, d.loc
order by d.deptno

--14) ๋ชจ๋ฅด๊ฒ ์Œ 
SELECT E.FIRST_NAME, E.LAST_NAME, E.SALARY, E.JOB_ID, C.COUNTRY_NAME  
FROM EMPLOYEES E , countries C
WHERE COUNTRY_NAME = 'Canada'
AND E.SALARY BETWEEN >= 6000 AND <= 13000; 

--SOL ์ง•๊ฒ€๋‹ค๋ฆฌ ํ…Œ์ด๋ธ” (4๊ฐœ ํ…Œ์ด๋ธ” ์กฐ์ธ ํ•„) E, Dํ…Œ์ด๋ธ” ๊ฐ„ ์—ฐ๊ฒฐ๊ณ ๋ฆฌ๋กœ์„œ L,C ์กฐ์ธ ์ˆ˜ํ–‰ (ERD ์ฐธ๊ณ ) 
select e.first_name, e.last_name, e.salary, e.job_id, c.country_name
from employees e,
	departments d
,  locations l
, 	countries c
where e.department_id=d.department_id
and d.location_id=l.location_id
and l.country_id=c.country_id
and initcap(c.country_name)='Canada';

--15) ๋ชจ๋ฅด๊ฒ ์Œ 
-- GROUP BY ํ–‰์—๋Š” SELECT ์ ˆ์— ์–ธ๊ธ‰๋œ ๋ชจ๋“  ์†์„ฑ (P.PROD_ID, P.PROD_NAME) ๊ธฐ์ž… ํ•„ 
SELECT  P.PROD_ID, P.PROD_NAME, SUM(S.QUANTITY_SOLD) SOLD_SUM 
FROM PRODS P
LEFT OUTER JOIN SALES S
ON P.PROD_ID = S.PROD_ID
GROUP BY P.PROD_ID, P.PROD_NAME;

--SOL 

select p.prod_id, p.prod_name, sum(s.quantity_sold) as sold_sum
from prods p, sales s
where p.prod_id=s.prod_id(+)
group by p.prod_id, p.prod_name;

๐Ÿงก SQL_PRAC ํ•ด๋‹ต

๐Ÿ”ฝ 3DAY_2

--1
SELECT 	e.employee_id, e.last_name, e.department_id,
        d.department_id, d.location_id
FROM    employees e , departments d
WHERE 	e.department_id = d.department_id;

SELECT 	e.employee_id, e.last_name, e.department_id,
        d.department_id, d.location_id
FROM 	employees e JOIN departments d
ON 	    e.department_id = d.department_id;

--2
SELECT 	d.department_id, d.department_name, l.city
FROM 	departments d, locations l
WHERE 	d.location_id = l.location_id
AND 	d.department_id IN (20, 50);

SELECT 	d.department_id, d.department_name, l.city
FROM 	departments d JOIN locations l
ON 	    d.location_id = l.location_id
AND 	d.department_id IN (20, 50);

--3
SELECT 	e.last_name, d.department_name, l.city
FROM 	employees e , departments d, locations l
WHERE 	e.department_id = d.department_id
AND 	d.location_id = l.location_id;

SELECT 	e.last_name, d.department_name, l.city
FROM 	employees e JOIN departments d
ON 	    e.department_id = d.department_id
JOIN    locations l
ON      d.location_id = l.location_id;

--4
SELECT  e.last_name e_name, m.last_name m_name
FROM    employees e , employees m
WHERE   e.manager_id=m.employee_id;

SELECT  e.last_name e_name, m.last_name m_name
FROM    employees e JOIN employees m
ON      e.manager_id=m.employee_id;

--5
SELECT 	e.last_name, e.department_id, d.department_name
FROM 		employees e, departments d
WHERE 	e.department_id = d.department_id(+) ;

SELECT 	e.last_name, e.department_id, d.department_name
FROM 	employees e LEFT OUTER JOIN departments d
ON      e.department_id = d.department_id ;


--6
SELECT 	d.department_id, e.last_name, e.salary
FROM 	employees e, departments d
WHERE 	e.department_id(+) = d.department_id;


SELECT 	d.department_id, e.last_name, e.salary
FROM 	employees e RIGHT OUTER JOIN departments d
ON  	e.department_id = d.department_id;


--7
SELECT 	d.department_id, e.last_name, e.salary
FROM 		employees e, departments d
WHERE 	e.department_id(+) = d.department_id
and   e.salary(+) > 10000;

SELECT 	d.department_id, e.last_name, e.salary
FROM 	employees e RIGHT OUTER JOIN departments d
ON 	    e.department_id = d.department_id
and     e.salary > 10000;

--8
SELECT   e.last_name, e.job_id, e.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';

SELECT   e.last_name, e.job_id, e.department_id, d.department_name
FROM     employees e JOIN departments d
ON       e.department_id=d.department_id
JOIN     locations l
ON       d.location_id=l.location_id
AND      lower(l.city)='toronto';

--9
SELECT   e.last_name "Employee", e.employee_id "EMP#",
         m.last_name "Manager", m.employee_id "Mgr#"
FROM     employees e, employees m
WHERE    e.manager_id=m.employee_id(+);

SELECT   e.last_name "Employee", e.employee_id "EMP#",
         m.last_name "Manager", m.employee_id "Mgr#"
FROM     employees e LEFT OUTER JOIN employees m
ON e.manager_id=m.employee_id;


--10
SELECT e.LAST_NAME, e.job_id, d.department_name,
       e.salary, j.grade_level
FROM   employees e, departments d, job_grades j
WHERE  e.department_id=d.department_id
AND    e.salary between j.lowest_sal and j.highest_sal;

SELECT e.LAST_NAME, e.job_id, d.department_name,
       e.salary, j.grade_level
FROM   employees e JOIN departments d
ON      e.department_id=d.department_id
JOIN   job_grades j
ON      e.salary between j.lowest_sal and j.highest_sal;

11
select e.empno, e.ename, e.emp_kind, k1.office_sal, k2.sal
from emp_kind e, emp_kind1 k1, emp_kind2 k2
where e.empno=k1.empno(+)
and e.empno=k2.empno(+)
order by e.emp_kind;

select e.empno, e.ename, e.emp_kind, k1.office_sal, k2.sal
from emp_kind e left join emp_kind1 k1
on e.empno=k1.empno
left join emp_kind2 k2
on e.empno=k2.empno
order by e.emp_kind;


12
select e.empno, e.ename, e.sal, j.sal as "Jones's Salary"
from emp e, emp j
where upper(j.ename)='JONES'
and j.sal < e.sal;

select e.empno, e.ename, e.sal, j.sal as "Jones's Salary"
from emp e join emp j
on upper(j.ename)='JONES'
and j.sal < e.sal;

13
select d.deptno, d.dname, d.loc, decode(count(e.deptno),0,'NO','YES') as emp
from dept d, emp e
where d.deptno=e.deptno(+)
group by d.deptno, d.dname, d.loc
order by d.deptno
/

select d.deptno, d.dname, d.loc, decode(count(e.deptno),0,'NO','YES') as emp
from dept d left outer join emp e
on d.deptno=e.deptno
group by d.deptno, d.dname, d.loc
order by d.deptno;


14
select e.first_name, e.last_name, e.salary, e.job_id, c.country_name
from employees e,
	departments d
,  locations l
, 	countries c
where e.department_id=d.department_id
and d.location_id=l.location_id
and l.country_id=c.country_id
and initcap(c.country_name)='Canada';

select e.first_name, e.last_name, e.salary, e.job_id, c.country_name
from employees e
join departments d
on e.department_id=d.department_id
join locations l
on d.location_id=l.location_id
join countries c
on l.country_id=c.country_id
where initcap(c.country_name)='Canada';

15
select p.prod_id, p.prod_name, sum(s.quantity_sold) as sold_sum
from prods p, sales s
where p.prod_id=s.prod_id(+)
group by p.prod_id, p.prod_name;

select p.prod_id, p.prod_name, sum(s.quantity_sold) as sold_sum
from prods p left outer join sales s
on p.prod_id=s.prod_id
group by p.prod_id, p.prod_name;

๐Ÿงก 8์žฅ ์„œ๋ธŒ์ฟผ๋ฆฌ.

  • MAIN ๋ช…๋ น์–ด ๋‚ด ๊ด„ํ˜ธ๋กœ ํ‘œ๊ธฐํ•˜๋Š” ์„œ๋ธŒ ์ฟผ๋ฆฌ
  • Subquery๋ฅผ ํฌํ•จํ•˜๋ฉด ๋ช…๋ น์–ด๋Š” ์กฐ์ธ์ฒ˜๋Ÿผ ์—ฌ๋Ÿฌ ํ…Œ์ด๋ธ”์„ ์ฐธ์กฐํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค
-- 
SELECT * 
FROM EMPLOYEES 
WHERE DEPARTMENT_ID  = (SELECT DEPARTMENT_ID 
                    FROM EMPLOYEES 
                    WHERE EMPLOYEE_ID = 141) 
AND EMPLOYEE_ID NOT IN (141) ;

--

--SELECT EMPLOYEE_ID, MIN(SALARY)
--FROM EMPLOYEES 
--GROUP BY EMPLOYEE_ID;

SELECT *
FROM EMPLOYEES 
WHERE SALARY = (SELECT MIN(SALARY) FROM EMPLOYEES);
-- 

SELECT DEPARTMENT_ID, MIN(SALARY)
FROM EMPLOYEES
GROUP BY DEPARTMENT_ID
HAVING MIN(SALARY) > (SELECT MIN(SALARY) FROM EMPLOYEES); 



-- 100, 999
-- ์„œ๋ธŒ์ฟผ๋ฆฌ ๋ฆฌํ„ด ๊ฐ’์ด NULL์ธ ๊ฒฝ์šฐ-> NVLํ•จ์ˆ˜ ํ™œ์šฉํ•œ ์ˆ˜์ • ํ•„ 
select last_name, commission_pct
from employees
where commission_pct = (select commission_pct
                    from employees
                    where employee_id=100);
                    
    -- ์„œ๋ธŒ์ฟผ๋ฆฌ ๋ฆฌํ„ด ๊ฐ’์ด NULL์ธ ๊ฒฝ์šฐ                
                    select commission_pct
                    from employees
                    where employee_id=100;
                    
-- ์„œ๋ธŒ์ฟผ๋ฆฌ ๋ฆฌํ„ด ๊ฐ’์ด NULL์ธ ๊ฒฝ์šฐ-> NVLํ•จ์ˆ˜ ํ™œ์šฉํ•œ ์ˆ˜์ • ํ•„ 
select last_name, commission_pct
from employees
where nvl(commission_pct,0) = (select nvl(commission_pct,0)
                    from employees
                    where employee_id=100);
-- 
select last_name, commission_pct
from employees
where nvl(commission_pct,0) = (select nvl(commission_pct,0)
                    from employees
                    where employee_id=999);
            
            select nvl(commission_pct,0)
                    from employees
                    where employee_id=999;
                    
-- ์„œ๋ธŒ์ฟผ๋ฆฌ ๋ฐ˜ํ™˜ ๊ฐ’์ด ์—ฌ๋Ÿฌ๊ฐœ์ธ ๊ฒฝ์šฐ '=' ์•„๋‹Œ 
-- IN ์—ฐ์‚ฐ์ž OR '=ANY' ์‚ฌ์šฉ ํ•„ 

SELECT last_name, salary, department_id
FROM employees
WHERE salary IN (SELECT MIN(salary)
 FROM employees
GROUP BY department_id);

-- ALL ์–ด๋–ค ๊ฐ’๊ณผ ๋น„๊ตํ•ด๋„ ๊ทธ ๊ฐ’๋ณด๋‹ค ์ž‘์•„์•ผ ํ•œ๋‹ค
-- ALL๋ณด๋‹ค ์ž‘์€ ๊ฒƒ์€ MIN ๋ณด๋‹ค ์ž‘๋‹ค
------------------------------- 
SELECT 
FROM
WHERE
employee_id, last_name, job_id, salary
employees
salary < ANY (9000,6000,4200)
(SELECT salary 
FROM employees
WHERE job_id = 'IT_PROG')
AND job_id <> 'IT_PROG';
-- OR 
SELECT 
FROM
WHERE
employee_id, last_name, job_id, salary
employees
salary < MAX (9000,6000,4200)
(SELECT salary 
FROM employees
WHERE job_id = 'IT_PROG')
AND job_id <> 'IT_PROG';
-----------------------------------
SELECT 
FROM
WHERE
employee_id, last_name, job_id, salary
employees
salary > ANY (9000,6000,4200)
(SELECT salary 
FROM employees
WHERE job_id = 'IT_PROG')
AND job_id <> 'IT_PROG';

-- OR 

SELECT 
FROM
WHERE
employee_id, last_name, job_id, salary
employees
salary > MIN (9000,6000,4200)
(SELECT salary 
FROM employees
WHERE job_id = 'IT_PROG')
AND job_id <> 'IT_PROG';
---------------------------------
SELECT 
FROM
WHERE
employee_id, last_name, job_id, salary
employees
salary < ALL (9000,6000,4200)
(SELECT salary 
FROM employees
WHERE job_id = 'IT_PROG')
AND job_id <> 'IT_PROG';

-- OR 

SELECT 
FROM
WHERE
employee_id, last_name, job_id, salary
employees
salary < MIN (9000,6000,4200)
(SELECT salary 
FROM employees
WHERE job_id = 'IT_PROG')
AND job_id <> 'IT_PROG';
---------------------------------
SELECT 
FROM
WHERE
employee_id, last_name, job_id, salary
employees
salary > ALL (9000,6000,4200)
(SELECT salary 
FROM employees
WHERE job_id = 'IT_PROG')
AND job_id <> 'IT_PROG';

-- OR 

SELECT 
FROM
WHERE
employee_id, last_name, job_id, salary
employees
salary > MAX (9000,6000,4200)
(SELECT salary 
FROM employees
WHERE job_id = 'IT_PROG')
AND job_id <> 'IT_PROG';
-----------------------------
  • Multiple-Row Subquery
    โ€ข <ANY ๋Š” ์ตœ๋Œ€๊ฐ’๋ณด๋‹ค ์ž‘์Œ์„ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค.
    >ANY ๋Š” ์ตœ์†Œ๊ฐ’๋ณด๋‹ค ํผ์„ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค.
    =ANY ๋Š” IN๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค.
    โ€ข <ALL ์€ ์ตœ์†Œ๊ฐ’๋ณด๋‹ค ์ž‘์Œ์„ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค.
    >ALL ์€ ์ตœ๋Œ€๊ฐ’๋ณด๋‹ค ํผ์„ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค.
-- ALL ์–ด๋–ค ๊ฐ’๊ณผ ๋น„๊ตํ•ด๋„ ๊ทธ ๊ฐ’๋ณด๋‹ค ์ž‘์•„์•ผ ํ•œ๋‹ค
-- ALL๋ณด๋‹ค ์ž‘์€ ๊ฒƒ์€ MIN ๋ณด๋‹ค ์ž‘๋‹ค
-------------------------------
SELECT
FROM
WHERE
employee_id, last_name, job_id, salary
employees
salary < ANY (9000,6000,4200)
(SELECT salary
FROM employees
WHERE job_id = 'IT_PROG')
AND job_id <> 'IT_PROG';
-- OR
SELECT
FROM
WHERE
employee_id, last_name, job_id, salary
employees
salary < MAX (9000,6000,4200)
(SELECT salary
FROM employees
WHERE job_id = 'IT_PROG')
AND job_id <> 'IT_PROG';
-----------------------------------
SELECT
FROM
WHERE
employee_id, last_name, job_id, salary
employees
salary > ANY (9000,6000,4200)
(SELECT salary
FROM employees
WHERE job_id = 'IT_PROG')
AND job_id <> 'IT_PROG';
-- OR
SELECT
FROM
WHERE
employee_id, last_name, job_id, salary
employees
salary > MIN (9000,6000,4200)
(SELECT salary
FROM employees
WHERE job_id = 'IT_PROG')
AND job_id <> 'IT_PROG';
---------------------------------
SELECT
FROM
WHERE
employee_id, last_name, job_id, salary
employees
salary < ALL (9000,6000,4200)
(SELECT salary
FROM employees
WHERE job_id = 'IT_PROG')
AND job_id <> 'IT_PROG';
-- OR
SELECT
FROM
WHERE
employee_id, last_name, job_id, salary
employees
salary < MIN (9000,6000,4200)
(SELECT salary
FROM employees
WHERE job_id = 'IT_PROG')
AND job_id <> 'IT_PROG';
---------------------------------
SELECT
FROM
WHERE
employee_id, last_name, job_id, salary
employees
salary > ALL (9000,6000,4200)
(SELECT salary
FROM employees
WHERE job_id = 'IT_PROG')
AND job_id <> 'IT_PROG';
-- OR
SELECT
FROM
WHERE
employee_id, last_name, job_id, salary
employees
salary > MAX (9000,6000,4200)
(SELECT salary
FROM employees
WHERE job_id = 'IT_PROG')
AND job_id <> 'IT_PROG';
-----------------------------
profile
heejoojeon@daou.co.kr

0๊ฐœ์˜ ๋Œ“๊ธ€