๋ชฉ๋กAlgorithm/CodingTest - SQL (5)

suvera-dev ๐Ÿฅฆ

SQL) ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค SQL ๊ณ ๋“์  Kit - IS NULL

1.์ด๋ฆ„์ด ์—†๋Š” ๋™๋ฌผ์˜ ์•„์ด๋”” ๋™๋ฌผ ๋ณดํ˜ธ์†Œ์— ๋“ค์–ด์˜จ ๋™๋ฌผ ์ค‘, ์ด๋ฆ„์ด ์—†๋Š” ์ฑ„๋กœ ๋“ค์–ด์˜จ ๋™๋ฌผ์˜ ID๋ฅผ ์กฐํšŒํ•˜๋Š” SQL ๋ฌธ์„ ์ž‘์„ฑํ•ด์ฃผ์„ธ์š”. ๋‹จ, ID๋Š” ์˜ค๋ฆ„์ฐจ์ˆœ ์ •๋ ฌ๋˜์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. SELECT ANIMAL_ID FROM ANIMAL_INS WHERE NAME IS NULL ORDER BY ANIMAL_ID 2. ์ด๋ฆ„์ด ์žˆ๋Š” ๋™๋ฌผ์˜ ์•„์ด๋”” ๋™๋ฌผ ๋ณดํ˜ธ์†Œ์— ๋“ค์–ด์˜จ ๋™๋ฌผ ์ค‘, ์ด๋ฆ„์ด ์žˆ๋Š” ๋™๋ฌผ์˜ ID๋ฅผ ์กฐํšŒํ•˜๋Š” SQL ๋ฌธ์„ ์ž‘์„ฑํ•ด์ฃผ์„ธ์š”. ๋‹จ, ID๋Š” ์˜ค๋ฆ„์ฐจ์ˆœ ์ •๋ ฌ๋˜์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. SELECT ANIMAL_ID FROM ANIMAL_INS WHERE NAME IS NOT NULL ORDER BY ANIMAL_ID 3. NULL ์ฒ˜๋ฆฌํ•˜๊ธฐ ์ž…์–‘ ๊ฒŒ์‹œํŒ์— ๋™๋ฌผ ์ •๋ณด๋ฅผ ๊ฒŒ์‹œํ•˜๋ ค ํ•ฉ๋‹ˆ๋‹ค. ๋™๋ฌผ์˜ ์ƒ๋ฌผ ์ข…, ์ด๋ฆ„, ์„ฑ๋ณ„ ๋ฐ ์ค‘..

Algorithm/CodingTest - SQL 2022. 4. 15. 17:01
SQL) ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค SQL ๊ณ ๋“์  kit - GROUP BY

1. ๊ณ ์–‘์ด์™€ ๊ฐœ๋Š” ๋ช‡๋งˆ๋ฆฌ ์žˆ์„๊นŒ ๋™๋ฌผ ๋ณดํ˜ธ์†Œ์— ๋“ค์–ด์˜จ ๋™๋ฌผ ์ค‘ ๊ณ ์–‘์ด์™€ ๊ฐœ๊ฐ€ ๊ฐ๊ฐ ๋ช‡ ๋งˆ๋ฆฌ์ธ์ง€ ์กฐํšŒํ•˜๋Š” SQL๋ฌธ์„ ์ž‘์„ฑํ•ด์ฃผ์„ธ์š”. ์ด๋•Œ ๊ณ ์–‘์ด๋ฅผ ๊ฐœ๋ณด๋‹ค ๋จผ์ € ์กฐํšŒํ•ด์ฃผ์„ธ์š”. SELECT ANIMAL_TYPE , COUNT(ANIMAL_TYPE) FROM ANIMAL_INS GROUP BY ANIMAL_TYPE ORDER BY ANIMAL_TYPE 2. ๋™๋ช… ๋™๋ฌผ ์ˆ˜ ์ฐพ๊ธฐ ๋™๋ฌผ ๋ณดํ˜ธ์†Œ์— ๋“ค์–ด์˜จ ๋™๋ฌผ ์ด๋ฆ„ ์ค‘ ๋‘ ๋ฒˆ ์ด์ƒ ์“ฐ์ธ ์ด๋ฆ„๊ณผ ํ•ด๋‹น ์ด๋ฆ„์ด ์“ฐ์ธ ํšŸ์ˆ˜๋ฅผ ์กฐํšŒํ•˜๋Š” SQL๋ฌธ์„ ์ž‘์„ฑํ•ด์ฃผ์„ธ์š”. ์ด๋•Œ ๊ฒฐ๊ณผ๋Š” ์ด๋ฆ„์ด ์—†๋Š” ๋™๋ฌผ์€ ์ง‘๊ณ„์—์„œ ์ œ์™ธํ•˜๋ฉฐ, ๊ฒฐ๊ณผ๋Š” ์ด๋ฆ„ ์ˆœ์œผ๋กœ ์กฐํšŒํ•ด์ฃผ์„ธ์š”. - ์ด๋ฆ„์ด ์—†๋Š” ๋™๋ฌผ ์ œ์™ธ, ์ด๋ฆ„๋ณ„๋กœ ๊ทธ๋ฃน ๋ฌถ์–ด์ฃผ๊ณ , HAVING ์ ˆ์—์„œ 2๋ฒˆ ์ด์ƒ ์“ฐ์ธ๊ฑฐ ๊ฑธ๋Ÿฌ์ฃผ๊ธฐ. SELECT NAME, COUNT..

Algorithm/CodingTest - SQL 2022. 4. 15. 03:40
SQL) ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค SQL ๊ณ ๋“์  Kit - SUM, MIN, MAX

1. ์ตœ๋Œ“๊ฐ’ ๊ตฌํ•˜๊ธฐ ๊ฐ€์žฅ ์ตœ๊ทผ์— ๋“ค์–ด์˜จ ๋™๋ฌผ์€ ์–ธ์ œ ๋“ค์–ด์™”๋Š”์ง€ ์กฐํšŒํ•˜๋Š” SQL ๋ฌธ์„ ์ž‘์„ฑํ•ด์ฃผ์„ธ์š”. => ๊ฐ€์žฅ ์ตœ๊ทผ์— ๋“ค์–ด์™”์„ ๊ฒฝ์šฐ, Datetime์ด ์ œ์ผ ํผ SELECT MAX(DATETIME) FROM ANIMAL_INS LIMIT 1; 2. ์ตœ์†Ÿ๊ฐ’ ๊ตฌํ•˜๊ธฐ ๋™๋ฌผ ๋ณดํ˜ธ์†Œ์— ๊ฐ€์žฅ ๋จผ์ € ๋“ค์–ด์˜จ ๋™๋ฌผ์€ ์–ธ์ œ ๋“ค์–ด์™”๋Š”์ง€ ์กฐํšŒํ•˜๋Š” SQL ๋ฌธ์„ ์ž‘์„ฑํ•ด์ฃผ์„ธ์š”. SELECT MIN(DATETIME) FROM ANIMAL_INS LIMIT 1; 3. ๋™๋ฌผ ์ˆ˜ ๊ตฌํ•˜๊ธฐ ๋™๋ฌผ ๋ณดํ˜ธ์†Œ์— ๋™๋ฌผ์ด ๋ช‡ ๋งˆ๋ฆฌ ๋“ค์–ด์™”๋Š”์ง€ ์กฐํšŒํ•˜๋Š” SQL ๋ฌธ์„ ์ž‘์„ฑํ•ด์ฃผ์„ธ์š”. SELECT COUNT(*) FROM ANIMAL_INS 4. ์ค‘๋ณต ์ œ๊ฑฐํ•˜๊ธฐ ๋™๋ฌผ ๋ณดํ˜ธ์†Œ์— ๋“ค์–ด์˜จ ๋™๋ฌผ์˜ ์ด๋ฆ„์€ ๋ช‡ ๊ฐœ์ธ์ง€ ์กฐํšŒํ•˜๋Š” SQL ๋ฌธ์„ ์ž‘์„ฑํ•ด์ฃผ์„ธ์š”. ์ด๋•Œ ์ด๋ฆ„์ด NULL..

Algorithm/CodingTest - SQL 2022. 4. 15. 03:19