[Oracle] Solution to puzzles

Joy🌱·2023λ…„ 1μ›” 23일
0

🧩 Coding Challenges

λͺ©λ‘ 보기
15/20
post-thumbnail
post-custom-banner

🧩 DML

Q1

πŸ’β€ κ³Όλͺ©μœ ν˜• ν…Œμ΄λΈ”(TB_CLASS_TYPE)에 μ•„λž˜μ™€ 같은 데이터λ₯Ό μž…λ ₯ν•˜μ„Έμš”.

INSERT
    INTO TB_CLASS_TYPE
(
    CLASS_TYPE_NO, CLASS_TYPE_NAME
)
VALUES
(
    01, 'μ „κ³΅ν•„μˆ˜'
);

INSERT
    INTO TB_CLASS_TYPE
(
    CLASS_TYPE_NO, CLASS_TYPE_NAME
)
VALUES
(
    02, '전곡선택'
);

INSERT
    INTO TB_CLASS_TYPE
(
    CLASS_TYPE_NO, CLASS_TYPE_NAME
)
VALUES
(
    03, 'κ΅μ–‘ν•„μˆ˜'
);

INSERT
    INTO TB_CLASS_TYPE
(
    CLASS_TYPE_NO, CLASS_TYPE_NAME
)
VALUES
(
    04, 'ꡐ양선택'
);

INSERT
    INTO TB_CLASS_TYPE
(
    CLASS_TYPE_NO, CLASS_TYPE_NAME
)
VALUES
(
    05, '논문지도'
);

Q2

πŸ’β€ 좘 κΈ°μˆ λŒ€ν•™κ΅ ν•™μƒλ“€μ˜ 정보가 ν¬ν•¨λ˜μ–΄ μžˆλŠ” ν•™μƒμΌλ°˜μ •λ³΄ ν…Œμ΄λΈ”μ„ λ§Œλ“€κ³ μž ν•©λ‹ˆλ‹€. μ•„λž˜ λ‚΄μš©μ„ μ°Έκ³ ν•˜μ—¬ μ μ ˆν•œ SQL 문을 μž‘μ„±ν•˜μ„Έμš”. (μ„œλΈŒμΏΌλ¦¬λ₯Ό μ΄μš©ν•˜μ„Έμš”.)

CREATE TABLE TB_ν•™μƒμΌλ°˜μ •λ³΄ (
    ν•™λ²ˆ
,   학생이름
,   μ£Όμ†Œ
)
AS
SELECT
        STUDENT_NO
    ,   STUDENT_NAME
    ,   STUDENT_ADDRESS
    FROM TB_STUDENT;

Q3

πŸ’β€ κ΅­μ–΄κ΅­λ¬Έν•™κ³Ό ν•™μƒλ“€μ˜ μ •λ³΄λ§Œμ΄ ν¬ν•¨λ˜μ–΄ μžˆλŠ” 학과정보 ν…Œμ΄λΈ”μ„ λ§Œλ“€κ³ μž ν•©λ‹ˆλ‹€. μ•„λž˜ λ‚΄μš©μ„ μ°Έκ³ ν•˜μ—¬ μ μ ˆν•œ SQL문을 μž‘μ„±ν•˜μ„Έμš”. (힌트 : 방법은 닀양함, μ†Œμ‹ κ» μž‘μ„±ν•˜μ„Έμš”.)

CREATE TABLE TB_κ΅­μ–΄κ΅­λ¬Έν•™κ³Ό (
    ν•™λ²ˆ
,   학생이름
,   μΆœμƒλ…„λ„
,   κ΅μˆ˜μ΄λ¦„
)
AS
SELECT
        STUDENT_NO
    ,   STUDENT_NAME
    ,   19 || SUBSTR(STUDENT_SSN , 1, 2)
    ,   PROFESSOR_NAME
    FROM TB_STUDENT S
    JOIN TB_PROFESSOR ON (COACH_PROFESSOR_NO = PROFESSOR_NO)
    JOIN TB_DEPARTMENT D ON (S.DEPARTMENT_NO = D.DEPARTMENT_NO)
    WHERE D.DEPARTMENT_NAME = 'κ΅­μ–΄κ΅­λ¬Έν•™κ³Ό';

Q4

πŸ’β€ ν˜„ ν•™κ³Όλ“€μ˜ 정원을 10% μ¦κ°€μ‹œν‚€κ²Œ λ˜μ—ˆμŠ΅λ‹ˆλ‹€. 이에 μ‚¬μš©ν•œ SQL 문을 μž‘μ„±ν•˜μ„Έμš”. (단, λ°˜μ˜¬λ¦Όμ„ μ‚¬μš©ν•˜μ—¬ μ†Œμˆ˜μ  μžλ¦Ώμˆ˜λŠ” 생기지 μ•Šλ„λ‘ ν•©λ‹ˆλ‹€.)

UPDATE 
        TB_DEPARTMENT
    SET CAPACITY = ROUND(CAPACITY + (CAPACITY * 0.1), -1);

Q5

πŸ’β€ ν•™λ²ˆ A413042 인 λ°•κ±΄μš° ν•™μƒμ˜ μ£Όμ†Œκ°€ "μ„œμšΈμ‹œ μ’…λ‘œκ΅¬ μˆ­μΈλ™ 181-21 "둜 λ³€κ²½λ˜μ—ˆλ‹€κ³  ν•©λ‹ˆλ‹€. μ£Όμ†Œμ§€λ₯Ό μ •μ •ν•˜κΈ° μœ„ν•΄ μ‚¬μš©ν•  SQL 문을 μž‘μ„±ν•˜μ„Έμš”.

UPDATE
        TB_STUDENT
    SET STUDENT_ADDRESS = 'μ„œμšΈμ‹œ μ’…λ‘œκ΅¬ μˆ­μΈλ™ 181-21'
    WHERE STUDENT_NO = 'A413042'
    AND STUDENT_NAME = 'λ°•κ±΄μš°';

Q6

πŸ’β€ μ£Όλ―Όλ“±λ‘λ²ˆν˜Έ λ³΄ν˜Έλ²•μ— 따라 학생정보 ν…Œμ΄λΈ”μ—μ„œ 주민번호 λ’·μžλ¦¬λ₯Ό μ €μž₯ν•˜μ§€ μ•ŠκΈ°λ‘œ κ²°μ •ν–ˆμŠ΅λ‹ˆλ‹€. 이 λ‚΄μš©μ„ λ°˜μ˜ν•  μ μ ˆν•œ SQL λ¬Έμž₯을 μž‘μ„±ν•˜μ„Έμš”. (예. 830530-2124663 ==> 830530 )

UPDATE
        TB_STUDENT
    SET STUDENT_SSN = SUBSTR(STUDENT_SSN, 1, 6);

Q7

πŸ’β€ μ˜ν•™κ³Ό κΉ€λͺ…ν›ˆ 학생은 2005 λ…„ 1 학기에 μžμ‹ μ΄ μˆ˜κ°•ν•œ '피뢀생리학' μ μˆ˜κ°€ 잘λͺ»λ˜μ—ˆλ‹€λŠ” 것을 λ°œκ²¬ν•˜κ³ λŠ” 정정을 μš”μ²­ν–ˆμŠ΅λ‹ˆλ‹€. λ‹΄λ‹Ή ꡐ수의 확인 받은 κ²°κ³Ό ν•΄λ‹Ή κ³Όλͺ©μ˜ 학점을 3.5 둜 λ³€κ²½ν‚€λ‘œ κ²°μ •λ˜μ—ˆμŠ΅λ‹ˆλ‹€. μ μ ˆν•œ SQL 문을 μž‘μ„±ν•˜μ„Έμš”.


UPDATE
        TB_GRADE
    SET POINT = 3.5
    WHERE TERM_NO = '200501'
    AND (STUDENT_NO, CLASS_NO) = (SELECT
                            STUDENT_NO
                        ,   CLASS_NO
                        FROM TB_GRADE
                        JOIN TB_STUDENT S USING (STUDENT_NO)
                        JOIN TB_CLASS USING (CLASS_NO)
                        JOIN TB_DEPARTMENT D ON (S.DEPARTMENT_NO = D.DEPARTMENT_NO)
                        WHERE STUDENT_NAME = 'κΉ€λͺ…ν›ˆ'
                        AND CLASS_NAME = '피뢀생리학'
                        AND DEPARTMENT_NAME = 'μ˜ν•™κ³Ό');

πŸ’¬ Overall Comment

* DMLλ¬Έ μ•ˆμ—μ„œ JOIN을 μ‚¬μš©ν•˜μ§€ λͺ» ν•œλ‹€λŠ” 것을 μ•Œκ²Œν•΄μ€€ λ¬Έν•­μ΄μ—ˆλ‹€. 
  이럴 λ•ŒλŠ” μ„œλΈŒμΏΌλ¦¬ μ•ˆμ—μ„œ μ‚¬μš©ν•˜λŠ” λ°©ν–₯으둜 κ°€μ•Όκ² λ‹€.

Q8

πŸ’β€ 성적 ν…Œμ΄λΈ”(TB_GRADE)μ—μ„œ νœ΄ν•™μƒλ“€μ˜ 성적항λͺ©μ„ μ œκ±°ν•˜μ„Έμš”.

COMMIT;
DELETE 
    FROM TB_GRADE
    WHERE STUDENT_NO IN (SELECT
                                STUDENT_NO
                            FROM TB_GRADE
                            JOIN TB_STUDENT USING (STUDENT_NO)
                            WHERE ABSENCE_YN = 'Y');

πŸ’¬ Overall Comment

* WHEREμ ˆμ—μ„œ 닀쀑행 μ„œλΈŒμΏΌλ¦¬λ₯Ό μ‚¬μš©ν•  λ•Œ '='이 μ•„λ‹Œ 'IN'을 μ‚¬μš©ν•΄μ•Όν•œλ‹€λŠ” 것을 μƒκΈ°μ‹œμΌœμ€€ 
  λ¬Έν•­μ΄μ—ˆλ‹€. μƒμ‹μ μœΌλ‘œ 행이 μ—¬λŸ¬ 개이면 '=(κ°™λ‹€)'λ₯Ό μ‚¬μš©ν•˜λ©΄ μ μ ˆν•˜μ§€ μ•ŠμœΌλ‹ˆ 잘 κΈ°μ–΅ν•΄λ‘μž!

profile
Tiny little habits make me
post-custom-banner

0개의 λŒ“κΈ€