[ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค] ์กฐ๊ฑด์— ๋ถ€ํ•ฉํ•˜๋Š” ์ค‘๊ณ ๊ฑฐ๋ž˜ ์ƒํƒœ ์กฐํšŒํ•˜๊ธฐ | ์˜ค๋ผํด
ยท
๐Ÿ“ Coding Test/SQL
[level 2] ์กฐ๊ฑด์— ๋ถ€ํ•ฉํ•˜๋Š” ์ค‘๊ณ ๊ฑฐ๋ž˜ ์ƒํƒœ ์กฐํšŒํ•˜๊ธฐ - 164672๋ฌธ์ œ ๋งํฌ์„ฑ๋Šฅ ์š”์•ฝ๋ฉ”๋ชจ๋ฆฌ: 0.0 MB, ์‹œ๊ฐ„: 0.00 ms๊ตฌ๋ถ„์ฝ”๋”ฉํ…Œ์ŠคํŠธโ€…์—ฐ์Šต > String๏ผŒโ€…Date์ •๋‹ตSELECT BOARD_ID, WRITER_ID, TITLE, PRICE, CASE WHEN STATUS = 'SALE' THEN 'ํŒ๋งค์ค‘' WHEN STATUS = 'RESERVED' THEN '์˜ˆ์•ฝ์ค‘' ELSE '๊ฑฐ๋ž˜์™„๋ฃŒ' END AS STATUSFROM USED_GOODS_BOARDWHERE TO_CHAR(CREATED_DATE, 'YYYY-MM-DD') = '2022-10-05'ORDER BY BOARD_ID DESC;
[ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค] ์˜ค๋žœ ๊ธฐ๊ฐ„ ๋ณดํ˜ธํ•œ ๋™๋ฌผ(1) | ์˜ค๋ผํด
ยท
๐Ÿ“ Coding Test/SQL
๋ฌธ์ œ์˜ค๋žœ ๊ธฐ๊ฐ„ ๋ณดํ˜ธํ•œ ๋™๋ฌผ(1)ํ’€์ดSELECT *FROM ( SELECT AI.NAME, AI.DATETIME FROM ANIMAL_INS AI LEFT JOIN ANIMAL_OUTS AO ON AI.ANIMAL_ID = AO.ANIMAL_ID WHERE AO.ANIMAL_ID IS NULL ORDER BY AI.DATETIME)WHERE ROWNUM LEFT JOIN์„ ์‚ฌ์šฉํ•˜์—ฌ ๋“ค์–ด์˜จ ๊ธฐ๋ก์€ ์žˆ์ง€๋งŒ ๋‚˜๊ฐ„ ๊ธฐ๋ก์ด ์—†๋Š” ๋™๋ฌผ์„ ์กฐํšŒํ•˜๊ณ  ๋“ค์–ด์˜จ ์‹œ๊ฐ„์„ ๊ธฐ์ค€์œผ๋กœ ์˜ค๋ฆ„์ฐจ์ˆœ ์ •๋ ฌํ•œ ์ธ๋ผ์ธ ๋ทฐ๋ฅผ ๋งŒ๋“ญ๋‹ˆ๋‹ค.MySQL์—๋Š” limit ๊ธฐ๋Šฅ์„ ์‚ฌ์šฉํ•˜๋ฉด ์›ํ•˜๋Š” ๋ฐ์ดํ„ฐ์˜ ์ˆ˜๋ฅผ ๊ฐ€์ ธ์˜ค๊ธฐ ํŽธํ•œ๋ฐ ์˜ค๋ผํด์—๋Š” ์—†์Šต๋‹ˆ๋‹ค.ROWNUM์„ ์‚ฌ์šฉํ•˜์—ฌ ์›ํ•˜๋Š” ์ˆ˜๋งŒํผ์˜ ๋ฐ์ดํ„ฐ๋ฅผ ๊ฐ€์ ธ์˜ฌ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
[ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค] ์—†์–ด์ง„ ๊ธฐ๋ก ์ฐพ๊ธฐ | ์˜ค๋ผํด
ยท
๐Ÿ“ Coding Test/SQL
๋ฌธ์ œ์—†์–ด์ง„ ๊ธฐ๋ก ์ฐพ๊ธฐํ’€์ดSELECT AO.ANIMAL_ID, AO.NAMEFROM ANIMAL_OUTS AO LEFT JOIN ANIMAL_INS AION AO.ANIMAL_ID = AI.ANIMAL_IDWHERE AI.ANIMAL_ID IS NULLORDER BY AO.ANIMAL_ID, AO.NAME;ANIMAL_OUTS์„ ๊ธฐ์ค€์œผ๋กœ ANIMAL_INS์— ๋Œ€ํ•œ ์ •๋ณด๊ฐ€ ์žˆ๋Š”์ง€ ์—†๋Š”์ง€ ์—ฌ๋ถ€๋ฅผ ํŒ๋‹จํ•˜๋ฉด ๋˜๋Š” ๋ฌธ์ œ์ž…๋‹ˆ๋‹ค. LEFT JOIN์„ ์‚ฌ์šฉํ•ด์„œ ๋ฌธ์ œ๋ฅผ ํ’€์—ˆ์Šต๋‹ˆ๋‹ค.
[ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค] ์กฐ๊ฑด์— ๋งž๋Š” ๋„์„œ์™€ ์ €์ž ๋ฆฌ์ŠคํŠธ ์ถœ๋ ฅํ•˜๊ธฐ | ์˜ค๋ผํด
ยท
๐Ÿ“ Coding Test/SQL
๋ฌธ์ œ์กฐ๊ฑด์— ๋งž๋Š” ๋„์„œ์™€ ์ €์ž ๋ฆฌ์ŠคํŠธ ์ถœ๋ ฅํ•˜๊ธฐํ’€์ดSELECT B.BOOK_ID, A.AUTHOR_NAME, TO_CHAR(B.PUBLISHED_DATE, 'YYYY-MM-DD') PUBLISHED_DATEFROM BOOK B JOIN AUTHOR AON B.AUTHOR_ID = A.AUTHOR_IDWHERE B.CATEGORY = '๊ฒฝ์ œ'ORDER BY B.PUBLISHED_DATE;JOIN์„ ์ด์šฉํ•ด ์‰ฝ๊ฒŒ ํ’€ ์ˆ˜ ์žˆ๋Š” ๊ฐ„๋‹จํ•œ ๋ฌธ์ œ์˜€์Šต๋‹ˆ๋‹ค.
[ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค] ์ƒํ’ˆ ๋ณ„ ์˜คํ”„๋ผ์ธ ๋งค์ถœ ๊ตฌํ•˜๊ธฐ | ์˜ค๋ผํด
ยท
๐Ÿ“ Coding Test/SQL
๋ฌธ์ œ์ƒํ’ˆ ๋ณ„ ์˜คํ”„๋ผ์ธ ๋งค์ถœ ๊ตฌํ•˜๊ธฐํ’€์ดSELECT P.PRODUCT_CODE, SUM(OS.SALES_AMOUNT * P.PRICE) AS SALES FROMOFFLINE_SALE OS JOIN PRODUCT PON OS.PRODUCT_ID = P.PRODUCT_IDGROUP BY PRODUCT_CODEORDER BY SALES DESC, PRODUCT_CODE;์ƒํ’ˆ์ฝ”๋“œ ๋ณ„ ๋งค์ถœ์•ก(ํŒ๋งค๊ฐ€ * ํŒ๋งค๋Ÿ‰) ํ•ฉ๊ณ„๋ฅผ ์ถœ๋ ฅํ•˜๋Š” ๋ฌธ์ œ์ž…๋‹ˆ๋‹ค.๋‘ ํ…Œ์ด๋ธ”์„ JOINํ•ด์ฃผ๊ณ , ์ƒํ’ˆ์ฝ”๋“œ ๋ณ„ => GROUP BY, ๋งค์ถœ์•ก(ํŒ๋งค๊ฐ€ * ํŒ๋งค๋Ÿ‰) ํ•ฉ๊ณ„ => SUM()์„ ์ด์šฉํ•˜์—ฌ ์ฟผ๋ฆฌ๋ฅผ ์ž‘์„ฑํ•˜์˜€์Šต๋‹ˆ๋‹ค.