๐ŸŒฑํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค SQL ๊ณ ๋“์  Kit [SELECT]

์œค๊ฑดํฌ(Guny)ยท2020๋…„ 10์›” 15์ผ
0

Database

๋ชฉ๋ก ๋ณด๊ธฐ
2/7
post-thumbnail

SQL ๊ณ ๋“์  Kit - SELECT

๐Ÿ“š์กฐ๊ฑด์— ๋งž๋Š” ๋ ˆ์ฝ”๋“œ๋ฅผ, ์›ํ•˜๋Š” ์ˆœ์„œ๋Œ€๋กœ. ๊ธฐ๋ณธ๊ธฐ๋ฅผ ์ฒ˜์Œ๋ถ€ํ„ฐ ํƒ„ํƒ„ํžˆ ๋‹ค์ ธ๋ณด์„ธ์š”.


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

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

SELECT *
FROM ANIMAL_INS
ORDER BY ANIMAL_ID;


โœ…์—ญ์ˆœ ์ •๋ ฌํ•˜๊ธฐ

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

SELECT NAME, DATETIME
FROM ANIMAL_INS
ORDER BY ANIMAL_ID desc;


โœ…์•„ํ”ˆ ๋™๋ฌผ ์ฐพ๊ธฐ

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

SELECT ANIMAL_ID, NAME
FROM ANIMAL_INS
WHERE INTAKE_CONDITION = 'Sick';


โœ…์–ด๋ฆฐ ๋™๋ฌผ ์ฐพ๊ธฐ

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

SELECT ANIMAL_ID, NAME
FROM ANIMAL_INS
WHERE NOT INTAKE_CONDITION = 'Aged'
ORDER BY ANIMAL_ID;


โœ…๋™๋ฌผ์˜ ์•„์ด๋””์™€ ์ด๋ฆ„

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

SELECT ANIMAL_ID, NAME
FROM ANIMAL_INS
ORDER BY ANIMAL_ID;


โœ…์—ฌ๋Ÿฌ ๊ธฐ์ค€์œผ๋กœ ์ •๋ ฌํ•˜๊ธฐ

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

SELECT ANIMAL_ID, NAME, DATETIME
FROM ANIMAL_INS
ORDER BY NAME, DATETIME DESC;


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

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

SELECT NAME
FROM ANIMAL_INS
ORDER BY DATETIME LIMIT 1

profile
๊ฐœ๋ฐœ์ž

0๊ฐœ์˜ ๋Œ“๊ธ€