๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค/์•Œ๊ณ ๋ฆฌ์ฆ˜ ํ’€์ด(SQL)

ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค_๋ณดํ˜ธ์†Œ์—์„œ ์ค‘์„ฑํ™”๋œ ๋™๋ฌผ

by carrot0911 2024. 12. 12.

๋ฌธ์ œ ์„ค๋ช…

https://school.programmers.co.kr/learn/courses/30/lessons/59045

 

ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค

SW๊ฐœ๋ฐœ์ž๋ฅผ ์œ„ํ•œ ํ‰๊ฐ€, ๊ต์œก, ์ฑ„์šฉ๊นŒ์ง€ Total Solution์„ ์ œ๊ณตํ•˜๋Š” ๊ฐœ๋ฐœ์ž ์„ฑ์žฅ์„ ์œ„ํ•œ ๋ฒ ์ด์Šค์บ ํ”„

programmers.co.kr

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

NAME TYPE NULLABLE
ANIMAL_ID VARCHAR(N) FALSE
ANIMAL_TYPE VARCHAR(N) FALSE
DATETIME DATETIME FALSE
INTAKE_CONDITION VARCHAR(N) FALSE
NAME VARCHAR(N) TRUE
SEX_UPON_INTAKE VARCHAR(N) FALSE

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

NAME TYPE NULLABLE
ANIMAL_ID VARCHAR(N) FALSE
ANIMAL_TYPE VARCHAR(N) FALSE
DATETIME DATETIME FALSE
NAME VARCHAR(N) TRUE
SEX_UPON_OUTCOME VARCHAR(N) FALSE

๋ฌธ์ œ

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

์˜ˆ์‹œ

์˜ˆ๋ฅผ ๋“ค์–ด. ANIMAL_INS ํ…Œ์ด๋ธ”๊ณผ ANIMAL_OUTS ํ…Œ์ด๋ธ”์ด ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค๋ฉด

ANIMAL_INS

ANIMAL
_ID
ANIMAL
_TYPE
DATETIME INTAKE
_CONDITION
NAME SEX_UPON
_INTAKE
A367438 Dog 2015-09-10
16:01:00
Normal Cookie Spayed Female
A382192 Dog 2015-03-13
13:14:00
Normal Maxwell 2 Intact Male
A405494 Dog 2014-05-16
14:17:00
Normal Kaila Spayed Female
A410330 Dog 2016-09-11
14:09:00
Sick Chewy Intact Female

ANIMAL_OUTS

ANIMAL
_ID
ANIMAL
_TYPE
DATETIME NAME SEX_UPON
_OUTCOME
A367438 Dog 2015-09-12 13:30:00 Cookie Spayed Female
A382192 Dog 2015-03-16 13:46:00 Maxwell 2 Neutered Male
A405494 Dog 2014-05-20 11:44:00 Kaila Spayed Female
A410330 Dog 2016-09-13 13:46:00 Chewy Spayed Female
  • Cookie๋Š” ๋ณดํ˜ธ์†Œ์— ๋“ค์–ด์˜ฌ ๋‹น์‹œ์— ์ด๋ฏธ ์ค‘์„ฑํ™”๋˜์–ด ์žˆ์—ˆ์Šต๋‹ˆ๋‹ค.
  • Maxwell 2๋Š” ๋ณดํ˜ธ์†Œ์— ๋“ค์–ด์˜จ ํ›„ ์ค‘์„ฑํ™”๋˜์—ˆ์Šต๋‹ˆ๋‹ค.
  • Kaila๋Š” ๋ณดํ˜ธ์†Œ์— ๋“ค์–ด์˜ฌ ๋‹น์‹œ์— ์ด๋ฏธ ์ค‘์„ฑํ™”๋˜์—ˆ์Šต๋‹ˆ๋‹ค.
  • Chewy๋Š” ๋ณดํ˜ธ์†Œ์— ๋“ค์–ด์˜จ ํ›„ ์ค‘์„ฑํ™”๋˜์—ˆ์Šต๋‹ˆ๋‹ค.

๋”ฐ๋ผ์„œ SQL๋ฌธ์„ ์‹คํ–‰ํ•˜๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๋‚˜์™€์•ผ ํ•ฉ๋‹ˆ๋‹ค.

ANIMAL_ID ANIMAL_TYPE NAME
A382192 Dog Maxwell 2
A410330 Dog Chewy

 

๋‚ด๊ฐ€ ์ž‘์„ฑํ•œ ์ฝ”๋“œ

SELECT AI.ANIMAL_ID, AI.ANIMAL_TYPE, AI.NAME
FROM ANIMAL_INS AI INNER JOIN ANIMAL_OUTS AO
ON AI.ANIMAL_ID = AO.ANIMAL_ID
WHERE AI.SEX_UPON_INTAKE LIKE "Intact%" AND (AO.SEX_UPON_OUTCOME LIKE "Spayed%" OR AO.SEX_UPON_OUTCOME LIKE "Neutered%")
ORDER BY AI.ANIMAL_ID;

์ฝ”๋“œ ์„ค๋ช…

  • SELECT AI.ANIMAL_ID, AI.ANIMAL_TYPE, AI.NAME : ANIMAL_INS ํ…Œ์ด๋ธ”์˜ ANIMAL_ID, ANIMAL_TYPE, NAME ์ปฌ๋Ÿผ์„ ์„ ํƒํ•œ๋‹ค.
  • FROM ANIMAL_INS AI INNER JOIN ANIMAL_OUTS AO : ANIMAL_INS๋ฅผ AI๋กœ, ANIMAL_OUTS๋ฅผ AO๋กœ ์„ ์–ธํ•˜๊ณ  INNER JOIN์„ ์ง„ํ–‰ํ•œ๋‹ค.
  • ON AI.ANIMAL_ID = AO.ANIMAL_ID : INNER JOIN์˜ ์กฐ๊ฑด์€ AI.ANIMAL_ID์™€ AO.ANIMAL_ID๊ฐ€ ๊ฐ™์€ ๋ฐ์ดํ„ฐ๋“ค์ด๋‹ค.
  • WHERE AI.SEX_UPON_INTAKE LIKE "Intact%" AND (AO.SEX_UPON_OUTCOME LIKE "Spayed%" OR AO.SEX_UPON_OUTCOME LIKE "Neutered%") : AI.SEX_UPON_INTAKE๊ฐ€ "Intact%" ํ˜•์‹์œผ๋กœ ๋˜์–ด ์žˆ๊ณ , AO.SEX_UPON_OUTCOME์ด "Spayed%" ๋˜๋Š” "Neutered%" ํ˜•์‹์œผ๋กœ ๋˜์–ด ์žˆ๋Š” ๋ฐ์ดํ„ฐ๋งŒ ๊ฐ€์ ธ์˜จ๋‹ค.
  • ORDER BY AI.ANIMAL_ID : AI.ANIMAL_ID๋ฅผ ๊ธฐ์ค€์œผ๋กœ ์˜ค๋ฆ„์ฐจ์ˆœ ์ •๋ ฌํ•œ๋‹ค.