250x250
Link
๋‚˜์˜ GitHub Contribution ๊ทธ๋ž˜ํ”„
Loading data ...
Notice
Recent Posts
Recent Comments
๊ด€๋ฆฌ ๋ฉ”๋‰ด

Data Science LAB

[MySQL]ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค ์ฝ”๋”ฉํ…Œ์ŠคํŠธ ์—ฐ์Šต 8 ๋ณธ๋ฌธ

๐Ÿ“ Coding Test/MySQL

[MySQL]ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค ์ฝ”๋”ฉํ…Œ์ŠคํŠธ ์—ฐ์Šต 8

ใ…… ใ…œ ใ…” ใ…‡ 2022. 2. 14. 11:44
728x90

๋“œ๋””์–ด ์˜ค๋Š˜ ๋ถ€ํ„ฐ๋Š” LEVEL 2๋ฌธ์ œ๋ฅผ ํ’€์–ด๋ณด๋ ค๊ณ  ํ•œ๋‹ค!

 

 

 

๋ฌธ์ œ
ANIMAL_INS ํ…Œ์ด๋ธ”์€ ๋™๋ฌผ ๋ณดํ˜ธ์†Œ์— ๋“ค์–ด์˜จ ๋™๋ฌผ์˜ ์ •๋ณด๋ฅผ ๋‹ด์€ ํ…Œ์ด๋ธ”์ž…๋‹ˆ๋‹ค. 
ANIMAL_INS ํ…Œ์ด๋ธ” ๊ตฌ์กฐ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์œผ๋ฉฐ, 
ANIMAL_ID, ANIMAL_TYPE, DATETIME, INTAKE_CONDITION, NAME, SEX_UPON_INTAKE
๋Š” ๊ฐ๊ฐ ๋™๋ฌผ์˜ ์•„์ด๋””, ์ƒ๋ฌผ ์ข…, ๋ณดํ˜ธ ์‹œ์ž‘์ผ, ๋ณดํ˜ธ ์‹œ์ž‘ ์‹œ ์ƒํƒœ, ์ด๋ฆ„, ์„ฑ๋ณ„ ๋ฐ ์ค‘์„ฑํ™” ์—ฌ๋ถ€๋ฅผ ๋‚˜ํƒ€๋ƒ…๋‹ˆ๋‹ค.

๋™๋ฌผ ๋ณดํ˜ธ์†Œ์— ๋“ค์–ด์˜จ ๋™๋ฌผ ์ค‘ ๊ณ ์–‘์ด์™€ ๊ฐœ๊ฐ€ ๊ฐ๊ฐ ๋ช‡ ๋งˆ๋ฆฌ์ธ์ง€ ์กฐํšŒํ•˜๋Š” SQL๋ฌธ์„ ์ž‘์„ฑํ•ด์ฃผ์„ธ์š”. ์ด๋•Œ ๊ณ ์–‘์ด๋ฅผ ๊ฐœ๋ณด๋‹ค ๋จผ์ € ์กฐํšŒํ•ด์ฃผ์„ธ์š”.

 

 

 

 

 

์˜ˆ์‹œ

 

 

 

 

 

๋ฌธ์ œ ํ’€์ด
SELECT ANIMAL_TYPE, COUNT(*) AS count
FROM ANIMAL_INS
GROUP BY ANIMAL_TYPE
HAVING ANIMAL_TYPE IN ('Cat','Dog')
ORDER BY ANIMAL_TYPE ASC

 

1. MySQL์—์„œ ๋ฐ์ดํ„ฐ์˜ ์ˆ˜๋ฅผ ์„ธ์ฃผ๋Š” ํ•จ์ˆ˜๋Š” COUNT(์ปฌ๋Ÿผ๋ช…)์ด๋‹ค

  => ๋‹ค๋ฅธ ๋ฐ์ดํ„ฐ๋ฅผ ๋ชจ๋‘ ์„ธ์ค˜์•ผ ํ•˜๋ฏ€๋กœ COUNT(*)

    

 

2.  ๋™๋ฌผ ํƒ€์ž…๋ณ„๋กœ ์กฐํšŒํ•ด์•ผ ํ•˜๊ธฐ ๋•Œ๋ฌธ์— GROUP BY ANIMAL_TYPE

 

 

3. ๋™๋ฌผ ํƒ€์ž… ์ค‘ ๊ณ ์–‘์ด์™€ ๊ฐ•์•„์ง€๋งŒ ์กฐํšŒํ•ด์•ผ ํ•˜๊ธฐ ๋•Œ๋ฌธ์— HAVING ANIMAL_TYPE IN ('Cat','Dog')

 

 

4. ์˜ค๋ฆ„์ฐจ์ˆœ์œผ๋กœ ์ •๋ ฌ

 

 

 

 

728x90
Comments