post-custom-banner

๐Ÿงฉ ๋„์„œ๊ด€๋ฆฌ ํ…Œ์ด๋ธ”์„ ๊ธฐ๋ฐ˜์œผ๋กœ ํ•œ ์ฟผ๋ฆฌ๋ฌธ ์ž‘์„ฑํ•˜๊ธฐ

Q1 SQL๊ตฌ๋ฌธ์„ RESULT SET์— ์ถœ๋ ฅํ•˜๊ธฐ

๐Ÿ’โ€ 4๊ฐœ ํ…Œ์ด๋ธ”์— ํฌํ•จ๋œ ๋ฐ์ดํ„ฐ ๊ฑด ์ˆ˜๋ฅผ ๊ตฌํ•˜๋Š” SQL ๊ตฌ๋ฌธ์„ ๋งŒ๋“œ๋Š” SQL ๊ตฌ๋ฌธ์„ ์ž‘์„ฑํ•˜์„ธ์š”.

๐Ÿšฉ Example Output 4 rows selected

SELECT COUNT(*) FROM TB_BOOK;
SELECT COUNT(*) FROM TB_BOOK_AUTHOR;
SELECT COUNT(*) FROM TB_PUBLISHER;
SELECT COUNT(*) FROM TB_WRITER;
SELECT
        'SELECT COUNT(*) FROM ' || A || ';' " "
    FROM (SELECT
                DISTINCT TABLE_NAME A
            FROM USER_TAB_COLUMNS
            ORDER BY 1
            );

๐Ÿ’ก Another Solution

SELECT 'SELECT COUNT(*) FROM '||TABLE_NAME||';' AS " "
FROM   USER_TABLES U
WHERE  TABLE_NAME IN ('TB_BOOK', 'TB_BOOK_AUTHOR', 'TB_PUBLISHER', 'TB_WRITER');

๐Ÿ’ฌ Overall Comment

* ์ฒ˜์Œ์—๋Š” SELECT ๊ตฌ๋ฌธ ์ž์ฒด๋ฅผ ์–ด๋–ป๊ฒŒ RESULT SET์— ๋‚˜์˜ค๊ฒŒ๋” ํ•ด์•ผํ•˜๋Š”์ง€ ๊ฐ์ด ์žกํžˆ์งˆ ์•Š์•„ ์—ฌ๋Ÿฌ ๋ฐฉ๋ฉด์œผ๋กœ ํ—ค๋งธ๋‹ค. ๊ต‰์žฅํžˆ ๋„Œ์„ผ์Šค์ ์ธ ๋ฌธ์ œ์˜€๊ณ , ์„œ๋ธŒ์ฟผ๋ฆฌ์™€ ๋ฐ์ดํ„ฐ ๋”•์…”๋„ˆ๋ฆฌ๋ฅผ ํ™œ์šฉํ•˜์—ฌ ๊ฒฐ๊ตญ ํ’€์–ด๋‚ด๋‹ˆ ๋ฟŒ๋“ฏํ–ˆ๋‹ค.

Q2 ๋ฐ์ดํ„ฐ ๋”•์…”๋„ˆ๋ฆฌ๋ฅผ ํ™œ์šฉํ•˜์—ฌ ํ…Œ์ด๋ธ”์˜ ๊ตฌ์กฐ ํŒŒ์•…ํ•˜๊ธฐ

๐Ÿ’โ€ 4๊ฐœ ํ…Œ์ด๋ธ”์˜ ๊ตฌ์กฐ๋ฅผ ํŒŒ์•…ํ•˜๋ ค๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ์ œ์‹œ๋œ ๊ฒฐ๊ณผ์ฒ˜๋Ÿผ TABLE_NAME, COLUMN_NAME, DATA_TYPE, DATA_DEFAULT, NULLABLE, CONSTRAINT_NAME, CONSTRAINT_TYPE, R_CONSTRAINT_NAME ๊ฐ’์„
์กฐํšŒํ•˜๋Š” SQL ๊ตฌ๋ฌธ์„ ์ž‘์„ฑํ•˜์„ธ์š”.

๐Ÿšฉ Example Output 29 rows selected

TABLE_NAMECOLUMN_NAMEDATA_TYPEDATA_DEFAULTNULLABLECONSTRAINT_NAMECONSTRAINT_TYPER_CONSTRAINT_NAME
TB_BOOKBOOK_NOVARCHAR2(null)NPK_BOOK_NOP(null)
TB_BOOKBOOK_NMVARCHAR2(null)Y(null)(null)(null)
. . . . . .. . . . . .. . . . . .. . . . . .. . . . . .. . . . . .. . . . . .. . . . . .
TB_WRITEREMAIL_ADDRVARCHAR2(null)Y(null)(null)(null)
TB_WRITERREGIST_DATEDATE(null)Y(null)(null)(null)
SELECT TABLE_NAME, COLUMN_NAME, DATA_TYPE, DATA_DEFAULT, NULLABLE, CONSTRAINT_NAME, CONSTRAINT_TYPE, R_CONSTRAINT_NAME
FROM USER_TAB_COLS
LEFT JOIN USER_CONS_COLUMNS USING (TABLE_NAME, COLUMN_NAME) 
LEFT JOIN USER_CONSTRAINTS USING  (TABLE_NAME, CONSTRAINT_NAME)
WHERE  TABLE_NAME IN ('TB_BOOK', 'TB_BOOK_AUTHOR', 'TB_PUBLISHER', 'TB_WRITER');

Q3 LENGTH ํ™œ์šฉํ•˜๊ธฐ

๐Ÿ’โ€ ๋„์„œ๋ช…์ด 25์ž ์ด์ƒ์ธ ์ฑ… ๋ฒˆํ˜ธ์™€ ๋„์„œ๋ช…์„ ํ™”๋ฉด์— ์ถœ๋ ฅํ•˜๋Š” SQL ๋ฌธ์„ ์ž‘์„ฑํ•˜์„ธ์š”.

๐Ÿšฉ Example Output 5 rows selected

์ฑ… ๋ฒˆํ˜ธ๋„์„œ๋ช…
1987071002์ฐฝ๋น„ 1987(58ํ˜ธ_1987 ๋ถ€์ •๊ธฐ ๊ฐ„ํ–‰๋ฌผ)
1991120501์ง„์‹ค์„ ์˜์›ํžˆ ๊ฐ์˜ฅ์— ๊ฐ€๋‘์–ด ๋‘˜ ์ˆ˜๋Š” ์—†์Šต๋‹ˆ๋‹ค
1999121501SMILES OF THE BABY BUDDHA
2004020501๋‰ดํ„ด๊ณผ ์•„์ธ์Šˆํƒ€์ธ, ์šฐ๋ฆฌ๊ฐ€ ๋ชฐ๋ž๋˜ ์ฒœ์žฌ๋“ค์˜ ์ฐฝ์กฐ์„ฑ
2007052102๋ฌธํ•™์ง‘๋ฐฐ์› ๋„์ข…ํ™˜์˜ ์‹œ๋ฐฐ๋‹ฌ-๊ฝƒ์žŽ์˜ ๋ง๋กœ ํŽธ์ง€๋ฅผ ์“ด๋‹ค
SELECT
        BOOK_NO "์ฑ… ๋ฒˆํ˜ธ"
,       BOOK_NM ๋„์„œ๋ช…
    FROM TB_BOOK
    WHERE LENGTH(BOOK_NM) >= 25;

Q4 ์ธ๋ผ์ธ ๋ทฐ๋ฅผ ํ™œ์šฉํ•œ TOP-N ๊ตฌํ•˜๊ธฐ

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

๐Ÿšฉ Example Output 1 row selected

์ž‘๊ฐ€ ์ด๋ฆ„์‚ฌ๋ฌด์‹ค ์ „ํ™”๋ฒˆํ˜ธ์ง‘ ์ „ํ™”๋ฒˆํ˜ธํœด๋Œ€ํฐ ์ „ํ™”๋ฒˆํ˜ธ
๊น€๊ฒฝ์ผ052-9272-8984(null)019-8623-8304
SELECT
        WRITER_NM "์ž‘๊ฐ€ ์ด๋ฆ„"
    ,   OFFICE_TELNO "์‚ฌ๋ฌด์‹ค ์ „ํ™”๋ฒˆํ˜ธ"
    ,   HOME_TELNO "์ง‘ ์ „ํ™”๋ฒˆํ˜ธ"
    ,   MOBILE_NO "ํœด๋Œ€ํฐ ์ „ํ™”๋ฒˆํ˜ธ"
    FROM (SELECT 
                WRITER_NM 
            ,   OFFICE_TELNO 
            ,   HOME_TELNO 
            ,   MOBILE_NO 
            FROM TB_WRITER
            WHERE MOBILE_NO LIKE '019%'
            AND WRITER_NM LIKE '๊น€%'
            ORDER BY 1
        )
    WHERE ROWNUM = 1;

๐Ÿ’ก Another Solution

SELECT *
FROM   ( SELECT WRITER_NM, 
                 OFFICE_TELNO, 
                 HOME_TELNO, 
                 MOBILE_NO
         FROM   TB_WRITER
         WHERE  WRITER_NM LIKE '๊น€%'
         AND MOBILE_NO LIKE '019-%'
         ORDER BY 1 )
WHERE  ROWNUM = 1;

Q5 COUNT ํ•จ์ˆ˜ ํ™œ์šฉํ•˜๊ธฐ

๐Ÿ’โ€ ์ €์ž‘ ํ˜•ํƒœ๊ฐ€ โ€œ์˜ฎ๊น€โ€์— ํ•ด๋‹นํ•˜๋Š” ์ž‘๊ฐ€๋“ค์ด ์ด ๋ช‡ ๋ช…์ธ์ง€ ๊ณ„์‚ฐํ•˜๋Š” SQL ๊ตฌ๋ฌธ์„ ์ž‘์„ฑํ•˜์„ธ์š”. (๊ฒฐ๊ณผ ํ—ค๋”๋Š” โ€œ์ž‘๊ฐ€(๋ช…)โ€์œผ๋กœ ํ‘œ์‹œ)

๐Ÿšฉ Example Output 1 row selected

์ž‘๊ฐ€(๋ช…)
155
SELECT
        COUNT(*) "์ž‘๊ฐ€(๋ช…)"
    FROM TB_BOOK_AUTHOR
    WHERE COMPOSE_TYPE = '์˜ฎ๊น€';

Q6 ๊ทธ๋ฃนํ•จ์ˆ˜์™€ GROUP BY ํ™œ์šฉํ•˜๊ธฐ

๐Ÿ’โ€ 300๊ถŒ ์ด์ƒ ๋“ฑ๋ก๋œ ๋„์„œ์˜ ์ €์ž‘ ํ˜•ํƒœ ๋ฐ ๋“ฑ๋ก๋œ ๋„์„œ ์ˆ˜๋Ÿ‰์„ ํ‘œ์‹œํ•˜๋Š” SQL ๊ตฌ๋ฌธ์„ ์ž‘์„ฑํ•˜์„ธ์š”. (๋‹จ, ์ €์ž‘ํ˜•ํƒœ๊ฐ€ ๋“ฑ๋ก๋˜์ง€ ์•Š์€ ๊ฒฝ์šฐ๋Š” ์ œ์™ธ)

๐Ÿšฉ Example Output 2 rows selected

์ €์ž‘ ํ˜•ํƒœ๋“ฑ๋ก๋œ ๋„์„œ ์ˆ˜๋Ÿ‰
์ง€์Œ1087
์—ฎ์Œ331
SELECT
        COMPOSE_TYPE "์ €์ž‘ ํ˜•ํƒœ"
    ,   COUNT(COMPOSE_TYPE) "๋“ฑ๋ก๋œ ๋„์„œ ์ˆ˜๋Ÿ‰"
    FROM TB_BOOK_AUTHOR
    GROUP BY COMPOSE_TYPE
    HAVING COUNT(COMPOSE_TYPE) >= 300;

Q7 ์ธ๋ผ์ธ ๋ทฐ๋ฅผ ํ™œ์šฉํ•œ TOP-N ๊ตฌํ•˜๊ธฐ

๐Ÿ’โ€ ๊ฐ€์žฅ ์ตœ๊ทผ์— ๋ฐœ๊ฐ„๋œ ์ตœ์‹ ์ž‘ ์ด๋ฆ„๊ณผ ๋ฐœํ–‰์ผ์ž, ์ถœํŒ์‚ฌ ์ด๋ฆ„์„ ํ‘œ์‹œํ•˜๋Š” SQL ๊ตฌ๋ฌธ์„ ์ž‘์„ฑํ•˜์„ธ์š”.

๐Ÿšฉ Example Output 1 rows selected

์ตœ์‹ ์ž‘ ์ด๋ฆ„๋ฐœํ–‰์ผ์ž์ถœํŒ์‚ฌ ์ด๋ฆ„
ํ˜ธ๊ธฐ์‹ฌ08/01/02์ฒญ์—ฐ
SELECT
        BOOK_NM "์ตœ์‹ ์ž‘ ์ด๋ฆ„"
    ,   ISSUE_DATE ๋ฐœํ–‰์ผ์ž
    ,   PUBLISHER_NM "์ถœํŒ์‚ฌ ์ด๋ฆ„"
    FROM (SELECT
                BOOK_NM 
            ,   ISSUE_DATE 
            ,   PUBLISHER_NM 
            FROM TB_BOOK
            ORDER BY 2 DESC
        )
    WHERE ROWNUM = 1;

๐Ÿ’ก Another Solution

SELECT 
		BOOK_NM
	,	ISSUE_DATE
	,	PUBLISHER_NM
FROM   TB_BOOK
WHERE  ISSUE_DATE = (SELECT 
							MAX(ISSUE_DATE)
                      FROM  TB_BOOK
                    ); 

Q8 ๊ทธ๋ฃนํ•จ์ˆ˜๊ฐ€ ํฌํ•จ๋œ ์ธ๋ผ์ธ ๋ทฐ๋ฅผ ํ™œ์šฉํ•œ TOP-N ๊ตฌํ•˜๊ธฐ

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

๐Ÿšฉ Example Output 3 rows selected

์ž‘๊ฐ€ ์ด๋ฆ„๊ถŒ ์ˆ˜
๊ณ ์€(้ซ˜้Š€)46
ํ™ฉ์„์˜36
์ฐฝ๋น„26
SELECT
       "์ž‘๊ฐ€ ์ด๋ฆ„"
    ,  "๊ถŒ ์ˆ˜"
    FROM (SELECT 
                WRITER_NM "์ž‘๊ฐ€ ์ด๋ฆ„"
            ,   COUNT(BOOK_NO) "๊ถŒ ์ˆ˜"
            FROM TB_WRITER
            JOIN TB_BOOK_AUTHOR USING (WRITER_NO)
            WHERE COMPOSE_TYPE = '์ง€์Œ'
            GROUP BY WRITER_NM
            ORDER BY 2 DESC
        )
    WHERE ROWNUM <= 3;

๐Ÿ’ฌ Overall Comment

* ์ธ๋ผ์ธ ๋ทฐ๋ฅผ ํ™œ์šฉํ•œ TOP-N ๋ถ„์„ ๋ฌธ์ œ๋Š” ๋งŽ์ด ํ’€์–ด๋ณด์•˜๊ธฐ ๋•Œ๋ฌธ์— ํฐ ์–ด๋ ค์›€ ์—†์ด ์ด ๋ฌธํ•ญ ๋˜ํ•œ 
  ํ’€ ์ˆ˜ ์žˆ์„๊ฑฐ๋ผ ์ƒ๊ฐํ–ˆ๋‹ค. ํ•˜์ง€๋งŒ ๊ทธ๋ฃนํ•จ์ˆ˜ COUNT์™€ GROUP BY๊ฐ€ ํฌํ•จ๋˜๋Š” ๋“ฑ ์•ฝ๊ฐ„ ๋ณต์žกํ•œ 
  ์ฟผ๋ฆฌ๋ฅผ ๊ฐ€์ง€๊ณ  ์ธ๋ผ์ธ ๋ทฐ๋ฅผ ๋งŒ๋“ค๊ณ ์ž ํ•˜๋‹ˆ ์˜ฌ๋ฐ”๋ฅธ SQL ๊ตฌ๋ฌธ์ด ์•„๋‹ˆ๋ผ๋Š” ์˜ค๋ฅ˜๊ฐ€ ๊ณ„์† ๋– ์„œ 
  ๋‚œ๊ฐํ–ˆ๋‹ค. ๊ฒฐ๋ก ์ ์œผ๋กœ๋Š” ๋ฉ”์ธ์ฟผ๋ฆฌ์— ์„œ๋ธŒ์ฟผ๋ฆฌ์˜ ๋ณ„์นญ์„ ๋„ฃ๊ณ  GROUP BY์ ˆ์„ ์‚ญ์ œํ•˜๋‹ˆ ํ•ด๊ฒฐ์ด 
  ๋˜์—ˆ๋‹ค. ์—ฌ๋Ÿฌ ๊ฐ€์ง€ ๋ฐฉ๋ฒ•์„ ์‹œ๋„ํ•˜๋‹ค ํ•ด๊ฒฐ๋ฐฉ์•ˆ์„ ๋ฐœ๊ฒฌํ•˜๋‹ˆ ์ธ์ƒ๊นŠ๊ฒŒ ๋‚จ์€ ๋ฌธํ•ญ์ด์—ˆ๋‹ค.  

Q9 UPDATE์™€ ์„œ๋ธŒ์ฟผ๋ฆฌ ํ™œ์šฉํ•˜๊ธฐ

๐Ÿ’โ€ ์ž‘๊ฐ€ ์ •๋ณด ํ…Œ์ด๋ธ”์˜ ๋ชจ๋“  ๋“ฑ๋ก์ผ์ž ํ•ญ๋ชฉ์ด ๋ˆ„๋ฝ๋˜์–ด ์žˆ๋Š” ๊ฑธ ๋ฐœ๊ฒฌํ•˜์˜€์Šต๋‹ˆ๋‹ค. ๋ˆ„๋ฝ๋œ ๋“ฑ๋ก์ผ์ž ๊ฐ’์„ ๊ฐ ์ž‘๊ฐ€์˜ โ€˜์ตœ์ดˆ ์ถœํŒ๋„์„œ์˜ ๋ฐœํ–‰์ผ๊ณผ ๋™์ผํ•œ ๋‚ ์งœโ€™๋กœ ๋ณ€๊ฒฝ์‹œํ‚ค๋Š” SQL ๊ตฌ๋ฌธ์„ ์ž‘์„ฑํ•˜์„ธ์š”. (COMMIT ์ฒ˜๋ฆฌ ์š”๋ง)

UPDATE
        TB_WRITER TW
    SET REGIST_DATE = (SELECT
                                MIN(ISSUE_DATE)
                            FROM TB_BOOK 
                            JOIN TB_BOOK_AUTHOR USING(BOOK_NO)
                            WHERE TW.WRITER_NO = WRITER_NO
                            -- TB_WRITER์™€ TB_BOOK_AUTHOR์˜ ๊ฐ WRITER_NO๊ฐ€ ๋™์ผํ•œ ์กฐ๊ฑด
                        );

๐Ÿ’ฌ Overall Comment

* ์—ฌ๊ธฐ์„œ๋„ ์ž‘๊ฐ€ ๋งˆ๋‹ค์˜ ์ตœ์ดˆ ์ถœํŒ๋„์„œ ๋ฐœํ–‰์ผ์€ ์„œ๋ธŒ์ฟผ๋ฆฌ๋กœ ์™„์„ฑํ–ˆ์œผ๋‚˜ ๊ฐ€์žฅ ํ•ด๊ฒฐํ•˜๊ธฐ ์–ด๋ ค์› ๋˜ 
  ๋ถ€๋ถ„์€ '์ž‘๊ฐ€๋งˆ๋‹ค์˜ ์ตœ์ดˆ ๋ฐœํ–‰์ผ ์‚ฝ์ž…'์ด์—ˆ๋‹ค. UPDATE๊ตฌ๋ฌธ์—์„œ ์„œ๋ธŒ์ฟผ๋ฆฌ๋ฅผ ์‚ฌ์šฉํ•ด๋ณธ ๊ฒฝํ—˜์ด 
  ์ ๋‹ค๋ณด๋‹ˆ ์–ด๋–ค ๋ฐฉํ–ฅ์œผ๋กœ ์‚ฝ์ž…์„ ํ•ด์•ผํ• ์ง€ ๋‚œ๊ฐํ–ˆ๋‹ค. ์š”๋ฆฌ์กฐ๋ฆฌ ์ฟผ๋ฆฌ๋ฅผ ๋ฐ”๊ฟ”๋ณด๋‹ค๊ฐ€ ๋ฉ”์ธ์ฟผ๋ฆฌ์™€ 
  ์„œ๋ธŒ์ฟผ๋ฆฌ๋ฅผ ์ƒํ˜ธ์—ฐ๊ด€์ง€์–ด 'B_WRITER์™€ TB_BOOK_AUTHOR์˜ ๊ฐ WRITER_NO๊ฐ€ ๋™์ผํ•œ ์กฐ๊ฑด'์„ 
  ์„œ๋ธŒ์ฟผ๋ฆฌ์— ์ž…๋ ฅํ•ด์ฃผ๋ฉด ํ•ด๊ฒฐ์ด ๋˜์—ˆ๋‹ค. ์˜ค๋ž˜ ๊ณ ๋ฏผํ•˜๋˜ ๋ฌธ์ œ์˜€๋Š”๋ฐ ๊ฒฐ๊ตญ์—” ํ’€์–ด๋‚ด์–ด ๊ต‰์žฅํžˆ ๋ฟŒ๋“ฏํ–ˆ๋‹ค.

Q10 ๊ธฐ์กด ํ…Œ์ด๋ธ”์˜ ๋ฐ์ดํ„ฐ๋กœ ์ƒˆ๋กœ์šด ํ…Œ์ด๋ธ” ์ƒ์„ฑํ•˜๊ธฐ

๐Ÿ’โ€ ํ˜„์žฌ ๋„์„œ์ €์ž ์ •๋ณด ํ…Œ์ด๋ธ”์€ ์ €์„œ์™€ ๋ฒˆ์—ญ์„œ๋ฅผ ๊ตฌ๋ถ„ ์—†์ด ๊ด€๋ฆฌํ•˜๊ณ  ์žˆ์ง€๋งŒ ์•ž์œผ๋กœ๋Š” ๋ฒˆ์—ญ์„œ๋Š” ๋”ฐ๋กœ ๊ด€๋ฆฌํ•˜๋ ค๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ์ œ์‹œ๋œ ๋‚ด์šฉ์— ๋งž๊ฒŒ โ€œTB_BOOK_TRANSLATORโ€ ํ…Œ์ด๋ธ”์„ ์ƒ์„ฑํ•˜๋Š” SQL ๊ตฌ๋ฌธ์„ ์ž‘์„ฑํ•˜์„ธ์š”.
(Primary Key ์ œ์•ฝ ์กฐ๊ฑด ์ด๋ฆ„์€ โ€œPK_BOOK_TRANSLATORโ€๋กœ ํ•˜๊ณ , Reference ์ œ์•ฝ ์กฐ๊ฑด ์ด๋ฆ„์€ โ€œFK_BOOK_TRANSLATOR_01โ€, โ€œFK_BOOK_TRANSLATOR_02โ€๋กœ ํ•  ๊ฒƒ)

๐Ÿšฉ Example Output

CREATE TABLE TB_BOOK_TRANSLATOR(
    BOOK_NO VARCHAR2(10) NOT NULL
,   WRITER_NO VARCHAR2(10) NOT NULL
,   TRANS_LANG VARCHAR2(60) 
,   CONSTRAINT PK_BOOK_TRANSLATOR PRIMARY KEY(BOOK_NO, WRITER_NO)
,   CONSTRAINT FK_BOOK_TRANSLATOR_01 FOREIGN KEY(BOOK_NO) REFERENCES TB_BOOK(BOOK_NO)
,   CONSTRAINT FK_BOOK_TRANSLATOR_02 FOREIGN KEY(WRITER_NO) REFERENCES TB_WRITER(WRITER_NO)
);

Q11 ์ƒˆ๋กœ์šด ํ…Œ์ด๋ธ”๋กœ ๋ฐ์ดํ„ฐ ์˜ฎ๊ธฐ๊ธฐ

๐Ÿ’โ€ ๋„์„œ ์ €์ž‘ ํ˜•ํƒœ(composetype)๊ฐ€ '์˜ฎ๊น€', '์—ญ์ฃผ', 'ํŽธ์—ญ', '๊ณต์—ญ'์— ํ•ด๋‹นํ•˜๋Š” ๋ฐ์ดํ„ฐ๋Š” ๋„์„œ ์ €์ž ์ •๋ณด ํ…Œ์ด๋ธ”์—์„œ ๋„์„œ ์—ญ์ž ์ •๋ณด ํ…Œ์ด๋ธ”(TB_BOOK TRANSLATOR)๋กœ ์˜ฎ๊ธฐ๋Š” SQL ๊ตฌ๋ฌธ์„ ์ž‘์„ฑํ•˜์„ธ์š”. ๋‹จ, โ€œTRANS_LANGโ€ ์ปฌ๋Ÿผ์€ NULL ์ƒํƒœ๋กœ ๋‘๋„๋ก ํ•ฉ๋‹ˆ๋‹ค. (์ด๋™๋œ ๋ฐ์ดํ„ฐ๋Š” ๋” ์ด์ƒ TB_BOOK_AUTHOR ํ…Œ์ด๋ธ”์— ๋‚จ์•„ ์žˆ์ง€ ์•Š๋„๋ก ์‚ญ์ œํ•  ๊ฒƒ)


/* ๋ฐ์ดํ„ฐ ๋ณต์‚ฌ */
INSERT
    INTO
    TB_BOOK_TRANSLATOR(BOOK_NO, WRITER_NO)
SELECT
        BOOK_NO
    ,   WRITER_NO
    FROM TB_BOOK_AUTHOR
    WHERE COMPOSE_TYPE IN ('์˜ฎ๊น€', '์—ญ์ฃผ', 'ํŽธ์—ญ', '๊ณต์—ญ');
    
/* ๋‚จ์•„์žˆ๋Š” ๋ฐ์ดํ„ฐ ์‚ญ์ œ */
DELETE
    FROM TB_BOOK_AUTHOR
    WHERE COMPOSE_TYPE IN ('์˜ฎ๊น€', '์—ญ์ฃผ', 'ํŽธ์—ญ', '๊ณต์—ญ');

Q12 JOIN ํ™œ์šฉํ•˜๊ธฐ

๐Ÿ’โ€ 2007๋…„๋„์— ์ถœํŒ๋œ ๋ฒˆ์—ญ์„œ ์ด๋ฆ„๊ณผ ๋ฒˆ์—ญ์ž(์—ญ์ž)๋ฅผ ํ‘œ์‹œํ•˜๋Š” SQL ๊ตฌ๋ฌธ์„ ์ž‘์„ฑํ•˜์„ธ์š”.

๐Ÿšฉ Example Output 5 rows selected

๋ฒˆ์—ญ์„œ ๋ช…๋ฒˆ์—ญ์ž
์˜ฌ๋ฆฌ๋ฒ„ ํŠธ์œ„์ŠคํŠธ 2์œคํ˜œ์ค€
์˜ฌ๋ฆฌ๋ฒ„ ํŠธ์œ„์ŠคํŠธ 1์œคํ˜œ์ค€
์—ญ์ฃผ ์‹œํ’ˆ์ด์ฒ ๋ฆฌ
์ถœ๋™! ๊ทธ๋ฆฐํŒ€ ๊ณ ๋ž˜๋ฅผ ๊ตฌํ•˜์ž์œ ํฌ์„
์•ˆ๊ณผ๋ฐ–: ์˜๋ฏธ๋ฌธํ•™์—ฐ๊ตฌ ์ œ23ํ˜ธ์ •ํ˜œ์šฉ
SELECT
        BOOK_NM "๋ฒˆ์—ญ์„œ ๋ช…"
    ,   WRITER_NM ๋ฒˆ์—ญ์ž
    FROM TB_BOOK 
    JOIN TB_BOOK_TRANSLATOR USING(BOOK_NO)
    JOIN TB_WRITER USING(WRITER_NO)
    WHERE BOOK_NO LIKE '2007%';

Q13 ๋ทฐ ์ƒ์„ฑํ•˜๊ธฐ

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

๐Ÿšฉ Example Output 169 rows selected

๋„์„œ๋ช…๋ฒˆ์—ญ์ž์ถœํŒ์ผ
๋ฏผ์ค‘๋ฌธํ™”์™€ ์ œ3์„ธ๊ณ„์‹ ๊ฒฝ๋ฆผ1983๋…„ 10์›” 25์ผ
ํ”ผ์˜ ๊ฝƒ์žŽ ไธ‹๊น€์ข…์ฒ 1983๋…„ 09์›” 10์ผ
. . . . . .. . . . . .
์‹œ์กฐ ์—์Ž„์ด์ž„ํฌ๊ทผ1995๋…„ 06์›” 30์ผ
์—ญ์ฃผ ๋ชฉ๋ฏผ์‹ฌ์„œ 2๋‹ค์‚ฐ์—ฐ๊ตฌํšŒ1995๋…„ 05์›” 25์ผ

/* CREATE VIEW ๊ถŒํ•œ ๋ถ€์—ฌ */
GRANT CREATE VIEW TO C##BOOK;

/* ๋ทฐ ์ƒ์„ฑ */
CREATE OR REPLACE VIEW VW_BOOK_TRANSLATOR
AS
SELECT
        BOOK_NM ๋„์„œ๋ช…
    ,   WRITER_NM ๋ฒˆ์—ญ์ž
    ,   TO_CHAR(TO_DATE(SUBSTR(BOOK_NO, 1, 8), 'YYMMDD'), 'RRRR"๋…„ "MM"์›” "DD"์ผ "') ์ถœํŒ์ผ
    FROM TB_BOOK 
    JOIN TB_BOOK_TRANSLATOR USING(BOOK_NO)
    JOIN TB_WRITER USING(WRITER_NO)
    WITH READ ONLY;

Q14 INSERT๋ฅผ ํ™œ์šฉํ•˜์—ฌ ๋ฐ์ดํ„ฐ ์‚ฝ์ž…ํ•˜๊ธฐ

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

INSERT
    INTO TB_PUBLISHER
(
    PUBLISHER_NM, PUBLISHER_TELNO, DEAL_YN
)
VALUES
(
    '์ถ˜ ์ถœํŒ์‚ฌ', '02-6710-3737', DEFAULT
);

COMMIT;

Q15 GROUP BY๋ฅผ ํ™œ์šฉํ•˜์—ฌ ๋™๋ช…์ด์ธ ์ˆ˜ ์„ธ๊ธฐ

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

๐Ÿšฉ Example Output 3 rows selected

์ด๋ฆ„๋™๋ช…์ด์ธ ์ˆ˜
์ด์ƒ๊ถŒ2
์ด๊ธฐ์˜2
์ด์ •ํ˜ธ2
SELECT
        WRITER_NM ์ด๋ฆ„
    ,   COUNT(WRITER_NM) "๋™๋ช…์ด์ธ ์ˆ˜" 
    FROM TB_WRITER
    GROUP BY WRITER_NM
    HAVING COUNT(WRITER_NM) > 1;

Q16 UPDATE๋ฅผ ํ™œ์šฉํ•˜์—ฌ NULL๊ฐ’ ์น˜ํ™˜ํ•˜๊ธฐ

๐Ÿ’โ€ ๋„์„œ์˜ ์ €์ž ์ •๋ณด ์ค‘ ์ €์ž‘ ํ˜•ํƒœ(compose_type)๊ฐ€ ๋ˆ„๋ฝ๋œ ๋ฐ์ดํ„ฐ๋“ค์ด ์ ์ง€ ์•Š๊ฒŒ ์กด์žฌํ•ฉ๋‹ˆ๋‹ค. ํ•ด๋‹น ์ปฌ๋Ÿผ์ด NULL์ธ ๊ฒฝ์šฐ '์ง€์Œ'์œผ๋กœ ๋ณ€๊ฒฝํ•˜๋Š” SQL ๊ตฌ๋ฌธ์„ ์ž‘์„ฑํ•˜์„ธ์š”. (COMMIT ์ฒ˜๋ฆฌ ์š”๋ง)

UPDATE
        TB_BOOK_AUTHOR
    SET COMPOSE_TYPE = DEFAULT -- ์ด ์ปฌ๋Ÿผ์˜ ๊ธฐ๋ณธ๊ฐ’์ด '์ง€์Œ'
    WHERE COMPOSE_TYPE IS NULL;

COMMIT;

๐Ÿ’ฌ Overall Comment

* ์ฒ˜์Œ์— WHERE COMPOSE_TYPE = NULL;๋กœ ์ž…๋ ฅํ–ˆ๋‹ค๊ฐ€ ๊ณ„์† 0ํ–‰์ด ์‚ฝ์ž…๋˜์—ˆ๋‹ค๋Š” ๋ฌธ๊ตฌ๋งŒ 
๋œจ๊ธธ๋ž˜ ๋ญ๊ฐ€ ๋ฌธ์ œ์ธ์ง€ ์ž ์‹œ ํ—ท๊ฐˆ๋ ธ๋‹ค. NULL์€ ๋ฐ˜๋“œ์‹œ '='์ด ์•„๋‹Œ 'IS'๋ฅผ ํ•จ๊ป˜ ์‚ฌ์šฉํ•ด์•ผ ํ•œ๋‹ค๋Š” 
๊ฒƒ์„ ์žŠ์ง€๋ง์ž!

Q17 LIKE์™€ LENGTHํ•จ์ˆ˜ ํ™œ์šฉํ•˜๊ธฐ

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

๐Ÿšฉ Example Output 8 rows selected

์ด๋ฆ„์‚ฌ๋ฌด์‹ค ์ „ํ™”๋ฒˆํ˜ธ
๊น€์ค€ํƒœ02-777-3558
์ž„ํ˜•ํƒ02-295-5329
. . . . . .. . . . . .
๊ณฝํ•™์†ก02-714-3181
์†ก์„ฑ์ˆ˜02-504-4275
SELECT
        WRITER_NM ์ด๋ฆ„
    ,   OFFICE_TELNO "์‚ฌ๋ฌด์‹ค ์ „ํ™”๋ฒˆํ˜ธ"
    FROM TB_WRITER
    WHERE OFFICE_TELNO LIKE '02%'
    AND LENGTH(OFFICE_TELNO) < 12;

๐Ÿ’ก Another Solution

SELECT WRITER_NM, 
        OFFICE_TELNO
FROM   TB_WRITER
WHERE  OFFICE_TELNO LIKE '02-___-%'
ORDER BY 1; 

๐Ÿ’ฌ Overall Comment

* ์ฒ˜์Œ์— ๊ตญ๋ฒˆ์ด ์ง€์—ญ๋ฒˆํ˜ธ์ธ 02๋ฅผ ์˜๋ฏธํ•˜๋Š”์ค„๋กœ ์•Œ๊ณ  ๋ฌธ์ œ๊ฐ€ ์กฐ๊ธˆ ์ด์ƒํ•˜๋‹ค๊ณ  ์ƒ๊ฐ๋˜์—ˆ์œผ๋‚˜ 
  ๊ฒ€์ƒ‰์„ ํ•ด๋ณด๋‹ˆ, ๊ตญ๋ฒˆ์€ ์ง€์—ญ๋ฒˆํ˜ธ(02)์˜ ๋ฐ”๋กœ ๋’ท ๋ฒˆํ˜ธ๋ฅผ ์˜๋ฏธํ•˜๋Š” ๊ฒƒ์ด์—ˆ๋‹ค. ํ•˜ํ•˜; 
  ๊ตญ๋ฒˆ์˜ ๋’ท์ž๋ฆฌ๋Š” ๋ชจ๋‘ 4์ž๋ฆฌ์ผํ…Œ๋‹ˆ, ๋”ฐ๋ผ์„œ LENGTH(OFFICE_TELNO) < 12;๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ 
  ๊ตญ๋ฒˆ์ด 3์ž๋ฆฌ์ธ ๋ฒˆํ˜ธ๋งŒ ์กฐํšŒํ•˜์˜€๋‹ค. ๋ฌธ์ œ๋ฅผ ํ•ด์„ํ•˜๊ณ  ํ’€๊ธฐ ์œ„ํ•ด์„œ๋Š” ์ด๋Ÿฐ ๊ธฐ๋ณธ์ ์ธ ์ƒ์‹ ๋˜ํ•œ 
  ํ•„์ˆ˜๋ผ๋Š” ๊ฒƒ์„ ๋˜ ๋ฐฐ์› ๋‹ค.

Q18 MONTHS_BETWEEN ํ™œ์šฉํ•˜๊ธฐ

๐Ÿ’โ€ 2006๋…„ 1์›” ๊ธฐ์ค€์œผ๋กœ ๋“ฑ๋ก๋œ์ง€ 31๋…„ ์ด์ƒ ๋œ ์ž‘๊ฐ€ ์ด๋ฆ„์„ ์ด๋ฆ„์ˆœ์œผ๋กœ ํ‘œ์‹œํ•˜๋Š” SQL ๊ตฌ๋ฌธ์„ ์ž‘์„ฑํ•˜์„ธ์š”.

๐Ÿšฉ Example Output 10 rows selected

์ž‘๊ฐ€ ์ด๋ฆ„
๊น€์ •ํ•œ(้‡‘ๅปทๆผข)
๋ฆฌ์˜ํฌ
. . . . . .
์ฒœ์Šน์„ธ
ํ™ฉ์„์˜
SELECT
        WRITER_NM "์ž‘๊ฐ€ ์ด๋ฆ„"
    FROM TB_WRITER
    WHERE MONTHS_BETWEEN(TO_DATE(200601, 'YYMM'), REGIST_DATE) / 12 >= 31
    ORDER BY 1;

Q19 CASE ํ•จ์ˆ˜ ์‚ฌ์šฉํ•˜๊ธฐ

๐Ÿ’โ€ ์š”์ฆ˜ ๋“ค์–ด ๋‹ค์‹œ๊ธˆ ์ธ๊ธฐ๋ฅผ ์–ป๊ณ  ์žˆ๋Š” 'ํ™ฉ๊ธˆ๊ฐ€์ง€' ์ถœํŒ์‚ฌ๋ฅผ ์œ„ํ•œ ๊ธฐํš์ „์„ ์—ด๋ ค๊ณ  ํ•ฉ๋‹ˆ๋‹ค. 'ํ™ฉ๊ธˆ๊ฐ€์ง€' ์ถœํŒ์‚ฌ์—์„œ ๋ฐœํ–‰ํ•œ ๋„์„œ ์ค‘ ์žฌ๊ณ  ์ˆ˜๋Ÿ‰์ด 10๊ถŒ ๋ฏธ๋งŒ์ธ ๋„์„œ๋ช…๊ณผ ๊ฐ€๊ฒฉ, ์žฌ๊ณ ์ƒํƒœ๋ฅผ ํ‘œ์‹œํ•˜๋Š” SQL ๊ตฌ๋ฌธ์„ ์ž‘์„ฑํ•˜์„ธ์š”. ์žฌ๊ณ  ์ˆ˜๋Ÿ‰์ด 5๊ถŒ ๋ฏธ๋งŒ์ธ ๋„์„œ๋Š” โ€˜์ถ”๊ฐ€์ฃผ๋ฌธํ•„์š”โ€™๋กœ, ๋‚˜๋จธ์ง€๋Š” โ€˜์†Œ๋Ÿ‰๋ณด์œ โ€™๋กœ ํ‘œ์‹œํ•˜๊ณ , ์žฌ๊ณ ์ˆ˜๋Ÿ‰์ด ๋งŽ์€ ์ˆœ, ๋„์„œ๋ช… ์ˆœ์œผ๋กœ ํ‘œ์‹œ๋˜๋„๋ก ํ•ฉ๋‹ˆ๋‹ค.

๐Ÿšฉ Example Output 12 rows selected

๋„์„œ๋ช…๊ฐ€๊ฒฉ์žฌ๊ณ ์ƒํƒœ
๊ทธ๋ฆฌ์Šค ๋กœ๋งˆ ์‹ ํ™” 26000์†Œ๋Ÿ‰๋ณด์œ 
๋ชจ์—ฌ๋ผ ๊พธ๋Ÿฌ๊ธฐ ์‹ ๋“ค8000์†Œ๋Ÿ‰๋ณด์œ 
. . . . . .. . . . . .. . . . . .
์žฅ๊ธธ์‚ฐ 97500์ถ”๊ฐ€์ฃผ๋ฌธํ•„์š”
ํ•œ๊ตญ๊ฒฝ์ œ์˜ ๊ตฌ์กฐ์™€ ์ „๊ฐœ6000์ถ”๊ฐ€์ฃผ๋ฌธํ•„์š”
SELECT
        BOOK_NM ๋„์„œ๋ช…
    ,   PRICE ๊ฐ€๊ฒฉ
    ,   CASE
            WHEN STOCK_QTY < 5 THEN '์ถ”๊ฐ€์ฃผ๋ฌธํ•„์š”'
            ELSE '์†Œ๋Ÿ‰๋ณด์œ '
        END ์žฌ๊ณ ์ƒํƒœ
    FROM TB_BOOK
    WHERE PUBLISHER_NM = 'ํ™ฉ๊ธˆ๊ฐ€์ง€'
    AND STOCK_QTY < 10
    ORDER BY STOCK_QTY DESC, 1;

๐Ÿ’ฌ Overall Comment

* ์ฒ˜์Œ์—๋Š” ์„ ํƒํ•จ์ˆ˜ DECODE๋ฅผ ์‚ฌ์šฉํ•˜๊ณ ์ž ํ–ˆ์œผ๋‚˜, ์š”๋ฆฌ์กฐ๋ฆฌ ์‚ฌ์šฉํ•˜๋‹ค๋ณด๋‹ˆ ๋น„๊ต์—ฐ์‚ฐ์ž ์‚ฌ์šฉ์ด 
๋ถˆ๊ฐ€๋Šฅํ•˜๋‹ค๋Š” ๊ฒƒ์„ ๊นจ๋‹ฌ์•˜๋‹ค. ๊ทธ๋ž˜์„œ ์•„์ง ์‚ฌ์šฉํ•ด๋ณด์ง€ ๋ชป ํ–ˆ๋˜ CASEํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์žฌ๊ณ ๊ฐ€ 
5๊ถŒ ๋ฏธ๋งŒ์ด๋ฉด '์ถ”๊ฐ€์ฃผ๋ฌธํ•„์š”', ๋‚˜๋จธ์ง€(ELSE)๋Š” '์†Œ๋Ÿ‰๋ณด์œ '๋กœ ์ฟผ๋ฆฌ๋ฌธ์„ ์ž‘์„ฑํ•˜์˜€๋‹ค. 
ํšจ์œจ์ ์ด๊ณ  ๊น”๋”ํ•œ ์ฟผ๋ฆฌ๊ฐ€ ์™„์„ฑ๋œ ๋“ฏํ•˜์—ฌ ๋ฟŒ๋“ฏํ–ˆ๋‹ค.

Q20 ๋‹ค์ค‘ ๋ฐ SELF JOIN ํ™œ์šฉํ•˜๊ธฐ

๐Ÿ’โ€ '์•„ํƒ€ํŠธ๋กค' ๋„์„œ ์ž‘๊ฐ€์™€ ์—ญ์ž๋ฅผ ํ‘œ์‹œํ•˜๋Š” SQL ๊ตฌ๋ฌธ์„ ์ž‘์„ฑํ•˜์„ธ์š”. (๊ฒฐ๊ณผ ํ—ค๋”๋Š” โ€˜๋„์„œ๋ช…โ€™,โ€™์ €์žโ€™,โ€™์—ญ์žโ€™๋กœ ํ‘œ์‹œ)

๐Ÿšฉ Example Output 1 row selected

๋„์„œ๋ช…์ €์ž์—ญ์ž
์•„ํƒ€ํŠธ๋กค๊ณ ํŠธํ™€ํŠธ ์—ํ”„๋ผ์ž„ ๋ ˆ์”ฝ(GOTTHOLD EPHRAIM LESSING)์œค๋„์ค‘
SELECT
        BOOK_NM ๋„์„œ๋ช…
    ,   TW1.WRITER_NM ์ €์ž
    ,   TW2.WRITER_NM ์—ญ์ž
    FROM TB_BOOK TB
    JOIN TB_BOOK_AUTHOR TBA ON(TB.BOOK_NO = TBA.BOOK_NO)
    JOIN TB_BOOK_TRANSLATOR TBT ON(TB.BOOK_NO = TBT.BOOK_NO)
    JOIN TB_WRITER TW1 ON(TBA.WRITER_NO = TW1.WRITER_NO)
    JOIN TB_WRITER TW2 ON(TBT.WRITER_NO = TW2.WRITER_NO)
    WHERE BOOK_NM = '์•„ํƒ€ํŠธ๋กค';

๐Ÿ’ฌ Overall Comment

* ์ฒ˜์Œ์— ๋ฌธ์ œ๋ฅผ ์ฝ์—ˆ์„ ๋•Œ, ์ด ๋ฌธํ•ญ์€ ๋ฌด์กฐ๊ฑด SELF-JOIN์„ ์‚ฌ์šฉํ•ด์•ผ๊ฒ ๋‹ค๊ณ  ์ƒ๊ฐํ–ˆ๋‹ค. 
  ํ•˜์ง€๋งŒ ์‹ค์ œ๋กœ ์ ์šฉ์‹œํ‚ค๊ธฐ์— ์•ฝ๊ฐ„ ํ—ท๊ฐˆ๋ฆฌ๋Š” ๋ถ€๋ถ„์ด ์žˆ์–ด ํ—ค๋งธ์ง€๋งŒ ์ฒœ์ฒœํžˆ ๋…ผ๋ฆฌ์ ์œผ๋กœ ์ƒ๊ฐํ•˜์—ฌ 
  JOIN์˜ ์ˆœ์„œ๋ฅผ ์‹ ๊ฒฝ์“ฐ๋ฉฐ ์ฟผ๋ฆฌ๋ฌธ์„ ์ž‘์„ฑํ•œ ๊ฒฐ๊ณผ ์˜ฌ๋ฐ”๋ฅธ RESULT SET์ด ์ถœ๋ ฅ๋˜์—ˆ๋‹ค :)
  ํ‰์†Œ์— JOIN์„ ๋‚จ์šฉํ•˜๋Š” ์Šต๊ด€์ด ์žˆ๋‹ค๋ณด๋‹ˆ ์™„์„ฑํ•˜๊ณ ๋‚˜์„œ๋„ ์ด๊ฒƒ์ด ์ตœ์„ ์ธ์ง€ ์˜๊ตฌ์‹ฌ์ด ๋“ค์—ˆ์ง€๋งŒ 
  ๊ฒ€ํ† ํ•œ ๊ฒฐ๊ณผ ํ•„์š”์—†๋Š” JOIN์ ˆ์€ ์—†์—ˆ๋‹ค. ์ด๊ฒƒ ์—ญ์‹œ ๋ฟŒ๋“ฏํ•œ ๋ฌธํ•ญ ์ค‘ ํ•˜๋‚˜์ด๋‹ค.

Q21 MONTHS_BETWEEN ํ™œ์šฉํ•˜๊ธฐ

๐Ÿ’โ€ ํ˜„์žฌ ๊ธฐ์ค€์œผ๋กœ ์ตœ์ดˆ ๋ฐœํ–‰์ผ๋กœ๋ถ€ํ„ฐ ๋งŒ 30๋…„์ด ๊ฒฝ๊ณผ๋˜๊ณ , ์žฌ๊ณ  ์ˆ˜๋Ÿ‰์ด 90๊ถŒ ์ด์ƒ์ธ ๋„์„œ์— ๋Œ€ํ•ด ๋„์„œ๋ช…, ์žฌ๊ณ ์ˆ˜๋Ÿ‰, ์›๋ž˜ ๊ฐ€๊ฒฉ, 20% ์ธํ•˜ ๊ฐ€๊ฒฉ์„ ํ‘œ์‹œํ•˜๋Š” SQL ๊ตฌ๋ฌธ์„ ์ž‘์„ฑํ•˜์„ธ์š”. (๊ฒฐ๊ณผ ํ—ค๋”๋Š” โ€œ๋„์„œ๋ช…โ€, โ€œ์žฌ๊ณ ์ˆ˜๋Ÿ‰โ€, โ€œ๊ฐ€๊ฒฉ(Org)โ€, โ€œ๊ฐ€๊ฒฉ(New)โ€๋กœ ํ‘œ์‹œ. ์žฌ๊ณ  ์ˆ˜๋Ÿ‰์ด ๋งŽ์€ ์ˆœ, ํ• ์ธ ๊ฐ€๊ฒฉ์ด ๋†’์€ ์ˆœ, ๋„์„œ๋ช…์ˆœ์œผ๋กœ ํ‘œ์‹œ)

๐Ÿšฉ Example Output 58 rows selected

๋„์„œ๋ช…์žฌ๊ณ ์ˆ˜๋Ÿ‰๊ฐ€๊ฒฉ(Org)๊ฐ€๊ฒฉ(New)
์‚ฌ๊ณผ๋‚˜๋ฌด๋ฐญ ๋‹ฌ๋‹˜_1978๋…„ํŒ9885006800
๋ฌธํ•™๊ณผ ์˜ˆ์ˆ ์˜ ์‚ฌํšŒ์‚ฌ 49880006400
. . . . . .. . . . . .. . . . . .. . . . . .
๋˜˜์Šค๋˜์ด9020001600
์ฐฝ์ž‘๊ณผ๋น„ํ‰ 49ํ˜ธ(78๋…„ ๊ฐ€์„ํ˜ธ)90800640
SELECT
        BOOK_NM ๋„์„œ๋ช…
    ,   STOCK_QTY ์žฌ๊ณ ์ˆ˜๋Ÿ‰
    ,   PRICE "๊ฐ€๊ฒฉ(Org)"
    ,   (PRICE - PRICE * 0.2) "๊ฐ€๊ฒฉ(New)"
    FROM TB_BOOK
    WHERE MONTHS_BETWEEN(SYSDATE, ISSUE_DATE) / 12 >= 30
    AND STOCK_QTY >= 90
    ORDER BY 2 DESC, 4 DESC, 1;

profile
Tiny little habits make me
post-custom-banner

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