Search

์ƒ์œ„ n๊ฐœ ๋ ˆ์ฝ”๋“œ

์ƒ์„ฑ์ผ
2022/03/03 16:11
ํ”Œ๋žซํผ
ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค
ํƒœ๊ทธ
SELECT
level
1
๋ถ„๋ฅ˜
๋‹ต ๋ณธ ๋ฌธ์ œ
๋งํฌ

Problem

๋ฌธ์ œ ์„ค๋ช…

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

์˜ˆ์‹œ

์˜ˆ๋ฅผ ๋“ค์–ดย ANIMAL_INSย ํ…Œ์ด๋ธ”์ด ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค๋ฉด
Search
ANIMAL_ID
ANIMAL_TYPE
DATETIME
INTAKE_CONDITION
NAME
SEX_UPON_INTAKE
Dog
2013/10/14 15:38
Normal
Jack
Neutered Male
Dog
2013/10/23 11:42
Normal
Disciple
Intact Male
Dog
2013/11/03 15:04
Normal
Katie
Spayed Female
Dog
2013/11/18 17:03
Normal
Anna
Spayed Female
์ด ์ค‘ ๊ฐ€์žฅ ๋ณดํ˜ธ์†Œ์— ๋จผ์ € ๋“ค์–ด์˜จ ๋™๋ฌผ์€ Jack์ž…๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ SQL๋ฌธ์„ ์‹คํ–‰ํ•˜๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๋‚˜์™€์•ผ ํ•ฉ๋‹ˆ๋‹ค.
NAME
Jack
โ€ป ๋ณดํ˜ธ์†Œ์— ๊ฐ€์žฅ ๋จผ์ € ๋“ค์–ด์˜จ ๋™๋ฌผ์€ ํ•œ ๋งˆ๋ฆฌ์ธ ๊ฒฝ์šฐ๋งŒ ํ…Œ์ŠคํŠธ ์ผ€์ด์Šค๋กœ ์ฃผ์–ด์ง‘๋‹ˆ๋‹ค.

Code

ย #1

Idea

๊ตฌํ•ด์•ผํ•˜๋Š” ๊ฒƒ : ๋™๋ฌผ๋ณดํ˜ธ์†Œ์— ๊ฐ€์žฅ ๋จผ์ € ๋“ค์–ด์˜จ ๋™๋ฌผ ์ด๋ฆ„
DATETIME ์ด MIN ์ธ ๋™๋ฌผ์ฐพ์•„์•ผํ•จ

ย Code

SELECT NAME FROM ANIMAL_INS WHERE DATETIME = (SELECT MIN(DATETIME) FROM ANIMAL_INS);
SQL
๋ณต์‚ฌ

Solution

WHERE์ ˆ ์„œ๋ธŒ์ฟผ๋ฆฌ๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค.

Commentary

MIN ํ•จ์ˆ˜์™€ ์„œ๋ธŒ์ฟผ๋ฆฌ ๋ฌธ๋ฒ•์„ ๋ชฐ๋ž์Œ