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

[SQL] Programmers : Level 3

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

๐Ÿ“Œ ์—†์–ด์ง„ ๊ธฐ๋ก ์ฐพ๊ธฐ

 

์ฝ”๋”ฉํ…Œ์ŠคํŠธ ์—ฐ์Šต - ์—†์–ด์ง„ ๊ธฐ๋ก ์ฐพ๊ธฐ

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

programmers.co.kr

์ฒœ์žฌ์ง€๋ณ€์œผ๋กœ ์ธํ•ด ์ผ๋ถ€ ๋ฐ์ดํ„ฐ๊ฐ€ ์œ ์‹ค๋˜์—ˆ์Šต๋‹ˆ๋‹ค. ์ž…์–‘์„ ๊ฐ„ ๊ธฐ๋ก์€ ์žˆ๋Š”๋ฐ, ๋ณดํ˜ธ์†Œ์— ๋“ค์–ด์˜จ ๊ธฐ๋ก์ด ์—†๋Š” ๋™๋ฌผ์˜ ID์™€ ์ด๋ฆ„์„ ID ์ˆœ์œผ๋กœ ์กฐํšŒํ•˜๋Š” SQL๋ฌธ์„ ์ž‘์„ฑํ•ด์ฃผ์„ธ์š”.

 

ON INS.ANIMAL_ID = OUTS.ANIMAL_ID ๋Œ€์‹  USING (ANIMAL_ID)๋„ ๊ฐ€๋Šฅ!
SELECT OUTS.ANIMAL_ID, OUTS.NAME
FROM ANIMAL_OUTS AS OUTS
LEFT JOIN ANIMAL_INS AS INS
ON INS.ANIMAL_ID = OUTS.ANIMAL_ID
WHERE INS.DATETIME IS NULL
ORDER BY OUTS.ANIMAL_ID

 

 

๐Ÿ“Œ ์žˆ์—ˆ๋Š”๋ฐ์š” ์—†์—ˆ์Šต๋‹ˆ๋‹ค

 

์ฝ”๋”ฉํ…Œ์ŠคํŠธ ์—ฐ์Šต - ์žˆ์—ˆ๋Š”๋ฐ์š” ์—†์—ˆ์Šต๋‹ˆ๋‹ค

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

programmers.co.kr

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

 

SELECT OUTS.ANIMAL_ID AS ANIMAL_ID, OUTS.NAME AS NAME
FROM ANIMAL_OUTS AS OUTS
LEFT JOIN ANIMAL_INS AS INS
ON INS.ANIMAL_ID = OUTS.ANIMAL_ID
WHERE INS.DATETIME > OUTS.DATETIME
ORDER BY INS.DATETIME;

 

 

๐Ÿ“Œ ์˜ค๋žœ ๊ธฐ๊ฐ„ ๋ณดํ˜ธํ•œ ๋™๋ฌผ(1)

 

์ฝ”๋”ฉํ…Œ์ŠคํŠธ ์—ฐ์Šต - ์˜ค๋žœ ๊ธฐ๊ฐ„ ๋ณดํ˜ธํ•œ ๋™๋ฌผ(1)

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

programmers.co.kr

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

 

 

SELECT INS.NAME, INS.DATETIME
FROM ANIMAL_INS AS INS
LEFT JOIN ANIMAL_OUTS AS OUTS
USING (ANIMAL_ID)
WHERE OUTS.DATETIME IS NULL
ORDER BY INS.DATETIME
LIMIT 3;

 

 

๐Ÿ“Œ ์˜ค๋žœ ๊ธฐ๊ฐ„ ๋ณดํ˜ธํ•œ ๋™๋ฌผ(2)

 

์ฝ”๋”ฉํ…Œ์ŠคํŠธ ์—ฐ์Šต - ์˜ค๋žœ ๊ธฐ๊ฐ„ ๋ณดํ˜ธํ•œ ๋™๋ฌผ(2)

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

programmers.co.kr

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

 

SELECT INS.ANIMAL_ID, INS.NAME
FROM ANIMAL_INS AS INS
LEFT JOIN ANIMAL_OUTS AS OUTS
USING (ANIMAL_ID)
WHERE OUTS.DATETIME IS NOT NULL
ORDER BY OUTS.DATETIME - INS.DATETIME DESC
LIMIT 2;

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

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

๋Œ“๊ธ€