[Oracle] Solution to puzzles

Joy๐ŸŒฑยท2023๋…„ 1์›” 16์ผ
0

๐Ÿงฉ Coding Challenges

๋ชฉ๋ก ๋ณด๊ธฐ
13/20
post-thumbnail
post-custom-banner

๐Ÿงฉ ํ•จ์ˆ˜, JOIN, ๊ทธ๋ฃนํ™”๋ฅผ ํ™œ์šฉํ•˜์—ฌ SQL ์ฟผ๋ฆฌ์งœ๊ธฐ

Q1

๐Ÿ’โ€ ํ•™์ƒ์ด๋ฆ„๊ณผ ์ฃผ์†Œ์ง€๋ฅผ ํ‘œ์‹œํ•˜์„ธ์š”. ๋‹จ, ์ถœ๋ ฅ ํ—ค๋”๋Š” "ํ•™์ƒ ์ด๋ฆ„", "์ฃผ์†Œ์ง€"๋กœ ํ•˜๊ณ , ์ •๋ ฌ์€ ์ด๋ฆ„์œผ๋กœ ์˜ค๋ฆ„์ฐจ์ˆœ ํ‘œ์‹œํ•˜๋„๋ก ํ•ฉ๋‹ˆ๋‹ค.

๐Ÿšฉ Example Output 588 rows selected

ํ•™์ƒ ์ด๋ฆ„์ฃผ์†Œ์ง€
๊ฐํ˜„์ œ์„œ์šธ๊ฐ•์„œ๋“ฑ์ดŒ๋™691-3๋ถ€์˜@102-505
๊ฐ•๋™์—ฐ๊ฒฝ๊ธฐ๋„ ์˜์ •๋ถ€์‹œ ๋ฏผ๋ฝ๋™ 694 ์‚ฐ๋“ค๋งˆ์„ ๋Œ€๋ฆผ์•„ํŒŒํŠธ 404-1404
. . . . . .. . . . . .
ํ™ฉํ˜•์ฒ ์ „๋‚จ ์ˆœ์ฒœ์‹œ ์ƒ๋ชฉ๋™ ํ˜„๋Œ€โ“ 106/407 T.061-772-2101
ํ™ฉํšจ์ข…์ธ์ฒœ์‹œ์„œ๊ตฌ ์„๋‚จ๋™ 564-4๋ฒˆ์ง€
SELECT
        STUDENT_NAME "ํ•™์ƒ ์ด๋ฆ„"
    ,   STUDENT_ADDRESS ์ฃผ์†Œ์ง€
    FROM TB_STUDENT
    ORDER BY 1;

Q2

๐Ÿ’โ€ ํœดํ•™์ค‘์ธ ํ•™์ƒ๋“ค์˜ ์ด๋ฆ„๊ณผ ์ฃผ๋ฏผ๋ฒˆํ˜ธ๋ฅผ ๋‚˜์ด๊ฐ€ ์ ์€ ์ˆœ์„œ๋กœ ํ™”๋ฉด์— ์ถœ๋ ฅํ•˜์„ธ์š”.

๐Ÿšฉ Example Output 91 rows selected

STUDENT_NAMESTUDENT_SSN
๋ฆดํฌ๊ถŒ871222
ํ™ฉํšจ์ข…871125
. . . . . .. . . . . .
์ตœ์ •ํฌ791215
์กฐ๊ธฐํ™˜791002
SELECT
		STUDENT_NAME
    ,   STUDENT_SSN
    FROM TB_STUDENT
    WHERE ABSENCE_YN = 'Y'
    ORDER BY MONTHS_BETWEEN(SYSDATE, TO_DATE(SUBSTR(STUDENT_SSN, 1, 6))) / 12;

Q3

๐Ÿ’โ€ ์ฃผ์†Œ์ง€๊ฐ€ ๊ฐ•์›๋„๋‚˜ ๊ฒฝ๊ธฐ๋„์ธ ํ•™์ƒ๋“ค ์ค‘ 1900 ๋…„๋Œ€ ํ•™๋ฒˆ์„ ๊ฐ€์ง„ ํ•™์ƒ๋“ค์˜ ์ด๋ฆ„๊ณผ ํ•™๋ฒˆ, ์ฃผ์†Œ๋ฅผ ์ด๋ฆ„์˜ ์˜ค๋ฆ„์ฐจ์ˆœ์œผ๋กœ ํ™”๋ฉด์— ์ถœ๋ ฅํ•˜์„ธ์š”. ๋‹จ, ์ถœ๋ ฅํ—ค๋”์—๋Š” "ํ•™์ƒ์ด๋ฆ„","ํ•™๋ฒˆ", "๊ฑฐ์ฃผ์ง€ ์ฃผ์†Œ" ๊ฐ€ ์ถœ๋ ฅ๋˜๋„๋ก ํ•ฉ๋‹ˆ๋‹ค.

๐Ÿšฉ Example Output 9 rows selected

ํ•™์ƒ์ด๋ฆ„ํ•™๋ฒˆ๊ฑฐ์ฃผ์ง€ ์ฃผ์†Œ
๊น€๊ณ„์˜9919024๊ฒฝ๊ธฐ๋„ ์šฉ์ธ์‹œ ์ˆ˜์ง€๊ตฌ ํ’๋•์ฒœ2๋™ ์‹ ์ •๋งˆ์„ ์ž„๊ด‘ 305-1703ํ˜ธ
๋ฐ•๊ทœ์ƒ9931111๊ฒฝ๊ธฐ๋„ ์„ฑ๋‚จ์‹œ ๋ถ„๋‹น๊ตฌ ํƒ‘๋งˆ์„ 526 ๊ฒฝ๋‚จ์•„ํŒŒํŠธ 710-1302
. . . . . .. . . . . .. . . . . .
์กฐ๊ธฐํ™˜9931312๊ฒฝ๊ธฐ๋„ ์‹œํฅ์‹œ ๋งคํ™”๋™ 194-1 ๋™์ง„์•„ํŒŒํŠธ 1-305
SELECT
        STUDENT_NAME ํ•™์ƒ์ด๋ฆ„
    ,   STUDENT_NO ํ•™๋ฒˆ
    ,   STUDENT_ADDRESS "๊ฑฐ์ฃผ์ง€ ์ฃผ์†Œ"
    FROM TB_STUDENT
    WHERE STUDENT_NO NOT LIKE 'A%'
    AND STUDENT_ADDRESS LIKE '%๊ฒฝ๊ธฐ๋„%' 
    OR STUDENT_ADDRESS LIKE '%๊ฐ•์›๋„%'
    ORDER BY 1;

Q4

๐Ÿ’โ€ ํ˜„์žฌ ๋ฒ•ํ•™๊ณผ ๊ต์ˆ˜ ์ค‘ ๊ฐ€์žฅ ๋‚˜์ด๊ฐ€ ๋งŽ์€ ์‚ฌ๋žŒ๋ถ€ํ„ฐ ์ด๋ฆ„์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋Š” SQL ๋ฌธ์žฅ์„ ์ž‘์„ฑํ•˜์„ธ์š”. (๋ฒ•ํ•™๊ณผ์˜ 'ํ•™๊ณผ์ฝ”๋“œ'๋Š” ํ•™๊ณผ ํ…Œ์ด๋ธ”(TB_DEPARTMENT)์„ ์กฐํšŒํ•ด์„œ ์ฐพ๊ธฐ)

๐Ÿšฉ Example Output 4 rows selected

PROFESSOR_NAMEPROFESSOR_SSN
ํ™๋‚จ์ˆ˜540304-1112251
๊น€์„ ํฌ551030-2159000
์ž„์ง„์ˆ™640125-1143548
์ด๋ฏธ๊ฒฝ741016-2103506
SELECT
        PROFESSOR_NAME
    ,   PROFESSOR_SSN
    FROM TB_PROFESSOR
    WHERE DEPARTMENT_NO = '005'
    ORDER BY MONTHS_BETWEEN(SYSDATE, TO_DATE(SUBSTR(PROFESSOR_SSN, 1, 6))) / 12 DESC;

๐Ÿ’ก Another Solution

SELECT 
       PROFESSOR_NAME
     , PROFESSOR_SSN
  FROM TB_PROFESSOR
  JOIN TB_DEPARTMENT USING(DEPARTMENT_NO)
 WHERE DEPARTMENT_NAME = '๋ฒ•ํ•™๊ณผ'
 ORDER BY 2;

Q5

๐Ÿ’โ€ 2004 ๋…„ 2 ํ•™๊ธฐ์— 'C3118100' ๊ณผ๋ชฉ์„ ์ˆ˜๊ฐ•ํ•œ ํ•™์ƒ๋“ค์˜ ํ•™์ ์„ ์กฐํšŒํ•˜๋ ค๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ํ•™์ ์ด ๋†’์€ ํ•™์ƒ๋ถ€ํ„ฐ ํ‘œ์‹œํ•˜๊ณ , ํ•™์ ์ด ๊ฐ™์œผ๋ฉด ํ•™๋ฒˆ์ด ๋‚ฎ์€ ํ•™์ƒ๋ถ€ํ„ฐ ํ‘œ์‹œํ•˜๋Š” ๊ตฌ๋ฌธ์„ ์ž‘์„ฑํ•ด๋ณด์„ธ์š”.

๐Ÿšฉ Example Output 7 rows selected

STUDENT_NOPOINT
A3520174.00
A1152703.50
. . . . . .. . . . . .
A3310762.00
SELECT
        STUDENT_NO
    ,   TO_CHAR(POINT, 99.99) POINT
    FROM TB_GRADE
    WHERE TERM_NO = '200402'
    AND CLASS_NO = 'C3118100'
    ORDER BY 2 DESC, 1 DESC;

๐Ÿ’ก Another Solution

SELECT 
       STUDENT_NO
     , TO_CHAR(POINT,'9.00') POINT
  FROM TB_GRADE
 WHERE TERM_NO = '200402'
   AND CLASS_NO = 'C3118100'
 ORDER BY POINT DESC, STUDENT_NO;

Q6

๐Ÿ’โ€ ํ•™์ƒ ๋ฒˆํ˜ธ, ํ•™์ƒ ์ด๋ฆ„, ํ•™๊ณผ ์ด๋ฆ„์„ ํ•™์ƒ ์ด๋ฆ„์œผ๋กœ ์˜ค๋ฆ„์ฐจ์ˆœ ์ •๋ ฌํ•˜์—ฌ ์ถœ๋ ฅํ•˜๋Š” SQL๋ฌธ์„ ์ž‘์„ฑํ•˜์„ธ์š”.

๐Ÿšฉ Example Output 588 rows selected

STUDENT_NOSTUDENT_NAMEDEPARTMENT_NAME
A411001๊ฐํ˜„์ œ์น˜์˜ํ•™๊ณผ
A131004๊ฐ•๋™์—ฐ๋””์ž์ธํ•™๊ณผ
. . . . . .. . . . . .
A411335ํ™ฉํ˜•์ฒ ์‚ฌํšŒํ•™๊ณผ
A511332ํ™ฉํšจ์ข…์ปดํ“จํ„ฐ๊ณตํ•™๊ณผ
SELECT
        STUDENT_NO
    ,   STUDENT_NAME
    ,   DEPARTMENT_NAME
    FROM TB_STUDENT
    JOIN TB_DEPARTMENT USING(DEPARTMENT_NO)
    ORDER BY 2;

Q7

๐Ÿ’โ€ ์ถ˜ ๊ธฐ์ˆ ๋Œ€ํ•™๊ต์˜ ๊ณผ๋ชฉ ์ด๋ฆ„๊ณผ ๊ณผ๋ชฉ์˜ ํ•™๊ณผ ์ด๋ฆ„์„ ์ถœ๋ ฅํ•˜๋Š” SQL ๋ฌธ์žฅ์„ ์ž‘์„ฑํ•˜์„ธ์š”.

๐Ÿšฉ Example Output 882 rows selected

CLASS_NAMEDEPARTMENT_NAME
๊ณ ์ „์‹œ๊ฐ€๋ก ํŠน๊ฐ•๊ตญ์–ด๊ตญ๋ฌธํ•™๊ณผ
๊ตญ์–ด์–ดํœ˜๋ก ํŠน๊ฐ•๊ตญ์–ด๊ตญ๋ฌธํ•™๊ณผ
. . . . . .. . . . . .
๋…ผ๋ฌธ์ง€๋„(๊ณต๊ฐœ๋ฐœํ‘œ)3์ฒด์œกํ•™๊ณผ
์ฒด์œก์ƒ๋ฆฌํ•™์—ฐ๊ตฌ์ฒด์œกํ•™๊ณผ
SELECT
        CLASS_NAME
    ,   DEPARTMENT_NAME
    FROM TB_CLASS
    JOIN TB_DEPARTMENT USING (DEPARTMENT_NO);

Q8

๐Ÿ’โ€ ๊ณผ๋ชฉ๋ณ„ ๊ต์ˆ˜ ์ด๋ฆ„์„ ์ฐพ์œผ๋ ค๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ๊ณผ๋ชฉ ์ด๋ฆ„๊ณผ ๊ต์ˆ˜ ์ด๋ฆ„์„ ์ถœ๋ ฅํ•˜๋Š” SQL๋ฌธ์„ ์ž‘์„ฑํ•˜์„ธ์š”.

๐Ÿšฉ Example Output 776 rows selected

CLASS_NAMEPROFESSOR_NAME
๋„์‹œ์ง€๋ฆฌํ•™๋ฐ•ํƒœํ™˜
๋„์‹œ์ง€๋ฆฌํ•™๊น€์„ ํ•„
. . . . . .. . . . . .
๋…ผ๋ฌธ์ง€๋„1๋ฐฑ์€์ •
๋…ผ๋ฌธ์ง€๋„2์œ ์šฉ์„
SELECT
        CLASS_NAME
    ,   PROFESSOR_NAME
    FROM TB_CLASS
    JOIN TB_CLASS_PROFESSOR USING(CLASS_NO)
    JOIN TB_PROFESSOR USING(PROFESSOR_NO);

Q9

๐Ÿ’โ€ 8 ๋ฒˆ์˜ ๊ฒฐ๊ณผ ์ค‘ โ€˜์ธ๋ฌธ์‚ฌํšŒโ€™ ๊ณ„์—ด์— ์†ํ•œ ๊ณผ๋ชฉ์˜ ๊ต์ˆ˜ ์ด๋ฆ„์„ ์ฐพ์œผ๋ ค๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ์ด์— ํ•ด๋‹นํ•˜๋Š” ๊ณผ๋ชฉ ์ด๋ฆ„๊ณผ ๊ต์ˆ˜ ์ด๋ฆ„์„ ์ถœ๋ ฅํ•˜๋Š” SQL ๋ฌธ์„ ์ž‘์„ฑํ•˜์„ธ์š”.

๐Ÿšฉ Example Output 197 rows selected

CLASS_NAMEPROFESSOR_NAME
์ธ์‚ฌ์กฐ์งํ–‰๋™ ํŠน๋ณ„์—ฐ๊ตฌ๊ฐ•ํ˜
๋…ผ๋ฌธ์ง€๋„1๊ฐ•ํ˜
. . . . . .. . . . . .
์‹ ๊ธฐ์ˆ  ์„ธ๋ฏธ๋‚˜ํ™ฉํ—Œ์ค‘
์ง€์‹๊ฒฝ์˜๊ณผ ๋น„์ง€๋‹ˆ์Šค ์ธํ…”๋ฆฌ์ „์Šคํ™ฉํ—Œ์ค‘
SELECT
        CLASS_NAME
    ,   PROFESSOR_NAME
    FROM TB_CLASS C
    JOIN TB_CLASS_PROFESSOR USING(CLASS_NO)
    JOIN TB_PROFESSOR USING(PROFESSOR_NO)
    JOIN TB_DEPARTMENT D ON(C.DEPARTMENT_NO = D.DEPARTMENT_NO)
    WHERE D.CATEGORY = '์ธ๋ฌธ์‚ฌํšŒ'
    ORDER BY 2;

Q10

๐Ÿ’โ€ โ€˜์Œ์•…ํ•™๊ณผโ€™ ํ•™์ƒ๋“ค์˜ ํ‰์ ์„ ๊ตฌํ•˜๋ ค๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ์Œ์•…ํ•™๊ณผ ํ•™์ƒ๋“ค์˜ "ํ•™๋ฒˆ", "ํ•™์ƒ ์ด๋ฆ„", "์ „์ฒด ํ‰์ "์„ ์ถœ๋ ฅํ•˜๋Š” SQL ๋ฌธ์žฅ์„ ์ž‘์„ฑํ•˜์„ธ์š”. (๋‹จ, ํ‰์ ์€ ์†Œ์ˆ˜์  1 ์ž๋ฆฌ๊นŒ์ง€๋งŒ ๋ฐ˜์˜ฌ๋ฆผํ•˜์—ฌ ํ‘œ์‹œ)

๐Ÿšฉ Example Output 7 rows selected

ํ•™๋ฒˆํ•™์ƒ ์ด๋ฆ„์ „์ฒด ํ‰์ 
9931310์กฐ๊ธฐํ˜„4.1
A411116๋ฐ•ํ˜„ํ™”3.6
. . . . . .. . . . . .. . . . . .
A612052์‹ ๊ด‘ํ˜„4.1
SELECT
        STUDENT_NO ํ•™๋ฒˆ
    ,   STUDENT_NAME "ํ•™์ƒ ์ด๋ฆ„"
    ,   ROUND(AVG(POINT), 1) "์ „์ฒด ํ‰์ "
    FROM TB_STUDENT
    JOIN TB_GRADE USING(STUDENT_NO)
    JOIN TB_DEPARTMENT USING(DEPARTMENT_NO)
    WHERE DEPARTMENT_NAME = '์Œ์•…ํ•™๊ณผ'
    GROUP BY STUDENT_NO, STUDENT_NAME
    ORDER BY 1;

Q11

๐Ÿ’โ€ ํ•™๋ฒˆ์ด A313047 ์ธ ํ•™์ƒ์ด ํ•™๊ต์— ๋‚˜์˜ค๊ณ  ์žˆ์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ์ง€๋„ ๊ต์ˆ˜์—๊ฒŒ ๋‚ด์šฉ์„ ์ „๋‹ฌํ•˜๊ธฐ ์œ„ํ•œ ํ•™๊ณผ ์ด๋ฆ„, ํ•™์ƒ ์ด๋ฆ„๊ณผ ์ง€๋„ ๊ต์ˆ˜ ์ด๋ฆ„์ด ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค. ์ด๋•Œ ์‚ฌ์šฉํ•œ SQL ๋ฌธ์„ ์ž‘์„ฑํ•˜์„ธ์š”. ๋‹จ, ์ถœ๋ ฅํ—ค๋”๋Š” โ€šํ•™๊ณผ์ด๋ฆ„โ€›, โ€šํ•™์ƒ์ด๋ฆ„โ€›, โ€š์ง€๋„๊ต์ˆ˜์ด๋ฆ„โ€›์œผ๋กœ ์ถœ๋ ฅ๋˜๋„๋ก ํ•ฉ๋‹ˆ๋‹ค.

๐Ÿšฉ Example Output 1 row selected

ํ•™๊ณผ์ด๋ฆ„ํ•™์ƒ์ด๋ฆ„์ง€๋„๊ต์ˆ˜์ด๋ฆ„
๊ฒฝ์ œํ•™๊ณผ์†๊ฑด์˜๋ฐ•ํƒœํ™˜
SELECT
        DEPARTMENT_NAME ํ•™๊ณผ์ด๋ฆ„
    ,   STUDENT_NAME ํ•™์ƒ์ด๋ฆ„
    ,   PROFESSOR_NAME ์ง€๋„๊ต์ˆ˜์ด๋ฆ„
    FROM TB_DEPARTMENT
    JOIN TB_STUDENT USING(DEPARTMENT_NO)
    JOIN TB_PROFESSOR ON(COACH_PROFESSOR_NO = PROFESSOR_NO)
    WHERE STUDENT_NO = 'A313047';

Q12

๐Ÿ’โ€ 2007 ๋…„๋„์— '์ธ๊ฐ„๊ด€๊ณ„๋ก ' ๊ณผ๋ชฉ์„ ์ˆ˜๊ฐ•ํ•œ ํ•™์ƒ์„ ์ฐพ์•„ ํ•™์ƒ์ด๋ฆ„๊ณผ ์ˆ˜๊ฐ•ํ•™๊ธฐ๋ฅผ ํ‘œ์‹œํ•˜๋Š” SQL ๋ฌธ์žฅ์„ ์ž‘์„ฑํ•˜์„ธ์š”.

๐Ÿšฉ Example Output 2 rows selected

ํ•™์ƒ์ด๋ฆ„์ˆ˜๊ฐ•ํ•™๊ธฐ
์˜ค์ง„ํ˜•200701
์ด์ •ํ˜ธ200703
SELECT
        STUDENT_NAME ํ•™์ƒ์ด๋ฆ„
    ,   TERM_NO ์ˆ˜๊ฐ•ํ•™๊ธฐ
    FROM TB_STUDENT
    JOIN TB_GRADE USING(STUDENT_NO)
    JOIN TB_CLASS USING(CLASS_NO) 
    WHERE TERM_NO LIKE '2007%'
    AND CLASS_NAME = '์ธ๊ฐ„๊ด€๊ณ„๋ก '
    ORDER BY 1;   

๐Ÿ’ฌ Overall Comment

* ์ฒ˜์Œ์— TB_CLASS๋ฅผ JOINํ•˜๋ ค๊ณ  ํ–ˆ์„ ๋•Œ, TB_STUDENT์˜ DEPARTMENT_NO๋กœ JOIN์„ ํ–ˆ๋‹ค๊ฐ€ 
  ๊ฒฐ๊ณผ๊ฐ’์ด ์˜ฌ๋ฐ”๋ฅด๊ฒŒ ๋‚˜์˜ค์ง€ ์•Š์•˜๋‹ค. ๊ทธ๋ž˜์„œ TB_GRADE์˜ CLASS_NO๋กœ JOIN ๋ฐฉ์‹์„ ๋ฐ”๊พผ ๊ฒฐ๊ณผ, 
  ์˜ฌ๋ฐ”๋ฅธ ์ถœ๋ ฅ๋ฌธ์ด ๋‚˜์™”๋‹ค. JOIN์„ ํ•  ๋•Œ, ๋ง‰๋ฌด๊ฐ€๋‚ด๋กœ ๊ณตํ†ต๋˜๋Š” ์ปฌ๋Ÿผ๋ผ๋ฆฌ JOIN์„ ํ•˜๋ฉด ์•ˆ๋œ๋‹ค๋Š” 
  ๊ตํ›ˆ์„ ์–ป๊ฒŒ ๋œ ๋ฌธํ•ญ์ด์—ˆ๋‹ค. ํ•ญ์ƒ ๋ฌด์—‡์„ ์ถœ๋ ฅํ•ด์•ผํ•˜๋Š”์ง€ ์ƒ๊ฐ์„ ํ•˜๊ณ  JOIN์„ ํ•ด์•ผํ•œ๋‹ค.

Q13

๐Ÿ’โ€ ์˜ˆ์ฒด๋Šฅ ๊ณ„์—ด ๊ณผ๋ชฉ ์ค‘ ๊ณผ๋ชฉ ๋‹ด๋‹น๊ต์ˆ˜๋ฅผ ํ•œ ๋ช…๋„ ๋ฐฐ์ •๋ฐ›์ง€ ๋ชปํ•œ ๊ณผ๋ชฉ์„ ์ฐพ์•„ ๊ทธ ๊ณผ๋ชฉ ์ด๋ฆ„๊ณผ ํ•™๊ณผ ์ด๋ฆ„์„ ์ถœ๋ ฅํ•˜๋Š” SQL ๋ฌธ์žฅ์„ ์ž‘์„ฑํ•˜์„ธ์š”.

๐Ÿšฉ Example Output 44 rows selected

CLASS_NAMEDEPARTMENT_NAME
๋ฌด์šฉ๊ธฐ๋Šฅํ•™๊ณต์—ฐ์˜ˆ์ˆ ํ•™๊ณผ
๋…ผ๋ฌธ์ง€๋„1๊ณต์—ฐ์˜ˆ์ˆ ํ•™๊ณผ
. . . . . .. . . . . .
์Šคํฌ์ธ ์‚ฐ์—…์—ฐ๊ตฌ ์ฒด์œกํ•™๊ณผ
ํ•ด๋ถ€ํ•™์‹คํ—˜์ฒด์œกํ•™๊ณผ
SELECT
        CLASS_NAME
    ,   DEPARTMENT_NAME
    FROM TB_CLASS 
    JOIN TB_DEPARTMENT USING(DEPARTMENT_NO)
    LEFT JOIN TB_CLASS_PROFESSOR USING(CLASS_NO)
    WHERE CATEGORY = '์˜ˆ์ฒด๋Šฅ'
    AND PROFESSOR_NO IS NULL
    ORDER BY 2;

Q14

๐Ÿ’โ€ ์ถ˜ ๊ธฐ์ˆ ๋Œ€ํ•™๊ต ์„œ๋ฐ˜์•„์–ดํ•™๊ณผ ํ•™์ƒ๋“ค์˜ ์ง€๋„๊ต์ˆ˜๋ฅผ ๊ฒŒ์‹œํ•˜๊ณ ์ž ํ•ฉ๋‹ˆ๋‹ค. ํ•™์ƒ์ด๋ฆ„๊ณผ ์ง€๋„๊ต์ˆ˜ ์ด๋ฆ„์„ ์ฐพ๊ณ  ๋งŒ์ผ ์ง€๋„ ๊ต์ˆ˜๊ฐ€ ์—†๋Š” ํ•™์ƒ์ผ ๊ฒฝ์šฐ "์ง€๋„๊ต์ˆ˜ ๋ฏธ์ง€์ •โ€›์œผ๋กœ ํ‘œ์‹œํ•˜๋„๋ก ํ•˜๋Š” SQL ๋ฌธ์„ ์ž‘์„ฑํ•˜์„ธ์š”. ๋‹จ, ์ถœ๋ ฅํ—ค๋”๋Š” โ€šํ•™์ƒ์ด๋ฆ„โ€›, โ€š์ง€๋„๊ต์ˆ˜โ€›๋กœ ํ‘œ์‹œํ•˜๋ฉฐ ๊ณ ํ•™๋ฒˆ ํ•™์ƒ์ด ๋จผ์ € ํ‘œ์‹œ๋˜๋„๋ก ํ•ฉ๋‹ˆ๋‹ค.

๐Ÿšฉ Example Output 14 rows selected

ํ•™์ƒ์ด๋ฆ„์ง€๋„๊ต์ˆ˜
์ฃผํ•˜๋‚˜ํ—ˆ๋ฌธํ‘œ
์ดํฌ์ง„๋‚จ๋ช…๊ธธ
. . . . . .. . . . . .
๊น€์ •ํ˜„๋ฐ•์ค€ํ˜ธ
์ตœ์ฒ ํ˜„๋ฐฑ์–‘์ž„
SELECT
        STUDENT_NAME ํ•™์ƒ์ด๋ฆ„
    ,   NVL(PROFESSOR_NAME, '์ง€๋„๊ต์ˆ˜ ๋ฏธ์ง€์ •') ์ง€๋„๊ต์ˆ˜
    FROM TB_STUDENT S
    LEFT JOIN TB_PROFESSOR ON(COACH_PROFESSOR_NO = PROFESSOR_NO)
    JOIN TB_DEPARTMENT D ON(S.DEPARTMENT_NO = D.DEPARTMENT_NO)
    WHERE DEPARTMENT_NAME = '์„œ๋ฐ˜์•„์–ดํ•™๊ณผ'
    ORDER BY S.ENTRANCE_DATE;

Q15

๐Ÿ’โ€ ํœดํ•™์ƒ์ด ์•„๋‹Œ ํ•™์ƒ ์ค‘ ํ‰์ ์ด 4.0 ์ด์ƒ์ธ ํ•™์ƒ์„ ์ฐพ์•„ ๊ทธ ํ•™์ƒ์˜ ํ•™๋ฒˆ, ์ด๋ฆ„, ํ•™๊ณผ ์ด๋ฆ„, ํ‰์ ์„ ์ถœ๋ ฅํ•˜๋Š” SQL ๋ฌธ์„ ์ž‘์„ฑํ•˜์„ธ์š”.

๐Ÿšฉ Example Output 19 rows selected

ํ•™๋ฒˆ์ด๋ฆ„ํ•™๊ณผ ์ด๋ฆ„ํ‰์ 
9811251๊น€์ถฉ์›๊ฑด์ถ•๊ณตํ•™๊ณผ4.11111111
9817035๊น€์†Œ๋ผํ† ๋ชฉ๊ณตํ•™๊ณผ4
. . . . . .. . . . . .. . . . . .. . . . . .
A517069๋ฐ•์Šนํ™˜์ „ํŒŒ๊ณตํ•™๊ณผ4
A612052์‹ ๊ด‘ํ˜„์Œ์•…ํ•™๊ณผ4.1
SELECT
        STUDENT_NO ํ•™๋ฒˆ
    ,   STUDENT_NAME ์ด๋ฆ„
    ,   DEPARTMENT_NAME "ํ•™๊ณผ ์ด๋ฆ„"
    ,   ROUND(AVG(POINT), 8) ํ‰์ 
    FROM TB_STUDENT
    JOIN TB_DEPARTMENT USING(DEPARTMENT_NO)
    JOIN TB_GRADE USING(STUDENT_NO)
    WHERE ABSENCE_YN = 'N'
    GROUP BY STUDENT_NO, STUDENT_NAME, DEPARTMENT_NAME
    HAVING AVG(POINT) >= 4.0
    ORDER BY 1;

๐Ÿ’ฌ Overall Comment

* GROUP BY์™€ HAVING์„ ์ ์ ˆํ•œ ๋•Œ์— ์‚ฌ์šฉํ•˜๋Š” ๋ฒ•์ด ๊ธด๊ฐ€๋ฏผ๊ฐ€ ํ–ˆ์—ˆ๋Š”๋ฐ, ์ด ๋ฌธํ•ญ์„ ํ’€๋ฉด์„œ 
  ์ œ๋Œ€๋กœ ์ดํ•ดํ•˜๊ณ  ์ ์šฉ์‹œํ‚ฌ ์ˆ˜ ์žˆ๊ฒŒ ๋˜์—ˆ๋‹ค. GROUP BY๋ฅผ ์‚ฌ์šฉํ•  ์‹œ, ์กฐํšŒํ•  ์ปฌ๋Ÿผ ์ค‘ 
  ๊ทธ๋ฃนํ•จ์ˆ˜๋ฅผ ์ œ์™ธํ•œ ๋‚˜๋จธ์ง€๋“ค์„ ๋ชจ๋‘ GROUP BY์ ˆ์— ๋‚˜์—ดํ•ด์ค˜์•ผํ•œ๋‹ค. ๊ทธ๋ ‡์ง€ ์•Š์œผ๋ฉด 
  'GROUP BY ํ‘œํ˜„์‹์ด ์•„๋‹™๋‹ˆ๋‹ค.'๋ผ๋Š” ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ•œ๋‹ค. ๊ทธ๋ฆฌ๊ณ  HAVING์ ˆ์€ ๊ทธ๋ฃน์— ๋Œ€ํ•œ 
  ์กฐ๊ฑด์ด๋ฏ€๋กœ WHERE์ ˆ์— ์ž…๋ ฅํ•ด์•ผํ•  ์กฐ๊ฑด๊ณผ ํ˜ผ๋™ํ•˜์ง€ ์•Š์•„์•ผํ•œ๋‹ค.

Q16

๐Ÿ’โ€ ํ™˜๊ฒฝ์กฐ๊ฒฝํ•™๊ณผ ์ „๊ณต๊ณผ๋ชฉ๋“ค์˜ ๊ณผ๋ชฉ ๋ณ„ ํ‰์ ์„ ํŒŒ์•…ํ•  ์ˆ˜ ์žˆ๋Š” SQL ๋ฌธ์„ ์ž‘์„ฑํ•˜์„ธ์š”.

๐Ÿšฉ Example Output 6 rows selected

CLASS_NOCLASS_NAMEAVG(POINT)
C3016200์ „ํ†ต๊ณ„์Šน๋ฐฉ๋ฒ•๋ก 3.91666666
C3081300์กฐ๊ฒฝ๊ณ„ํš๋ฐฉ๋ฒ•๋ก 3.55
. . . . . .. . . . . .. . . . . .
C4477600์กฐ๊ฒฝ์‹œํ•™3.36666666
C5009300๋‹จ์ง€๊ณ„ํš๋ฐ์„ค๊ณ„์ŠคํŠœ๋””์˜ค3.42857142
SELECT
        CLASS_NO
    ,   CLASS_NAME
    ,   TRUNC(AVG(POINT), 8) "AVG(POINT)"
    FROM TB_CLASS
    JOIN TB_GRADE USING(CLASS_NO)
    JOIN TB_DEPARTMENT USING(DEPARTMENT_NO)
    WHERE DEPARTMENT_NAME = 'ํ™˜๊ฒฝ์กฐ๊ฒฝํ•™๊ณผ'
    AND CLASS_TYPE LIKE '์ „๊ณต%' 
    GROUP BY CLASS_NO, CLASS_NAME
    ORDER BY 1;

Q17

๐Ÿ’โ€ ์ถ˜ ๊ธฐ์ˆ ๋Œ€ํ•™๊ต์— ๋‹ค๋‹ˆ๊ณ  ์žˆ๋Š” ์ตœ๊ฒฝํฌ ํ•™์ƒ๊ณผ ๊ฐ™์€ ๊ณผ ํ•™์ƒ๋“ค์˜ ์ด๋ฆ„๊ณผ ์ฃผ์†Œ๋ฅผ ์ถœ๋ ฅํ•˜๋Š” SQL ๋ฌธ์„ ์ž‘์„ฑํ•˜์„ธ์š”. (์„ ์ƒ๋‹˜ ์ฝ”๋“œ)

๐Ÿšฉ Example Output 17 rows selected

STUDENT_NAMESTUDENT_ADDRESS
๊น€์„๋ฏผ๊ฒฝ๊ธฐ๋„์•ˆ์‚ฐ์‹œ์ƒ๋ก๊ตฌ2๋™664๋ฒˆ์ง€ํˆฌ๋ฃจ์ง€์˜ค2์ฐจ@205/601
์–‘์„ ์ฃผ๊ฒฝ๊ธฐ๋„์•ˆ์–‘์‹œ๋งŒ์•ˆ๊ตฌ๋ฐ•๋‹ฌ๋™107๋ฒฝ์‚ฐ@101-1801
. . . . . .. . . . . .
๊ธฐํ˜œ๋ฏธ๋Œ€์ „์‹œ ์œ ์„ฑ๊ตฌ ๋•์ง„๋™ ํ•œ๊ตญ์›์ž๋ ฅ์•ˆ์ „๊ธฐ์ˆ ์› ํ–‰์ •๋ถ€์žฅ T.042-863-2820
๊น€ํฌํ›ˆ์ธ์ฒœ์‹œ ๋ถ€ํ‰๊ตฌ ์‹ญ์ • 1๋™ 323- 19ํ˜ธ
SELECT
        STUDENT_NAME
    ,   STUDENT_ADDRESS
    FROM TB_STUDENT
    JOIN TB_DEPARTMENT USING(DEPARTMENT_NO)
    WHERE DEPARTMENT_NAME = (SELECT
                                    DEPARTMENT_NAME
                                FROM TB_DEPARTMENT
                                JOIN TB_STUDENT USING(DEPARTMENT_NO)
                                WHERE STUDENT_NAME = '์ตœ๊ฒฝํฌ'
                                );

๐Ÿ’ก Another Solution

SELECT 
       STUDENT_NAME
     , STUDENT_ADDRESS
  FROM TB_STUDENT
 WHERE DEPARTMENT_NO = (SELECT 
                               DEPARTMENT_NO
                          FROM TB_STUDENT
                         WHERE STUDENT_NAME = '์ตœ๊ฒฝํฌ');

๐Ÿ’ฌ Overall Comment

* ๋‚˜๋Š” JOIN์„ ๋‚จ์šฉํ•˜๋Š” ๋ฒ„๋ฆ‡์ด ์žˆ๋Š” ๊ฒƒ ๊ฐ™๋‹ค. ๋”์šฑ ํšจ์œจ์ ์ธ ์ฟผ๋ฆฌ๋ฅผ ์งค ์ˆ˜ ์žˆ๋„๋ก ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค๋ฅผ 
  ์—ฐ๊ด€๊ด€๊ณ„๋ฅผ ์ž˜ ์ดํ•ดํ•˜๊ณ  ํ™•์ธํ•œ ํ›„ ์ฟผ๋ฆฌ๋ฌธ์„ ์ž‘์„ฑํ•ด์•ผ๊ฒ ๋‹ค๊ณ  ์ƒ๊ฐํ•˜๊ฒŒ ๋œ ๋ฌธํ•ญ์ด์—ˆ๋‹ค.

Q18

๐Ÿ’โ€ ๊ตญ์–ด๊ตญ๋ฌธํ•™๊ณผ์—์„œ ์ด ํ‰์ ์ด ๊ฐ€์žฅ ๋†’์€ ํ•™์ƒ์˜ ์ด๋ฆ„๊ณผ ํ•™๋ฒˆ์„ ํ‘œ์‹œํ•˜๋Š” SQL ๋ฌธ์„ ์ž‘์„ฑํ•˜์„ธ์š”.

๐Ÿšฉ Example Output 1 row selected

STUDENT_NOSTUDENT_NAME
9931165์†ก๊ทผ์šฐ
SELECT
        STUDENT_NO
    ,   STUDENT_NAME
    FROM (SELECT
                    STUDENT_NO
                ,   STUDENT_NAME
                FROM TB_STUDENT
                JOIN TB_DEPARTMENT USING(DEPARTMENT_NO)
                JOIN TB_GRADE USING(STUDENT_NO)
                WHERE DEPARTMENT_NAME = '๊ตญ์–ด๊ตญ๋ฌธํ•™๊ณผ'
                GROUP BY STUDENT_NO, STUDENT_NAME
                ORDER BY AVG(POINT) DESC
                )
    WHERE ROWNUM = 1;

๐Ÿ’ก Another Solution

SELECT 
       STUDENT_NO
     , STUDENT_NAME
  FROM (SELECT 
               STUDENT_NO
             , STUDENT_NAME
             , AVG(POINT)
          FROM TB_GRADE
          JOIN TB_STUDENT USING(STUDENT_NO)
         WHERE DEPARTMENT_NO = (SELECT 
                                       DEPARTMENT_NO
                                  FROM TB_DEPARTMENT
                                 WHERE DEPARTMENT_NAME = '๊ตญ์–ด๊ตญ๋ฌธํ•™๊ณผ')
      GROUP BY STUDENT_NO, STUDENT_NAME
      ORDER BY AVG(POINT) DESC)
 WHERE ROWNUM <= 1;

๐Ÿ’ฌ Overall Comment

* ํ‰์ ์ด ๊ฐ€์žฅ ๋†’์€ ํ•™์ƒ๋งŒ ์ถœ๋ ฅํ•˜๋Š” ๋ฐฉ๋ฒ•์ด ์–ด๋–ค๊ฒŒ ์žˆ์„๊นŒ ๊ณ ๋ฏผํ•˜๋˜ ์ค‘, ์ •๋ ฌ์„ ์˜ค๋ฆ„์ฐจ์ˆœ์œผ๋กœ 
  ํ•œ ์ฟผ๋ฆฌ๋ฅผ ์„œ๋ธŒ์ฟผ๋ฆฌ(์ธ๋ผ์ธ ๋ทฐ)๋กœ ๊ฐ€์ ธ์„œ ROWNUM์„ 1๋งŒ ์ถœ๋ ฅํ•˜๋Š” ๋ฐฉ์‹์œผ๋กœ ํ’€์–ด๋‚ด๋ฉด ๋˜๊ฒ ๋‹ค๊ณ  
  ์ƒ๊ฐํ–ˆ๋‹ค. ์•„์ง ์„œ๋ธŒ์ฟผ๋ฆฌ๋ฅผ ์œ ์—ฐํ•˜๊ฒŒ ์‚ฌ์šฉํ•˜๊ธฐ ์–ด๋ ต์ง€๋งŒ ์ด๋Ÿฐ ์‹์œผ๋กœ ๊พธ์ค€ํžˆ ์‚ฌ์šฉํ•˜๋Š” ๋ฒ•์„ 
  ํ„ฐ๋“ํ•œ๋‹ค๋ฉด ์‹ค๋ ฅ์€ ๋‚˜๋‚ ํžˆ ๋Š˜์–ด๋‚  ๊ฒƒ์ด๋‹ค !

Q19

๐Ÿ’โ€ ์ถ˜ ๊ธฐ์ˆ ๋Œ€ํ•™๊ต์˜ "ํ™˜๊ฒฝ์กฐ๊ฒฝํ•™๊ณผ"๊ฐ€ ์†ํ•œ ๊ฐ™์€ ๊ณ„์—ด ํ•™๊ณผ๋“ค์˜ ํ•™๊ณผ ๋ณ„ ์ „๊ณต๊ณผ๋ชฉ ํ‰์ ์„ ํŒŒ์•…ํ•˜๊ธฐ ์œ„ํ•œ ์ ์ ˆํ•œ SQL ๋ฌธ์„ ์ฐพ์•„๋‚ด์„ธ์š”. ๋‹จ, ์ถœ๋ ฅํ—ค๋”๋Š” "๊ณ„์—ด ํ•™๊ณผ๋ช…", "์ „๊ณตํ‰์ "์œผ๋กœ ํ‘œ์‹œ๋˜๋„๋ก ํ•˜๊ณ , ํ‰์ ์€ ์†Œ์ˆ˜์  ํ•œ ์ž๋ฆฌ๊นŒ์ง€๋งŒ ๋ฐ˜์˜ฌ๋ฆผํ•˜์—ฌ ํ‘œ์‹œ๋˜๋„๋ก ํ•ฉ๋‹ˆ๋‹ค.

๐Ÿšฉ Example Output 20 rows selected

๊ณ„์—ด ํ•™๊ณผ๋ช…์ „๊ณต ํ‰์ 
๊ฐ„ํ˜ธํ•™๊ณผ3.3
๋ฌผ๋ฆฌํ•™๊ณผ3.4
. . . . . .. . . . . .
ํ™˜๊ฒฝ์‘์šฉ๊ณผํ•™๊ณผ3.5
ํ™˜๊ฒฝ์กฐ๊ฒฝํ•™๊ณผ3.5
SELECT
        DEPARTMENT_NAME "๊ณ„์—ด ํ•™๊ณผ๋ช…"
    ,   ROUND(AVG(POINT), 1) "์ „๊ณต ํ‰์ "
    FROM TB_DEPARTMENT
    JOIN TB_STUDENT USING(DEPARTMENT_NO)
    JOIN TB_GRADE USING(STUDENT_NO)
    JOIN TB_CLASS USING(DEPARTMENT_NO)
    WHERE CATEGORY = (SELECT -- ํ™˜๊ฒฝ์กฐ๊ฒฝํ•™๊ณผ๊ฐ€ ์†ํ•œ ๊ณ„์—ด ์ฐพ๋Š” ์„œ๋ธŒ ์ฟผ๋ฆฌ
                            CATEGORY
                        FROM TB_DEPARTMENT
                        WHERE DEPARTMENT_NAME = 'ํ™˜๊ฒฝ์กฐ๊ฒฝํ•™๊ณผ'
                        )
    AND CLASS_TYPE LIKE '์ „๊ณต%'
    GROUP BY DEPARTMENT_NAME
    ORDER BY 1;

profile
Tiny little habits make me
post-custom-banner

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