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

[SQL] Programmers : Level 1(1)

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

๐Ÿ“Œ ๋ชจ๋“  ๋ ˆ์ฝ”๋“œ ์กฐํšŒํ•˜๊ธฐ

 

์ฝ”๋”ฉํ…Œ์ŠคํŠธ ์—ฐ์Šต - ๋ชจ๋“  ๋ ˆ์ฝ”๋“œ ์กฐํšŒํ•˜๊ธฐ

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

programmers.co.kr

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

SELECT * FROM ANIMAL_INS ORDER BY ANIMAL_ID;

 

ORDER BY๋ฅผ ๊ทธ๋Œ€๋กœ ์ ์šฉํ•˜๋ฉด ์˜ค๋ฆ„์ฐจ์ˆœ์ด๋‹ค. - ASC ์ƒ๋žต

ORDER BY column ASC;

๋‚ด๋ฆผ์ฐจ์ˆœ ์ •๋ ฌ์€ DESC๋ฅผ ์ ์–ด์ค˜์•ผ ํ•œ๋‹ค.

ORDER BY column DESC;

 

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

 

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

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

programmers.co.kr

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

MAX(column) AS "String"
SELECT MAX(DATETIME) AS "์‹œ๊ฐ„"
FROM ANIMAL_INS;
LIMIT
SELECT DATETIME
FROM ANIMAL_INS
ORDER BY DATETIME DESC
LIMIT 1;

 

๐Ÿ“Œ ์—ญ์ˆœ ์ •๋ ฌํ•˜๊ธฐ

 

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

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

programmers.co.kr

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

SELECT NAME, DATETIME
FROM ANIMAL_INS
ORDER BY ANIMAL_ID DESC;

 

๐Ÿ“Œ ์•„ํ”ˆ ๋™๋ฌผ ์ฐพ๊ธฐ

 

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

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

programmers.co.kr

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

FROM ANIMAL_INS
WHERE INTAKE_CONDITION = "Sick"
ORDER BY ANIMAL_ID;

 

๐Ÿ“Œ ์–ด๋ฆฐ ๋™๋ฌผ ์ฐพ๊ธฐ

 

์ฝ”๋”ฉํ…Œ์ŠคํŠธ ์—ฐ์Šต - ์–ด๋ฆฐ ๋™๋ฌผ ์ฐพ๊ธฐ

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

programmers.co.kr

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

SELECT ANIMAL_ID, NAME
FROM ANIMAL_INS
WHERE INTAKE_CONDITION != "Aged"
ORDER BY ANIMAL_ID;

'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 2(1)  (0) 2021.03.10
[SQL] Programmers : Level 1(2)  (0) 2021.03.09

๋Œ“๊ธ€