๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
SQL

[SQL] Programmers : Level 2(1)

by ํฌ๊ตฌ๋ฆฌ 2021. 3. 10.

๐Ÿ“Œ ์ตœ์†Ÿ๊ฐ’ ๊ตฌํ•˜๊ธฐ

 

์ฝ”๋”ฉํ…Œ์ŠคํŠธ ์—ฐ์Šต - ์ตœ์†Ÿ๊ฐ’ ๊ตฌํ•˜๊ธฐ

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

programmers.co.kr

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

SELECT MIN(DATETIME) AS "์‹œ๊ฐ„"
FROM ANIMAL_INS;

 

๐Ÿ“Œ ๋™๋ฌผ ์ˆ˜ ๊ตฌํ•˜๊ธฐ

 

์ฝ”๋”ฉํ…Œ์ŠคํŠธ ์—ฐ์Šต - ๋™๋ฌผ ์ˆ˜ ๊ตฌํ•˜๊ธฐ

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

programmers.co.kr

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

SELECT COUNT(*) AS "count"
FROM ANIMAL_INS;

 

๐Ÿ“Œ ์ค‘๋ณต ์ œ๊ฑฐํ•˜๊ธฐ

 

์ฝ”๋”ฉํ…Œ์ŠคํŠธ ์—ฐ์Šต - ์ค‘๋ณต ์ œ๊ฑฐํ•˜๊ธฐ

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

programmers.co.kr

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

SELECT COUNT(DISTINCT NAME) AS "count"
FROM ANIMAL_INS
WHERE NAME IS NOT NULL;
  • COUNT ํ•˜๋ ค๋Š” column์—์„œ ์ค‘๋ณต์„ ์ œ๊ฑฐํ•˜๋ ค๋ฉด COUNT(DISTINCT column) ์œผ๋กœ ํ•  ์ˆ˜ ์žˆ๋‹ค!

 

๐Ÿ“Œ ๊ณ ์–‘์ด์™€ ๊ฐœ๋Š” ๋ช‡ ๋งˆ๋ฆฌ ์žˆ์„๊นŒ

 

์ฝ”๋”ฉํ…Œ์ŠคํŠธ ์—ฐ์Šต - ๊ณ ์–‘์ด์™€ ๊ฐœ๋Š” ๋ช‡ ๋งˆ๋ฆฌ ์žˆ์„๊นŒ

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

programmers.co.kr

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

SELECT ANIMAL_TYPE, COUNT(*) AS "count"
FROM ANIMAL_INS
GROUP BY ANIMAL_TYPE
ORDER BY ANIMAL_TYPE;
  • GROUP BY : ์ปฌ๋Ÿผ ๊ฐ’(ANIMAL_TYPE)์ด ๊ฐ™์€ ๊ฒƒ ๋ผ๋ฆฌ ํ•˜๋‚˜๋กœ ๋ฌถ์–ด์ค€๋‹ค.

 

๐Ÿ“Œ NULL ์ฒ˜๋ฆฌํ•˜๊ธฐ

 

์ฝ”๋”ฉํ…Œ์ŠคํŠธ ์—ฐ์Šต - NULL ์ฒ˜๋ฆฌํ•˜๊ธฐ

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

programmers.co.kr

์ž…์–‘ ๊ฒŒ์‹œํŒ์— ๋™๋ฌผ ์ •๋ณด๋ฅผ ๊ฒŒ์‹œํ•˜๋ ค ํ•ฉ๋‹ˆ๋‹ค.

๋™๋ฌผ์˜ ์ƒ๋ฌผ ์ข…, ์ด๋ฆ„, ์„ฑ๋ณ„ ๋ฐ ์ค‘์„ฑํ™” ์—ฌ๋ถ€๋ฅผ ์•„์ด๋”” ์ˆœ์œผ๋กœ ์กฐํšŒํ•˜๋Š” SQL๋ฌธ์„ ์ž‘์„ฑํ•ด์ฃผ์„ธ์š”.

์ด๋•Œ ํ”„๋กœ๊ทธ๋ž˜๋ฐ์„ ๋ชจ๋ฅด๋Š” ์‚ฌ๋žŒ๋“ค์€ NULL์ด๋ผ๋Š” ๊ธฐํ˜ธ๋ฅผ ๋ชจ๋ฅด๊ธฐ ๋•Œ๋ฌธ์—, ์ด๋ฆ„์ด ์—†๋Š” ๋™๋ฌผ์˜ ์ด๋ฆ„์€ "No name"์œผ๋กœ ํ‘œ์‹œํ•ด ์ฃผ์„ธ์š”.

SELECT ANIMAL_TYPE, IFNULL(NAME, "No name") AS NAME, SEX_UPON_INTAKE
FROM ANIMAL_INS
ORDER BY ANIMAL_ID;
  • IFNULL(์ปฌ๋Ÿผ๋ช…, NULL์ผ ๊ฒฝ์šฐ ์ž…๋ ฅํ•  ๊ฐ’)

 

๐Ÿ“Œ ๋™๋ช… ๋™๋ฌผ ์ˆ˜ ์ฐพ๊ธฐ

 

์ฝ”๋”ฉํ…Œ์ŠคํŠธ ์—ฐ์Šต - ๋™๋ช… ๋™๋ฌผ ์ˆ˜ ์ฐพ๊ธฐ

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

programmers.co.kr

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

SELECT NAME, COUNT(*) AS "COUNT"
FROM ANIMAL_INS
WHERE NAME IS NOT NULL
GROUP BY NAME
HAVING COUNT(NAME) > 1
ORDER BY NAME;

 

'SQL' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

[SQL] Programmers : Level 4  (0) 2021.03.26
[SQL] Programmers : Level 3  (0) 2021.03.25
[SQL] Programmers : Level 2(2)  (0) 2021.03.10
[SQL] Programmers : Level 1(2)  (0) 2021.03.09
[SQL] Programmers : Level 1(1)  (0) 2021.03.08

๋Œ“๊ธ€