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

ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค_์žˆ์—ˆ๋Š”๋ฐ์š” ์—†์—ˆ์Šต๋‹ˆ๋‹ค

by carrot0911 2024. 11. 21.

๋ฌธ์ œ ์„ค๋ช…

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

 

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

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_ID, ANIMAL_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

 

๋ฌธ์ œ

๊ด€๋ฆฌ์ž์˜ ์‹ค์ˆ˜๋กœ ์ผ๋ถ€ ๋™๋ฌผ์˜ ์ž…์–‘์ผ์ด ์ž˜๋ชป ์ž…๋ ฅ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. ๋ณดํ˜ธ ์‹œ์ž‘์ผ๋ณด๋‹ค ์ž…์–‘์ผ์ด ๋” ๋น ๋ฅธ ๋™๋ฌผ์˜ ์•„์ด๋””์™€ ์ด๋ฆ„์„ ์กฐํšŒํ•˜๋Š” SQL๋ฌธ์„ ์ž‘์„ฑํ•ด ์ฃผ์„ธ์š”. ์ด๋•Œ ๊ฒฐ๊ณผ๋Š” ๋ณดํ˜ธ ์‹œ์ž‘์ผ์ด ๋น ๋ฅธ ์ˆœ์œผ๋กœ ์กฐํšŒํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.

์˜ˆ์‹œ

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

ANIMAL_INS 

ANIMAL
_ID
ANIMAL
_TYPE
DATETIME INTAKE
_CONDITION
NAME SEX_UPON
_INTAKE
A350276 Cat 2017-08-13
13:50:00
Normal Jewel Spayed Female
A381217 Dog 2017-07-08
09:41:00
Sick Cherokee Neutered Male

ANIMAL_OUTS 

ANIMAL_ID ANIMAL_TYPE DATETIME NAME SEX_UPON_OUTCOME
A350276 Cat 2018-01-28
17:51:00
Jewel Spayed Female
A381217 Dog 2017-06-09
18:51:00
Cherokee Neutered Male

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

ANIMAL_ID NAME
A381217 Cherokee

 

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

SELECT AI.ANIMAL_ID, AI.NAME
FROM ANIMAL_INS AI INNER JOIN ANIMAL_OUTS AO
ON AI.ANIMAL_ID=AO.ANIMAL_ID
WHERE AI.DATETIME > AO.DATETIME
ORDER BY AI.DATETIME;

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

  • SELECT AI.ANIMAL_ID, AI,NAME : ANIMAL_INS ํ…Œ์ด๋ธ”์˜ ANIMAL_ID ์ปฌ๋Ÿผ๊ณผ 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 : ANIMAL_INS ํ…Œ์ด๋ธ”์˜ ANIMAL_ID ์ปฌ๋Ÿผ๊ณผ ANIMAL_OUTS ํ…Œ์ด๋ธ”์˜ ANIMAL_ID๊ฐ€ ๊ฐ™์€ ๋ฐ์ดํ„ฐ๋งŒ ๊ฐ€์ ธ์˜จ๋‹ค.
  • WHERE AI.DATETIME > AO.DATETIME : ANIMAL_INS ํ…Œ์ด๋ธ”์˜ DATEIME ์ปฌ๋Ÿผ์˜ ๋ฐ์ดํ„ฐ๊ฐ€ ANIMAL_OUTS ํ…Œ์ด๋ธ”์˜ DATETIME ์ปฌ๋Ÿผ์˜ ๋ฐ์ดํ„ฐ๋ณด๋‹ค ํด ๊ฒฝ์šฐ์—๋งŒ ๋ฐ์ดํ„ฐ๋ฅผ ๊ฐ€์ ธ์˜จ๋‹ค.
  • ORDER BY AI.DATETIME : ANIMAL_INS ํ…Œ์ด๋ธ”์˜ DATETIME ์ปฌ๋Ÿผ์„ ๊ธฐ์ค€์œผ๋กœ ์˜ค๋ฆ„์ฐจ์ˆœ ์ •๋ ฌํ•œ๋‹ค.