๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
6๏ธโƒฃ SQL/Problem Solving

[MySQL] Programmers Lv.1 ์ „์ฒด ๋ฌธ์ œ ํ’€์ด

by seolhee2750 2022. 2. 2.
๋ชจ๋“  ๋ ˆ์ฝ”๋“œ ์กฐํšŒํ•˜๊ธฐ
SELECT *
FROM ANIMAL_INS
ORDER BY ANIMAL_ID

๐Ÿ– SQL ๋ฌธ์ œ์˜ ๊ฒฝ์šฐ ๋”ฑ ํ•˜๋‚˜์˜ ๋ฐ์ดํ„ฐ๋งŒ ์ถœ๋ ฅํ•˜๋Š” ๊ฒฝ์šฐ๋ฅผ ์ œ์™ธํ•˜๊ณ ๋Š” ORDER BY๋ฅผ ํ•„์ˆ˜๋กœ ์ƒ๊ฐํ•˜์ž.

 

์ด๋ฆ„์ด ์—†๋Š” ๋™๋ฌผ์˜ ์•„์ด๋””
SELECT ANIMAL_ID
FROM ANIMAL_INS
WHERE NAME IS NULL
ORDER BY ANIMAL_ID

๐Ÿ– NULL์„ ์ฒดํฌํ•  ๋•Œ๋Š” ๋น„๊ต ์—ฐ์‚ฐ์ž๋กœ IS ๋˜๋Š” IS NOT์„ ์‚ฌ์šฉ !

 

์ด๋ฆ„์ด ์žˆ๋Š” ๋™๋ฌผ์˜ ์•„์ด๋””
SELECT ANIMAL_ID
FROM ANIMAL_INS
WHERE NAME IS NOT NULL
ORDER BY ANIMAL_ID

 

์–ด๋ฆฐ ๋™๋ฌผ ์ฐพ๊ธฐ
SELECT ANIMAL_ID, NAME
FROM ANIMAL_INS
WHERE INTAKE_CONDITION != "Aged"
ORDER BY ANIMAL_ID

๐Ÿ– ๋ฌธ์ž์—ด์ด๋‚˜ ์ˆซ์ž ๋น„๊ต ์‹œ์—๋Š” !=, =, >=, >, ๋“ฑ์„ ๋น„๊ต ์—ฐ์‚ฐ์ž๋กœ ์‚ฌ์šฉํ•จ

 

์—ฌ๋Ÿฌ ๊ธฐ์ค€์œผ๋กœ ์ •๋ ฌํ•˜๊ธฐ
SELECT ANIMAL_ID, NAME, DATETIME
FROM ANIMAL_INS
ORDER BY NAME ASC, DATETIME DESC

๐Ÿ– ์šฐ์„ ์ ์œผ๋กœ ์ •๋ ฌํ•  ๋Œ€์ƒ์„ ์•ž์— ์“ฐ๊ณ , ์•ž์— ์žˆ๋Š” ๋Œ€์ƒ์ด ๊ฐ™์„ ๊ฒฝ์šฐ์— ๋’ค์— ์“ด ๋Œ€์ƒ์„ ๊ธฐ์ค€์œผ๋กœ ์ •๋ ฌํ•˜๊ฒŒ ํ•ด์ค€๋‹ค.

 

์ตœ๋Œ“๊ฐ’ ๊ตฌํ•˜๊ธฐ
SELECT MAX(DATETIME) AS "์‹œ๊ฐ„"
FROM ANIMAL_INS

๐Ÿ– MAX(), MIN(), COUNT(), HOUR() ๋“ฑ ํ•จ์ˆ˜ ๊ผญ ์•Œ๊ณ  ์žˆ๊ธฐ !

 

์—ญ์ˆœ ์ •๋ ฌํ•˜๊ธฐ
SELECT NAME, DATETIME
FROM ANIMAL_INS
ORDER BY ANIMAL_ID DESC

 

์•„ํ”ˆ ๋™๋ฌผ ์ฐพ๊ธฐ
SELECT ANIMAL_ID, NAME
FROM ANIMAL_INS
WHERE INTAKE_CONDITION = "Sick"
ORDER BY ANIMAL_ID

 

๋™๋ฌผ์˜ ์•„์ด๋””์™€ ์ด๋ฆ„
SELECT ANIMAL_ID, NAME
FROM ANIMAL_INS
ORDER BY ANIMAL_ID

 

์ƒ์œ„ n๊ฐœ ๋ ˆ์ฝ”๋“œ
SELECT NAME
FROM ANIMAL_INS
ORDER BY DATETIME
LIMIT 1

๐Ÿ– ์ •๋ ฌ ํ›„ LIMIT๋ฅผ ์จ์ฃผ๋ฉด ์ตœ์ƒ์œ„์˜ ๋ช‡ ๊ฐœ๋งŒ ์ถœ๋ ฅํ•  ๊ฒƒ์ธ์ง€ ์ •ํ•  ์ˆ˜ ์žˆ๋‹ค.

๋Œ“๊ธ€