๐Ÿ’– [ORACLE]_23.02.23

โ€์ „ํฌ์ฃผยท2023๋…„ 2์›” 23์ผ
1

๐Ÿงก ๋ณต์Šต

select
from
where
order by
offset 5 rows
fetch first 5 row only

๐Ÿงก 2day ๋ณต์Šต ๋ฌธ์ œ ํ’€์ด

๐Ÿ”ฝ ์Šค์Šค๋กœ ํ’€์ด

-- ์Šค์Šค๋กœ ํ’€์ด 
-- 1.
ALTER SESSION SET NLS_DATE_FORMAT = 'YYYY-MM-DD';
SELECT LAST_NAME, HIRE_DATE
FROM EMPLOYEES
WHERE HIRE_DATE = '2014-03-15';

-- 2. 
SELECT LAST_NAME, ROUND(SYSDATE-HIRE_DATE) WORKING_DAYS
FROM EMPLOYEES;

-- 3. 
SELECT LAST_NAME, TO_CHAR(HIRE_DATE, 'YYYY') HIRE
FROM EMPLOYEES ; 

-- 4. 
SELECT LAST_NAME, TO_CHAR(HIRE_DATE, 'YYYY') HIRE
FROM EMPLOYEES
WHERE TO_CHAR(HIRE_DATE, 'YYYY') = '2011';

-- 5. 
SELECT LAST_NAME, SALARY, UPPER(LAST_NAME)
FROM EMPLOYEES
WHERE UPPER(LAST_NAME) = 'KING';

-- 6. 
SELECT FIRST_NAME, LAST_NAME, substr(FIRST_name, 1, 3)||substr(last_name, -2)USERID
FROM EMPLOYEES; 

๐Ÿ”ฝ ํ•ด์„ค ๋น„๊ต

-- ํ•ด์„ค ๋น„๊ต 

-- 1. 
//AS(์•Œ๋ฆฌ์•„์Šค, ๋ณ„์นญ)์€ WHERE ์ ˆ์— ์‚ฌ์šฉ ๋ถˆ๊ฐ€ 
// ๋‚ ์งœ ๋ฐ์ดํ„ฐ๋ฅผ ๋ฌธ์žํ˜•์œผ๋กœ ๋ณ€ํ™˜ํ•˜๋ฉด ์„ธ์…˜์„ ๋ณ€๊ฒฝํ•  ํ•„์š”์—†์ด ์›ํ•˜๋Š” ํ˜•ํƒœ์˜ ๋‚ ์งœ ๋ฐ์ดํ„ฐ๋กœ ๋ณ€๊ฒฝ ๊ฐ€๋Šฅ 
select last_name, to_char(hire_date, 'yyyy/mm/dd') hire_date
from employees
where hire_date=to_date
('15-MAR-14','DD-MON-RR');

-- 2. 
select last_name, trunc(sysdate- hire_date) working_days
from employees;
// ์ถœ๋ ฅ ๊ฐ’์ด ๋‹ค๋ฅผ ์ˆ˜ ์žˆ์Œ 

--3. 
select last_name, to_char(hire_date, 'yyyy') hire_year
from employees;

--4. 
//AS(์•Œ๋ฆฌ์•„์Šค, ๋ณ„์นญ)์€ WHERE ์ ˆ์— ์‚ฌ์šฉ ๋ถˆ๊ฐ€ 
select last_name, to_char(hire_date, 'yyyy') hire_year
from employees
where to_char(hire_date,'yyyy')='2011';

--5. 
//AS(์•Œ๋ฆฌ์•„์Šค, ๋ณ„์นญ)์€ WHERE ์ ˆ์— ์‚ฌ์šฉ ๋ถˆ๊ฐ€ 
select last_name, salary
from employees
where upper(last_name) ='KING';
// where LOWER(last_name) ='king';

--6. 
// SUBSTR ํ•จ์ˆ˜: ๋ฌธ์ž์—ด ์ผ๋ถ€ ์ถ”์ถœ
// ๋ฌธ์ž์—ด ๊ฒฐํ•ฉ : || OR CONCAT ํ•จ์ˆ˜ ์‚ฌ์šฉ 

select first_name, last_name,              
    substr(first_name, 1,3)||substr(last_name,-2) 
    userid
from employees;

select first_name, last_name,
concat(substr(first_name, 1,3),substr(last_name, -2)) 
userid
from employees;

๐Ÿงก SQL ์Šคํฌ๋ฆฝํŠธ ์‹คํ–‰ ์‹ค์Šต

๐Ÿ”ฝ DATETYPE.SQL

-- DATETYPE.SQL 
alter session set nls_date_format='yyyy/mm/dd';
update emp
    set hiredate=to_date('1980/12/17 13:20:19','yyyy/mm/dd hh24:mi:ss')
    where empno=7369;
select * from emp;

-- 
SELECT ENAME, TO_CHAR(HIREDATE, 'YY/MM/DD') HIREDATE
FROM EMP 
WHERE TO_CHAR(HIREDATE, 'YY/MM/DD') = '80/12/17'; 

-- SELECT * FROM EMP 
-- WHERE EMPLOYEES;
-- HIREDATE ๋ฐ์ดํ„ฐ ํƒ€์ž…์€ ORACLE ๋‚ด hh:mi:ss ๋„ ์ธ์‹๋จ. 
-- ๋”ฐ๋ผ์„œ ์„ธ์…˜(SESSION) ํ˜•์‹์ด ๋™์ผํ•˜๋”๋ผ๋„ ๋ฐ˜๋“œ์‹œ TO_CHAR ๋กœ ๋ฐ์ดํ„ฐ ํƒ€์ž… ๋ณ€ํ™˜ ํ›„ WHERE ์ ˆ ๋‚ด ๋‚ ์งœ ๋ฐ์ดํ„ฐ ๋น„๊ต ํ•„ 

-- SQL ํŠœ๋‹ ๊ณ ๋ คํ•˜์ง€ ์•Š์€ ์˜ˆ
SELECT * 
FROM EMP
WHERE TO_CHAR(HIREDATE, 'YYYY/MM/DD') = TO_DATE('1980/12/17', 'YYYY/MM/DD'); 

SELECT * 
FROM EMP
WHERE HIREDATE between 
to_date('19801217010000', 'YYYYMMDDHHMISS') 
and to_date('19801217120000', 'YYYYMMDDHHMISS');

SELECT * 
FROM EMP
WHERE HIREDATE between
to_date('1980/12/17', 'yyyy/mm/dd') 
and to_date('1980/12/17', 'yyyy/mm/dd')- 1/86400;
-- 1์ดˆ : 1 / (24*60*60) = 1 / 86400

rollback; 

-- emp ํ…Œ์ด๋ธ”์—์„œ 11์›”์— ์ž…์‚ฌํ•œ ์‚ฌ์› ์ถ”์ถœ
-- ์ปฌ๋Ÿผ ๋ณ€๊ฒฝ ํ•„์š” like, to_char ํ™œ์šฉ ๊ฐ€๋Šฅ 

SELECT ename, hiredate 
FROM EMP
WHERE TO_CHAR(HIREDATE, 'MM') = '11'; 
 
-- or 

SELECT ename, hiredate 
FROM EMP
WHERE TO_CHAR(HIREDATE, 'MM') like '11'; 

-- or 

select * from emp
where hiredate like '%/11/%';  

-- ์งˆ์˜๋ฌธ์„ ์ถฉ์กฑํ•˜์ง€ ๋ชปํ•œ ์˜ˆ์‹œ 
select * from emp
where hiredate like '%11%' ; 
-- 11์„ ๊ฐ€์ง„ ๋ชจ๋“  hiredate๊ฐ€ ์ถœ๋ ฅ๋จ. 

-- ํ…Œ์ด๋ธ” ์ƒ ๋ณ€ํ™” ๋ฐœ์ƒ์‹œ to_char ์‚ฌ์šฉ
WHERE TO_CHAR(HIREDATE, 'MM') = '11';

๐Ÿงก 5์žฅ ๋ณ€ํ™˜ ํ•จ์ˆ˜ ๋ฐ ์ผ๋ฐ˜ ํ•จ์ˆ˜

๐Ÿค CASE ํ‘œํ˜„์‹

  • IF-THEN-ELSE ๋ฌธ ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•˜์—ฌ ์กฐ๊ฑด๋ถ€ ์กฐํšŒ๋ฅผ ํŽธ๋ฆฌํ•˜๊ฒŒ ์ˆ˜ํ–‰ํ•˜๋„๋ก ํ•จ
 
--* CASE ๋ฌธ์˜ ์‚ฌ์šฉ๋ฒ•
--CASE๋ฌธ์€ ์—ฌํƒ€ ๋‹ค๋ฅธ ํ”„๋กœ๊ทธ๋ž˜๋ฐ ์–ธ์–ด๋“ค๊ณผ ๊ฐ™์ด ์กฐ๊ฑด์ด๋‚˜ ๊ฐ’์— ํ•ด๋‹นํ•˜๋Š” ๊ฒฐ๊ณผ๊ฐ’์„ RETURNํ•˜๋Š”๋ฐ ์‚ฌ์šฉํ•œ๋‹ค.
--CASE๋ฌธ์—๋Š” ์‚ฌ์šฉ๋ฒ•์€ ๊ฐ™์ง€๋งŒ ์กฐ๊ฑด์ด ๋‹ค๋ฅธ 2๊ฐ€์ง€ ๋ฐฉ์‹์ด ์กด์žฌํ•œ๋‹ค. ๊ฐ’์— ๋”ฐ๋ผ ๊ฒฐ๊ณผ๊ฐ’์„ RETURNํ•˜๋Š” ๋ฐฉ์‹๊ณผ ์กฐ๊ฑด์— ๋”ฐ๋ผ ๊ฒฐ๊ณผ๊ฐ’์„ RETURNํ•˜๋Š” ๋ฐฉ์‹์ด ๊ทธ๊ฒƒ์ด๋‹ค.
-- null๊ฐ’ ํ‘œํ˜„ํ•˜์—ฌ ์ถ”์ถœ์‹œ case ๊ตฌ๋ฌธ ๋‚ด  else ์ƒ๋žต ๊ฐ€๋Šฅ
--์ฒซ๋ฒˆ์งธ ๋ฐฉ์‹
/* CASE ๋Œ€์ƒ๊ฐ’ WHEN ๊ฐ’1 THEN ๊ฒฐ๊ณผ1
            WHEN ๊ฐ’2 THEN ๊ฒฐ๊ณผ2
                        .
                        .
                        .
            WHEN ๊ฐ’n THEN ๊ฒฐ๊ณผm END;*/
--๋‘๋ฒˆ์งธ ๋ฐฉ์‹          
/*CASE WHEN ์กฐ๊ฑด1 THEN ๊ฒฐ๊ณผ1
     WHEN ์กฐ๊ฑด2 THEN ๊ฒฐ๊ณผ2
                .
                .
                .
     WHEN ์กฐ๊ฑดn THEN ๊ฒฐ๊ณผm END;*/

-- 

+) ๊ต์žฌ 103p ๋ฌธ๊ตฌ ์ˆ˜์ •
Scalar Subquery -> searched case expression
Searched Case Expression์€ 'Condition'์„ ์‚ฌ์šฉํ•˜์—ฌ Return ๊ฐ’์ด ์ •ํ•ด์ง

๐Ÿค DECODE ํ•จ์ˆ˜

  • IF-THEN-ELSE ๋ฌธ์˜ ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•˜์—ฌ ์กฐ๊ฑด๋ถ€ ์กฐํšŒ๋ฅผ ์ˆ˜ํ–‰
  • decode ํ–‰ as ์ง€์ • ํ•„
  • DECODE ํ•จ์ˆ˜๋Š” ๋‹ค์–‘ํ•œ ์–ธ์–ด์—์„œ ์‚ฌ์šฉ๋˜๋Š” IF-THEN-ELSE ๋…ผ๋ฆฌ์™€ ๋น„์Šทํ•œ ๋ฐฉ์‹์œผ๋กœ ํ‘œํ˜„์‹์„ ๋””์ฝ”๋”ฉํ•ฉ๋‹ˆ๋‹ค.
  • DECODE ํ•จ์ˆ˜๋Š” ํ‘œํ˜„์‹(expression)์„ ๊ฐ ๊ฒ€์ƒ‰ ๊ฐ’(search)๊ณผ ๋น„๊ตํ•ด์„œ ๋™์ผํ•œ ๊ฐ’์ด๋ฉด result๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.
  • ๊ธฐ๋ณธ๊ฐ’(default)์ด ์ƒ๋žต๋œ ๊ฒฝ์šฐ ๊ฒ€์ƒ‰ ๊ฐ’๊ณผ ์ผ์น˜ํ•˜๋Š” ๊ฒฐ๊ณผ ๊ฐ’์ด ์—†์œผ๋ฉด null ๊ฐ’์ด ๋ฐ˜ํ™˜๋ฉ๋‹ˆ๋‹ค.

๐Ÿค DECODE ์‹ค์Šต

๐Ÿ”ฝ SQL_์‹ค์Šต 32p

-- ์กฐ๊ฑด๋ถ€ ํ‘œํ˜„์‹ ์‹ค์Šต 
select ename, deptno, sal
    case deptno when 10 then sal *1.1
                when 20 then sal *1.2
                when 30 then sal *1.3
                else sal
    end as inc_sal 
from emp; 
--
select ename, deptno, sal, decode(deptno, 10, sal*1.1
                                          20, sal*1.2
                                          30, sal*1.3, sal) as inc_sal 
from emp; 
--
select ename, deptno, sal, 
        case when deptno in (10, 20) then sal *1.1
             when deptno = 30        then sal *1.2
        end as inc_sal 
from emp; 
--

๐Ÿงก 6์žฅ ๊ทธ๋ฃน ํ•จ์ˆ˜

โ€ข ์‚ฌ์šฉ ๊ฐ€๋Šฅํ•œ ๊ทธ๋ฃน ํ•จ์ˆ˜ ์†Œ๊ฐœ
โ€ข GROUP BY ์ ˆ์„ ์‚ฌ์šฉํ•˜์—ฌ ๋ฐ์ดํ„ฐ ๊ทธ๋ฃนํ™”
โ€ข HAVING ์ ˆ์„ ์‚ฌ์šฉํ•˜์—ฌ ๊ทธ๋ฃนํ™”๋œ ํ–‰ ์ œํ•œ

  • ๊ทธ๋ฃน ํ•จ์ˆ˜๋Š” ํ–‰ ์ง‘ํ•ฉ ์—ฐ์‚ฐ์„ ์ˆ˜ํ–‰ํ•˜์—ฌ ๊ทธ๋ฃน๋ณ„๋กœ ํ•˜๋‚˜์˜ ๊ฒฐ๊ณผ๋ฅผ ์‚ฐ์ถœํ•ฉ๋‹ˆ๋‹ค.

  • ๊ทธ๋ฃน ํ•จ์ˆ˜ ๊ฐœ๋…

    • ๋‹จ์ผ ํ–‰ ํ•จ์ˆ˜์™€ ๋‹ฌ๋ฆฌ ๊ทธ๋ฃน ํ•จ์ˆ˜๋Š” ํ–‰ ์ง‘ํ•ฉ์— ๋Œ€ํ•ด ์‹คํ–‰๋˜์–ด ๊ทธ๋ฃน๋‹น ํ•˜๋‚˜์˜ ๊ฒฐ๊ณผ๋ฅผ ์‚ฐ์ถœํ•ฉ๋‹ˆ๋‹ค.
    • ํ…Œ์ด๋ธ” ์ „์ฒด๊ฐ€ ํ–‰ ์ง‘ํ•ฉ์ด ๋˜๊ฑฐ๋‚˜ ํ…Œ์ด๋ธ”์„ ์—ฌ๋Ÿฌ ๊ทธ๋ฃน๋“ค๋กœ ๋ถ„ํ• ํ•˜์—ฌ ํ–‰ ์ง‘ํ•ฉ๋“ค์ด ๋  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค
  • ๊ทธ๋ฃน ํ•จ์ˆ˜ ์œ ํ˜•

    โ€ข AVG
    โ€ข COUNT
    โ€ข MAX
    โ€ข MIN
    โ€ข SUM
    โ€ข STDDEV
    โ€ข VARIANCE

  • ๊ทธ๋ฃน ํ•จ์ˆ˜ ์‚ฌ์šฉ

    • ์ˆซ์ž ๋ฐ์ดํ„ฐ์— ๋Œ€ํ•ด AVG, SUM, MIN, MAX, COUNT ์‚ฌ์šฉ
      ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค.
    • ๋ฌธ์ž ๋ฐ ๋‚ ์งœ ๋ฐ์ดํ„ฐ์— ๋Œ€ํ•ด MIN, MAX, COUNT ์‚ฌ์šฉ ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค
    • ๊ทธ๋ฃน ํ•จ์ˆ˜๋Š” ์—ด์— ์žˆ๋Š” null ๊ฐ’์„ ๋ฌด์‹œํ•ฉ๋‹ˆ๋‹ค.
  • SUM
    ํ•ด๋‹น ์ปฌ๋Ÿผ ๊ฐ’๋“ค์— ๋Œ€ํ•œ ์ดํ•ฉ์„ ๊ตฌํ•˜๋Š” ํ•จ์ˆ˜์ด๋‹ค.

SELECT SUM(SAL) FROM EMP;
--> EMPํ…Œ์ด๋ธ”์— ์žˆ๋Š” SAL์ปฌ๋Ÿผ ๊ฐ’์˜ ์ดํ•ฉ์„ ๊ตฌํ•œ๋‹ค. 
  • AVG
    ํ•ด๋‹น ์ปฌ๋Ÿผ ๊ฐ’๋“ค์— ๋Œ€ํ•œ ํ‰๊ท ์„ ๊ตฌํ•˜๋Š” ํ•จ์ˆ˜์ด๋‹ค.
SELECT AVG(SAL) FROM EMP;
--> EMPํ…Œ์ด๋ธ”์— ์žˆ๋Š” SAL์ปฌ๋Ÿผ ๊ฐ’์˜ ํ‰๊ท ์„ ๊ตฌํ•œ๋‹ค. 
  • MAX, MIN
    MAX๋Š” ํ•ด๋‹น ์ปฌ๋Ÿผ ๊ฐ’๋“ค์— ๋Œ€ํ•œ ์ตœ๋Œ€๊ฐ’, MIN์€ ์ตœ์†Œ๊ฐ’์„ ๊ตฌํ•˜๋Š” ํ•จ์ˆ˜์ด๋‹ค.
SELECT MAX(SAL), MIN(SAL)
FROM EMP;
--> EMPํ…Œ์ด๋ธ”์— ์žˆ๋Š” SAL์ปฌ๋Ÿผ ๊ฐ’์˜ ์ตœ๋Œ€๊ฐ’๊ณผ ์ตœ์†Œ๊ฐ’์„ ๊ตฌํ•œ๋‹ค. 

+)

  • ๊ทธ๋ฃน ํ•จ์ˆ˜์˜ ๊ฒฐ๊ณผ๋Š” 1๊ฐœ์ด๊ธฐ ๋•Œ๋ฌธ์— ๊ทธ๋ฃน ํ•จ์ˆ˜๋ฅผ ์ ์šฉํ•˜์ง€ ์•Š์€ ๋‹จ์ˆœ ์ปฌ๋Ÿผ๊ณผ๋Š” ๊ฐ™์ด ์˜ฌ ์ˆ˜ ์—†๋‹ค. (GROUP BY์˜ ๊ธฐ์ค€์ด ๋œ ์ปฌ๋Ÿผ์€ ์˜ฌ ์ˆ˜ ์žˆ๋‹ค. )
SELECT MAX(SAL), ENAME
FROM EMP;
--> MAX(SAL)๊ฐ’์€ 1๊ฐœ์ด์ง€๋งŒ ENAME๊ฐ’์€ ๊ฐ’์ด ์—ฌ๋Ÿฌ ๊ฐœ์ด๊ธฐ ๋•Œ๋ฌธ์— ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ•œ๋‹ค. 
  • ๊ทธ๋ฃน ํ•จ์ˆ˜ ๋ฐ Null ๊ฐ’

    • NVL ํ•จ์ˆ˜๋Š” ๊ฐ•์ œ๋กœ ๊ทธ๋ฃน ํ•จ์ˆ˜์— null ๊ฐ’์ด ํฌํ•จ๋˜๋„๋ก ํ•ฉ๋‹ˆ๋‹ค.
  • 1-> null ์ œ์™ธ ํ‰๊ท  ๊ตฌํ•˜๊ธฐ (ํ•ฉ๊ณ„/20)

  • 2-> null ํฌํ•จ ํ‰๊ท  ๊ตฌํ•˜๊ธฐ (ํ•ฉ๊ณ„/4)

๐Ÿ”ฝ

  • COUNT ํ•จ์ˆ˜ ์‚ฌ์šฉ

  • COUNT ํ•จ์ˆ˜์˜ ์„ธ๊ฐ€์ง€ ์‚ฌ์šฉ๋ฒ•

    ๏‚Ÿ COUNT(*)
    SELECT ๋ฌธ์˜ ์กฐ๊ฑด์„ ์ถฉ์กฑํ•˜๋Š” ํ…Œ์ด๋ธ”์˜ ํ–‰ ์ˆ˜๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋ฉฐ ์—ฌ๊ธฐ์—๋Š” ์ค‘๋ณต ํ–‰ ๊ณผ ์—ด์— null ๊ฐ’์„ ํฌํ•จํ•œ ํ–‰์ด ํฌํ•จ๋ฉ๋‹ˆ๋‹ค.
    ๏‚Ÿ COUNT(expr)
    expr์— ์˜ํ•ด ์‹๋ณ„๋˜๋Š” ์—ด์— ์žˆ๋Š” null์ด ์•„๋‹Œ ๊ฐ’์˜ ์ˆ˜๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.
    ๏‚Ÿ COUNT(DISTINCT expr)
    expr์— ์˜ํ•ด ์‹๋ณ„๋˜๋Š” ์—ด์— ์žˆ๋Š” ๊ณ ์œ ํ•˜๊ณ  null์ด ์•„๋‹Œ๊ฐ’์˜ ์ˆ˜๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.

  • ๋ฐ์ดํ„ฐ ๊ทธ๋ฃน ์ƒ์„ฑ

  • ๋ฐ์ดํ„ฐ ์†Œ๊ทธ๋ฃน ์ƒ์„ฑ : GROUP BY

    • ์ง€๊ธˆ๊นŒ์ง€์˜ ์„ค๋ช…์—์„œ๋Š” ๋ชจ๋“  ๊ทธ๋ฃน ํ•จ์ˆ˜๊ฐ€ ํ…Œ์ด๋ธ” ์ „์ฒด๋ฅผ ํ•˜๋‚˜์˜ ์ปค๋‹ค๋ž€ ์ •๋ณด ๊ทธ๋ฃน์œผ๋กœ
      ์ทจ๊ธ‰ํ–ˆ์Šต๋‹ˆ๋‹ค.
      ๊ทธ๋Ÿฌ๋‚˜ ํ…Œ์ด๋ธ”์„ ๋” ์ž‘์€ ๊ทธ๋ฃน๋“ค๋กœ ๋‚˜๋ˆ ์•ผ ํ•˜๋Š” ๊ฒฝ์šฐ๋„ ์žˆ์Šต๋‹ˆ๋‹ค.
      ์ด๋Ÿฌํ•œ ์ž‘์—…์€ GROUP BY ์ ˆ์„ ์‚ฌ์šฉํ•˜์—ฌ ์ˆ˜ํ–‰ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
  • ๋ฐ์ดํ„ฐ ๊ทธ๋ฃน ์ƒ์„ฑ : GROUP BY ์ ˆ

  • GROUP BY ์ ˆ ์‚ฌ์šฉ์‹œ ์ฃผ์˜์‚ฌํ•ญ

  • select ํ•ญ๋ชฉ์— ๊ทธ๋ฃน์œผ๋กœ ๋ฌถ์ด๋Š” ์†์„ฑ์€ ๋ฐ˜๋“œ์‹œ group by๋กœ ๋ฌถ์–ด ์ค˜์•ผ ํ•œ๋‹ค. (ํŠœํ”Œ ์ˆ˜๊ฐ€ ๋‹ฌ๋ผ null ๋ฐœ์ƒ)

  • group by๋กœ ๋ฌถ์€ ์†์„ฑ์„ select ์ ˆ์— ๋ฐ˜๋“œ์‹œ ๊ธฐ์žฌํ•  ํ•„์š”๋Š” ์—†์Œ(๊ฐ€๋…์„ฑ ์œ„ํ•จ)


    ๐Ÿ”ฝ ๊ทธ๋ฃน ํ•จ์ˆ˜์— ์†ํ•˜์ง€ ์•Š๋Š” SELECT list์˜ ๋ชจ๋“  ์—ด์€ GROUP BY ์ ˆ์— ์žˆ์–ด์•ผ ํ•จ

  • GROUP BY๋กœ ํˆฌํ”Œ์„ ๊ทธ๋ฃน์œผ๋กœ ๋ฌถ์€ ํ›„ SELECT ์ ˆ์—๋Š” GROUP BY์—์„œ ์‚ฌ์šฉํ•œ <์†์„ฑ>๊ณผ ์ง‘ ๊ณ„ํ•จ์ˆ˜๋งŒ ๋‚˜์˜ฌ ์ˆ˜ ์žˆ๋‹ค.

    • ยง ๋งž๋Š” ์˜ˆ
      SELECT custid, SUM(saleprice) FROM Orders
      GROUP BY custid;
      • โ€ข ํ‹€๋ฆฐ ์˜ˆ
        SELECT bookid, SUM(saleprice) / SELECT ์ ˆ์— bookid ์†์„ฑ์ด ์˜ฌ ์ˆ˜ ์—†๋‹ค / FROM Orders
        GROUP BY custid;
  • ์—ฌ๋Ÿฌ ์—ด์„ GROUP BY ์ ˆ์— ์‚ฌ์šฉ
    ๐Ÿ”ฝ

  • where์ ˆ์—๋Š” group by ์‚ฌ์šฉ ๋ถˆ๊ฐ€ (having ํ•จ์ˆ˜ ์‚ฌ์šฉ ํ•„)
    ๐Ÿ”ฝ

  • HAVING ์ ˆ์„ ์‚ฌ์šฉํ•˜์—ฌ ๊ทธ๋ฃน ๊ฒฐ๊ณผ ์ œํ•œ
    ๐Ÿ”ฝ HAVING ์ ˆ์„ ์‚ฌ์šฉํ•˜์—ฌ ๊ทธ๋ฃน ๊ฒฐ๊ณผ ์ œํ•œ

  • HAVING ์ ˆ ์ฃผ์˜ ์‚ฌํ•ญ

  • sql_์‹ค์Šต 32p

    • WHERE ์ ˆ๊ณผ HAVING ์ ˆ์˜ ์ฐจ์ด์ 

      HAVING ์ ˆ์€ WHERE ์ ˆ๊ณผ ๋น„์Šทํ•˜์ง€๋งŒ ๊ทธ๋ฃน ์ „์ฒด ์ฆ‰, ๊ทธ๋ฃน์„ ๋‚˜ํƒ€๋‚ด๋Š” ๊ฒฐ๊ณผ ์ง‘ํ•ฉ์˜ ํ–‰์—๋งŒ ์ ์šฉ๋œ๋‹ค๋Š” ์ ์—์„œ ์ฐจ์ด๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค.
      ๋ฐ˜๋ฉด, WHERE ์ ˆ์€ ๊ฐœ๋ณ„ ํ–‰์— ์ ์šฉ๋ฉ๋‹ˆ๋‹ค. ์ฟผ๋ฆฌ์—๋Š” WHERE ์ ˆ๊ณผ HAVING ์ ˆ์ด ๋ชจ๋‘ ํฌํ•จ๋  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

      ๐Ÿ”ฝ ์ด๋ฏธ ์กด์žฌํ•˜๋Š” ๋ฐ์ดํ„ฐ ๊ฐ’์—์„œ ๊ทธ๋ฃนํ•‘ ์‹คํ–‰

      ๐Ÿ”ฝ ๊ทธ๋ฃนํ•‘ ์‹คํ–‰ ํ›„ ์ƒˆ๋กœ์šด ๊ทธ๋ฃน์— ์กฐ๊ฑด ๋ถ€์—ฌ (์กฐ๊ฑด ์ถฉ์กฑํ•˜๋Š” ๋ฐ์ดํ„ฐ๊ฐ€ ์—†์„ ์ˆ˜ ์žˆ์Œ)
  • ๊ทธ๋ฃน ํ•จ์ˆ˜ ์ค‘์ฒฉ

    ๐Ÿ”ฝ ์ตœ๊ณ  ํ‰๊ท  ๊ธ‰์—ฌ๋ฅผ ํ‘œ์‹œ

  • sql_์‹ค์Šต (โ€ข GROUP BY, HAVING ์ ˆ ์‚ฌ์šฉ)
    ๐Ÿ”ฝ ์‹ค์Šต ์ง„ํ–‰


-- ์กฐ๊ฑด๋ถ€ ํ‘œํ˜„์‹ ์‹ค์Šต 
select ename, deptno, sal
    case deptno when 10 then sal *1.1
                when 20 then sal *1.2
                when 30 then sal *1.3
                else sal
    end as inc_sal 
from emp; 
--
select ename, deptno, sal, decode(deptno, 10, sal*1.1
                                        20, sal*1.2
                                        30, sal*1.3, sal) as inc_sal 
from emp; 
--
select ename, deptno, sal, 
        case when deptno in (10, 20) then sal *1.1
             when deptno = 30        then sal *1.2
        end as inc_sal 
from emp; 
--

-- GROUP BY, HAVING ์ ˆ ์‚ฌ์šฉ
SELECT deptno, SUM(sal)
 FROM emp
 GROUP BY deptno ;
 
 SELECT SUM(sal)
 FROM emp
 GROUP BY deptno ;
 
 -- 
  SELECT deptno, SUM(sal) FROM emp
 GROUP BY deptno ;
 
   SELECT SUM(sal) FROM emp
 GROUP BY deptno ;
 --
  SELECT deptno, job, SUM(sal) FROM emp
 GROUP BY deptno, job ;
-- where ์ ˆ ๊ทธ๋ฃนํ•จ์ˆ˜ ์‚ฌ์šฉ ๋ถˆ๊ฐ€ 
--SELECT deptno, AVG(sal) FROM emp
-- WHERE AVG(sal) > 1500
-- GROUP BY deptno ;

SELECT deptno, AVG(sal) FROM emp
GROUP BY deptno 
 HAVING AVG(sal) > 2500; 
-- or 
 SELECT  AVG(sal) FROM emp
GROUP BY deptno 
 HAVING AVG(sal) > 2500; 
 
-- 
SELECT deptno, SUM(sal) FROM emp
 WHERE deptno IN (20,30) GROUP BY deptno
 HAVING SUM(sal) > 10000 ;

SELECT deptno, SUM(sal) FROM emp
 GROUP BY deptno HAVING SUM(sal) > 10000
 AND deptno IN (20,30) ;
--
 SELECT deptno, AVG(sal) FROM emp
 WHERE JOB = 'CLERK'
 GROUP BY deptno HAVING AVG(sal) > 1000 ;

-- ๊ทธ๋ฃน ํ•จ์ˆ˜์˜ ์ค‘์ฒฉ
SELECT SUM(sal)
 FROM emp
 GROUP BY deptno ;
  
 SELECT MAX(SUM(sal))
 FROM emp
 GROUP BY deptno ;
 
 
 SELECT AVG(MAX(SUM(sal)))
 2 FROM emp
 3 GROUP BY deptno ;
SELECT AVG(MAX(SUM(sal)))

๐Ÿงก 7์žฅ. ์กฐ์ธ์„ ์‚ฌ์šฉํ•˜์—ฌ ๋‹ค์ค‘ ํ…Œ์ด๋ธ”์˜ ๋ฐ์ดํ„ฐ ํ‘œ์‹œ

  • ๋‹จ์› ๋‚ด์šฉ

    โ€ข Equijoin ๋ฐ Nonequijoin ๋ฌธ ์ž‘์„ฑ
    โ€ข Outer Join์˜ ์ดํ•ด
    โ€ข Self Join์˜ ์ดํ•ด
    โ€ข Cartesian Product

  • ์กฐ์ธ ๋ช…๋ น์–ด ๊ตฌ๋ฌธ

  • ๊ณตํ†ต ์ปฌ๋Ÿผ๋ช…์— ์กฐ๊ฑด์„ ์ฃผ์–ด ๋‚ด๋ถ€์กฐ์ธ(INNER JOIN) ์ˆ˜ํ–‰

  • ANSI Join ์‚ฌ์šฉ(Equi Join)

    SQL> SELECT e.empno, e.ename, e.sal, d.deptno, d.dname, d.loc
    FROM emp e JOIN dept d
    ON e.deptno = d.deptno ;

  • Oracle ์กฐ์ธ ๊ตฌ๋ฌธ : Equijoin

  • FK ์„ค์ •์‹œ ์ฐธ์กฐํ•˜๋Š” ํ…Œ์ด๋ธ” ๋ช… ๊ธฐ์žฌ ํ•„์ˆ˜
    ๐Ÿ”ฝ

  • ์„ธ ๊ฐœ ์ด์ƒ์˜ ํ…Œ์ด๋ธ” ์กฐ์ธ

    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;

  • Oracle ์กฐ์ธ ๊ตฌ๋ฌธ : Nonequijoin

      1. ์ •์˜
    • ๋‘ ๊ฐœ์˜ ํ…Œ์ด๋ธ” ๊ฐ„์— ์นผ๋Ÿผ ๊ฐ’๋“ค์ด ์„œ๋กœ ์ •ํ™•ํ•˜๊ฒŒ ์ผ์น˜ํ•˜์ง€ ์•Š๋Š” ๊ฒฝ์šฐ์— ์‚ฌ์šฉ๋œ๋‹ค.

    • Non EQUI JOIN์˜ ๊ฒฝ์šฐ์—๋Š” โ€œ=โ€ ์—ฐ์‚ฐ์ž๊ฐ€ ์•„๋‹Œ ๋‹ค๋ฅธ(Between, >, >=, <, <= ๋“ฑ) ์—ฐ์‚ฐ์ž๋“ค์„ ์‚ฌ์šฉํ•˜์—ฌ JOIN์„ ์ˆ˜ํ–‰ํ•˜๋Š” ๊ฒƒ์ด๋‹ค.

    • ๋‘ ๊ฐœ์˜ ํ…Œ์ด๋ธ”์ด PK-FK๋กœ ์—ฐ๊ด€๊ด€๊ณ„๋ฅผ ๊ฐ€์ง€๊ฑฐ๋‚˜ ๋…ผ๋ฆฌ์ ์œผ๋กœ ๊ฐ™์€ ๊ฐ’์ด ์กด์žฌํ•˜๋Š” ๊ฒฝ์šฐ์—๋Š” โ€œ=โ€ ์—ฐ์‚ฐ์ž๋ฅผ ์ด์šฉํ•˜์—ฌ EQUI JOIN์„ ์‚ฌ์šฉํ•œ๋‹ค.

    • ๊ทธ๋Ÿฌ๋‚˜ ๋‘ ๊ฐœ์˜ ํ…Œ์ด๋ธ” ๊ฐ„์— ์นผ๋Ÿผ ๊ฐ’๋“ค์ด ์„œ๋กœ ์ •ํ™•ํ•˜๊ฒŒ ์ผ์น˜ํ•˜์ง€ ์•Š๋Š” ๊ฒฝ์šฐ์—๋Š” EQUI JOIN์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์—†๋‹ค.

    • ์ด๋Ÿฐ ๊ฒฝ์šฐ Non EQUI JOIN์„ ์‹œ๋„ํ•  ์ˆ˜ ์žˆ์œผ๋‚˜ ๋ฐ์ดํ„ฐ ๋ชจ๋ธ์— ๋”ฐ๋ผ์„œ Non EQUI JOIN์ด ๋ถˆ๊ฐ€๋Šฅํ•œ ๊ฒฝ์šฐ๋„ ์žˆ๋‹ค.

  • Nonequijoin ์‹ค์Šต

  • ๋ถ€์„œ๊ฐ€ ํ• ๋‹น๋˜์ง€ ์•Š์€ ์‚ฌ์›๊นŒ์ง€ ๋ชจ๋“  ์‚ฌ์›๊ณผ
    ์‚ฌ์›์ด ํ• ๋‹น๋˜์ง€ ์•Š์€ ๋ถ€์„œ๊นŒ์ง€ ๋ชจ๋“  ๋ถ€์„œ๋ฅผ ํ•จ๊ป˜ ํ‘œ์‹œ
    ๐Ÿ”ฝ


SELECT e.last_name, e.department_id, d.department_name 
FROM employees e, departments d
WHERE e.department_id = d.department_id(+)
UNION 
SELECT e.last_name, e.department_id, d.department_name
FROM e.last_name, e.department_id, d.department_name
WHERE e.department_id(+) = d.department_id;
  • Oracle ์กฐ์ธ ๊ตฌ๋ฌธ : Self Join

  • ์…€ํ”„ ์กฐ์ธ์‹œ ํ…Œ์ด๋ธ”๋ช… AS ํ•„
    ๐Ÿ”ฝ

  • ์…€ํ”„ ์กฐ์ธ ์˜ˆ์‹œ

-- ์ค‘๋ณต์„ ์ฐพ์•„ ํ‘œ์‹œ 
SELECT C1.ID, C1.NAME, C2.ID, C2.NAME
FROM COLOR C1, COLOR C2
WHERE C1.NAME = C2.NAME
AND C1.ID < C2.ID;

๐Ÿ”ฝ

  • ์™ธ๋ถ€ ์กฐ์ธ
-- ์ „ ๋ถ€์„œ์— ๋Œ€ํ•œ ๋ฐ์ดํ„ฐ๊ฐ€ ์ถ”์ถœ๋˜๋Š” ์™ธ๋ถ€ ์กฐ์ธ
-- outer join ์‹คํ–‰ ํ›„ ์กฐ๊ฑด ์‹คํ–‰๋จ 
select e.last_name, e.department_id, d.department_name, e.salary 
from employees e, departments d
where e.department_id(+) = d.department_id
and e.salary > 10000; 

-- outer join ๊นŒ์ง€ ์ˆ˜ํ–‰ํ•˜๊ธฐ ์œ„ํ•ด ์•„๋ž˜์™€ ๊ฐ™์ด ์‹คํ–‰ 
-- salary ์กฐ๊ฑด ์ถฉ์กฑ ํ›„ outer join ์‹คํ–‰๋จ 
-- ์ „ ๋ถ€์„œ์— ๋Œ€ํ•œ ๋ฐ์ดํ„ฐ๊ฐ€ ์ถ”์ถœ
select e.last_name, e.department_id, d.department_name, e.salary 
from employees e, departments d
where e.department_id(+) = d.department_id
and e.salary(+) > 10000; 

-- or 
SELECT e.last_name, e.department_id, d.department_name, e.salary                                     
  FROM employees e                             
  RIGHT OUTER JOIN departments d                
  ON (e.department_id = d.department_id)
and e.salary(+) > 10000; 

-- 
select e.last_name, d.department_name
from employees e, departments d
where e.department_id = d.department_id; 
  • NATURAL JOIN
SELECT e.employee_id, e.last_name, e.salary, e.department_id, d.department_id, d.department_name 
FROM employees e LEFT OUTER JOIN departments d
ON e.department_id = d.department_id 
  • ์˜ค๋ฅธ์ชฝ ์™ธ๋ถ€ ์กฐ์ธ ์˜ˆ์‹œ (right outer join)
    right (outer) join ์‚ฌ์šฉ์‹œ ์™ผ์ชฝ ํ…Œ์ด๋ธ”์— null ๊ฐ’์ด ์ƒ์„ฑ๋จ
select e.employee_id, e.last_name, e.salary, e.department_id, d.department_id, d.department_name 
from employees e, departments d
where e.department_id (+) = d.department_id; 

-- 5์žฅ ์—ฐ์Šต
-- 6์žฅ ์—ฐ์Šต
-- 7์žฅ ์—ฐ์Šต- oracle / ansi

๐Ÿค 5์žฅ ์—ฐ์Šต๋ฌธ์ œ ํ’€์ด

profile
heejoojeon@daou.co.kr

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