-- ์ฝ๋๋ฅผ ์ ๋ ฅํ์ธ์
SELECT * FROM ANIMAL_INS ORDER BY ANIMAL_ID;
2. ์ญ์ ์ ๋ ฌํ๊ธฐ
-- ์ฝ๋๋ฅผ ์ ๋ ฅํ์ธ์
SELECT NAME, DATETIME FROM ANIMAL_INS ORDER BY ANIMAL_ID DESC;
3. ์ํ ๋๋ฌผ ์ฐพ๊ธฐ
-- ์ฝ๋๋ฅผ ์ ๋ ฅํ์ธ์
SELECT ANIMAL_ID, NAME FROM ANIMAL_INS WHERE ANIMAL_INS.INTAKE_CONDITION = 'Sick';
4. ์ด๋ฆฐ ๋๋ฌผ ์ฐพ๊ธฐ
-- ์ฝ๋๋ฅผ ์ ๋ ฅํ์ธ์
SELECT ANIMAL_ID, NAME FROM ANIMAL_INS WHERE INTAKE_CONDITION != 'Aged' ORDER BY ANIMAL_ID;
5. ๋๋ฌผ์ ์์ด๋์ ์ด๋ฆ
-- ์ฝ๋๋ฅผ ์ ๋ ฅํ์ธ์
SELECT ANIMAL_ID, NAME FROM ANIMAL_INS ORDER BY ANIMAL_ID;
6. ์ฌ๋ฌ ๊ธฐ์ค์ผ๋ก ์ ๋ ฌํ๊ธฐ
-- ์ฝ๋๋ฅผ ์ ๋ ฅํ์ธ์
SELECT ANIMAL_ID, NAME, DATETIME FROM ANIMAL_INS ORDER BY NAME, DATETIME DESC;
7. ์์ n๊ฐ ๋ ์ฝ๋
-- ์ฝ๋๋ฅผ ์ ๋ ฅํ์ธ์
SELECT NAME FROM ANIMAL_INS ORDER BY DATETIME LIMIT 1;
SUM, MAX, MIN(4๊ฐ)
1. ์ต๋๊ฐ ๊ตฌํ๊ธฐ
-- ์ฝ๋๋ฅผ ์ ๋ ฅํ์ธ์
SELECT DATETIME AS ์๊ฐ FROM ANIMAL_INS ORDER BY DATETIME DESC LIMIT 1;
2. ์ต์๊ฐ ๊ตฌํ๊ธฐ
-- ์ฝ๋๋ฅผ ์ ๋ ฅํ์ธ์
SELECT DATETIME AS ์๊ฐ FROM ANIMAL_INS ORDER BY DATETIME LIMIT 1;
3. ๋๋ฌผ ์ ๊ตฌํ๊ธฐ
-- ์ฝ๋๋ฅผ ์ ๋ ฅํ์ธ์
SELECT COUNT(*) AS count FROM ANIMAL_INS;
4. ์ค๋ณต ์ ๊ฑฐํ๊ธฐ
-- ์ฝ๋๋ฅผ ์ ๋ ฅํ์ธ์
SELECT COUNT(DISTINCT NAME) FROM ANIMAL_INS WHERE NAME is not null;
GROUP BY(4๊ฐ)
1. ๊ณ ์์ด์ ๊ฐ๋ ๋ช ๋ง๋ฆฌ ์์๊น
-- ์ฝ๋๋ฅผ ์ ๋ ฅํ์ธ์
SELECT ANIMAL_TYPE, COUNT(ANIMAL_TYPE) FROM ANIMAL_INS GROUP BY ANIMAL_TYPE ORDER BY ANIMAL_TYPE;
2. ๋๋ช ๋๋ฌผ ์ ์ฐพ๊ธฐ
-- ์ฝ๋๋ฅผ ์ ๋ ฅํ์ธ์
SELECT NAME, COUNT(NAME) FROM ANIMAL_INS WHERE NAME is not null
GROUP BY NAME HAVING COUNT(NAME)>=2 ORDER BY NAME;
3. ์ ์ ์๊ฐ ๊ตฌํ๊ธฐ(1)
-- ์ฝ๋๋ฅผ ์ ๋ ฅํ์ธ์
SELECT HOUR(DATETIME), COUNT(HOUR(DATETIME)) FROM ANIMAL_OUTS
WHERE HOUR(DATETIME)>=9 AND HOUR(DATETIME)<=19
GROUP BY HOUR(DATETIME)
ORDER BY HOUR(DATETIME)
4. ์ ์ ์๊ฐ ๊ตฌํ๊ธฐ(2)
-- ์ฝ๋๋ฅผ ์ ๋ ฅํ์ธ์
SET @HOUR:=-1;
SELECT @HOUR:= @HOUR+1, (SELECT COUNT(HOUR(DATETIME)) FROM ANIMAL_OUTS WHERE @HOUR = HOUR(DATETIME))
FROM ANIMAL_OUTS
WHERE @HOUR<23
IS NULL(3๊ฐ)
1. ์ด๋ฆ์ด ์๋ ๋๋ฌผ์ ์์ด๋
-- ์ฝ๋๋ฅผ ์ ๋ ฅํ์ธ์
SELECT ANIMAL_ID FROM ANIMAL_INS WHERE NAME is null ORDER BY ANIMAL_ID;
2. ์ด๋ฆ์ด ์๋ ๋๋ฌผ์ ์์ด๋
-- ์ฝ๋๋ฅผ ์ ๋ ฅํ์ธ์
SELECT ANIMAL_ID FROM ANIMAL_INS WHERE NAME is not null ORDER BY ANIMAL_ID;
3. NULL ์ฒ๋ฆฌํ๊ธฐ
-- ์ฝ๋๋ฅผ ์ ๋ ฅํ์ธ์
SELECT ANIMAL_TYPE, IFNULL(NAME,'No name'), SEX_UPON_INTAKE FROM ANIMAL_INS
ORDER BY ANIMAL_ID;
JOIN(4๊ฐ)
1. ์์ด์ง ๊ธฐ๋ก ์ฐพ๊ธฐ
-- ์ฝ๋๋ฅผ ์ ๋ ฅํ์ธ์
SELECT OUTS.ANIMAL_ID, OUTS.NAME FROM ANIMAL_OUTS OUTS
LEFT OUTER JOIN ANIMAL_INS INS ON OUTS.ANIMAL_ID = INS.ANIMAL_ID
WHERE INS.ANIMAL_ID is null
ORDER BY OUTS.ANIMAL_ID;
2. ์์๋๋ฐ์ ์์์ต๋๋ค
-- ์ฝ๋๋ฅผ ์ ๋ ฅํ์ธ์
SELECT OUTS.ANIMAL_ID, OUTS.NAME FROM ANIMAL_OUTS OUTS
JOIN ANIMAL_INS INS ON OUTS.ANIMAL_ID = INS.ANIMAL_ID
WHERE OUTS.DATETIME<INS.DATETIME
ORDER BY INS.DATETIME;
3. ์ค๋ ๊ธฐ๊ฐ ๋ณดํธํ ๋๋ฌผ(1)
-- ์ฝ๋๋ฅผ ์ ๋ ฅํ์ธ์
SELECT INS.NAME, INS.DATETIME FROM ANIMAL_INS INS
LEFT OUTER JOIN ANIMAL_OUTS OUTS ON INS.ANIMAL_ID = OUTS.ANIMAL_ID
WHERE OUTS.ANIMAL_ID is null
ORDER BY INS.DATETIME
LIMIT 3;
4. ๋ณดํธ์์์ ์ค์ฑํํ ๋๋ฌผ
-- ์ฝ๋๋ฅผ ์ ๋ ฅํ์ธ์
SELECT INS.ANIMAL_ID, INS.ANIMAL_TYPE, INS.NAME FROM ANIMAL_INS INS
JOIN ANIMAL_OUTS OUTS ON INS.ANIMAL_ID = OUTS.ANIMAL_ID
WHERE INS.SEX_UPON_INTAKE != OUTS.SEX_UPON_OUTCOME
ORDER BY OUTS.ANIMAL_ID;
String, Date(5๊ฐ)
1. ๋ฃจ์์ ์๋ผ ์ฐพ๊ธฐ
-- ์ฝ๋๋ฅผ ์ ๋ ฅํ์ธ์
SELECT ANIMAL_ID, NAME, SEX_UPON_INTAKE FROM ANIMAL_INS
WHERE NAME IN ('LUCY', 'Ella', 'Pickle', 'Rogan', 'Sabrina', 'Mitty')
ORDER BY ANIMAL_ID;
2. ์ด๋ฆ์ el์ด ๋ค์ด๊ฐ๋ ๋๋ฌผ ์ฐพ๊ธฐ
-- ์ฝ๋๋ฅผ ์ ๋ ฅํ์ธ์
SELECT ANIMAL_ID, NAME FROM ANIMAL_INS
WHERE ANIMAL_TYPE = 'Dog' AND NAME LIKE('%el%')
ORDER BY NAME;
3. ์ค์ฑํ ์ฌ๋ถ ํ์ ํ๊ธฐ
-- ์ฝ๋๋ฅผ ์ ๋ ฅํ์ธ์
SELECT ANIMAL_ID, NAME,
IF(SEX_UPON_INTAKE LIKE('%Neutered%') OR SEX_UPON_INTAKE LIKE('%Spayed%'),'O','X')
FROM ANIMAL_INS
ORDER BY ANIMAL_ID;
4. ์ค๋ ๊ธฐ๊ฐ ๋ณดํธํ ๋๋ฌผ(2)
-- ์ฝ๋๋ฅผ ์ ๋ ฅํ์ธ์
SELECT OUTS.ANIMAL_ID, OUTS.NAME FROM ANIMAL_OUTS OUTS
JOIN ANIMAL_INS INS ON OUTS.ANIMAL_ID = INS.ANIMAL_ID
ORDER BY OUTS.DATETIME - INS.DATETIME DESC
LIMIT 2;
5. DATETIME์์ DATE๋ก ํ ๋ณํ
-- ์ฝ๋๋ฅผ ์ ๋ ฅํ์ธ์
SELECT ANIMAL_ID, NAME, DATE_FORMAT(DATETIME, '%Y-%m-%d') AS ๋ ์ง FROM ANIMAL_INS
ORDER BY ANIMAL_ID;
๋๊ธ