๋ฌธ์ ํ์ด ๊ธฐ๊ฐ : 24๋ 12์ 24์ผ ~ 26์ผ
์ต์ ์์ผ๋ก ์ ๋ ฌํ ๋ฌธ์ ์์์ ๋๋ค
์์ด์ง ๊ธฐ๋ก ์ฐพ๊ธฐ
SELECT
O.ANIMAL_ID
, O.NAME
FROM ANIMAL_OUTS AS O
LEFT JOIN ANIMAL_INS AS I
ON O.ANIMAL_ID = I.ANIMAL_ID
WHERE I.ANIMAL_ID IS NULL
ORDER BY ANIMAL_ID
์์๋๋ฐ์ ์์์ต๋๋ค
SELECT
I.ANIMAL_ID
, I.NAME
FROM ANIMAL_INS AS I
JOIN ANIMAL_OUTS AS O
ON I.ANIMAL_ID = O.ANIMAL_ID
WHERE I.DATETIME > O.DATETIME -- ๋ณดํธ์์์ผ > ์
์์ผ (๋ณดํธ ์์์ผ๋ณด๋ค ์
์์ผ์ด ๋ ๋น ๋ฆ)
ORDER BY I.DATETIME
๋ ์ง ๋น๊ต ์ฐ์ฐ (WHERE I.DATETIME > O.DATETIME)
- ํฐ ๊ฐ์ผ์๋ก ๋ ๋์ค์ ์๊ฐ์!!
์ค๋ ๊ธฐ๊ฐ ๋ณดํธํ ๋๋ฌผ(1)
SELECT
I.NAME
, I.DATETIME
FROM ANIMAL_INS AS I
LEFT JOIN ANIMAL_OUTS AS O
ON I.ANIMAL_ID = O.ANIMAL_ID
WHERE O.ANIMAL_ID IS NULL
ORDER BY DATETIME
LIMIT 3
์ค๋ ๊ธฐ๊ฐ ๋ณดํธํ ๋๋ฌผ(2)
SELECT
I.ANIMAL_ID
, I.NAME
FROM ANIMAL_INS AS I
JOIN ANIMAL_OUTS AS O
ON I.ANIMAL_ID = O.ANIMAL_ID
WHERE O.ANIMAL_ID IS NOT NULL
ORDER BY (O.DATETIME - I.DATETIME) DESC
LIMIT 2
ํค๋น ์ ์ ๊ฐ ์์ ํ ์ฅ์
SELECT
ID
, NAME
, HOST_ID
FROM PLACES
WHERE HOST_ID IN (
SELECT HOST_ID
FROM PLACES
GROUP BY HOST_ID
HAVING COUNT(*) >= 2)
ORDER BY ID
์กฐ๊ฑด๋ณ๋ก ๋ถ๋ฅํ์ฌ ์ฃผ๋ฌธ์ํ ์ถ๋ ฅํ๊ธฐ
SELECT
ORDER_ID
, PRODUCT_ID
, DATE_FORMAT(OUT_DATE, '%Y-%m-%d') AS OUT_DATE
, CASE
WHEN OUT_DATE <= '2022-05-01' THEN '์ถ๊ณ ์๋ฃ'
WHEN OUT_DATE IS NULL THEN '์ถ๊ณ ๋ฏธ์ '
ELSE '์ถ๊ณ ๋๊ธฐ'
FROM FOOD_ORDER
ORDER BY ORDER_ID
์ฆ๊ฒจ์ฐพ๊ธฐ๊ฐ ๊ฐ์ฅ ๋ง์ ์๋น ์ ๋ณด ์ถ๋ ฅํ๊ธฐ
ํ์ด1. ์๋ธ์ฟผ๋ฆฌ ํ์ฉ
SELECT
FOOD_TYPE
, REST_ID
, REST_NAME
, FAVORITES
FROM REST_INFO
WHERE (FOOD_TYPE, FAVORITES) IN (
SELECT FOOD_TYPE, MAX(FAVORITES)
FROM REST_INFO
GROUP BY FOOD_TYPE
)
ORDER BY FOOD_TYPE DESC
---
ํ์ด 2. ์๋์ฐ ํจ์ ํ์ฉ
SELECT
FOOD_TYPE
, REST_ID
, REST_NAME
, FAVORITES
FROM (
SELECT *
, MAX(FAVORITES) OVER (PARTITION BY FOOD_TYPE) AS MAX_FAV
FROM REST_INFO
) AS M
WHERE FAVORITES = MAX_FAV
ORDER BY FOOD_TYPE DESC
์นดํ ๊ณ ๋ฆฌ ๋ณ ๋์ ํ๋งค๋ ์ง๊ณํ๊ธฐ
SELECT
CATEGORY
, SUM(SALES) AS TOTAL_SALES
FROM BOOK AS B
JOIN BOOK_SALES AS S
ON B.BOOK_ID = S.BOOK_ID
WHERE SALES_DATE LIKE '2022-01%'
GROUP BY CATEGORY
ORDER BY CATEGORY
๋์ฌ ํ์๊ฐ ๋ง์ ์๋์ฐจ๋ค์ ์๋ณ ๋์ฌ ํ์ ๊ตฌํ๊ธฐ
SELECT
MONTH(START_DATE) AS MONTH
, CAR_ID
, COUNT(*) AS RECORDS
FROM CAR_RENTAL_COMPANY_RENTAL_HISTORY
WHERE CAR_ID IN (
SELECT CAR_ID
FROM CAR_RENTAL_COMPANY_RENTAL_HISTORY
WHERE START_DATE BETWEEN '2022-08-01' AND '2022-10-31'
GROUP BY CAR_ID
HAVING COUNT(*) >= 5)
AND START_DATE BETWEEN '2022-08-01' AND '2022-10-31'
GROUP BY MONTH, CAR_ID
ORDER BY MONTH, CAR_ID DESC
์๋์ฐจ ๋์ฌ ๊ธฐ๋ก์์ ๋์ฌ์ค / ๋์ฌ ๊ฐ๋ฅ ์ฌ๋ถ ๊ตฌ๋ถํ๊ธฐ
SELECT
CAR_ID
, CASE
WHEN MAX('2022-10-16' BETWEEN START_DATE AND END_DATE) = 1 THEN '๋์ฌ์ค'
ELSE '๋์ฌ ๊ฐ๋ฅ'
END AS AVAILABILITY
FROM CAR_RENTAL_COMPANY_RENTAL_HISTORY
GROUP BY CAR_ID
ORDER BY CAR_ID DESC
group by๋ฅผ ์ํํ๋ฉด car_id ๋ผ๋ฆฌ ๋ฌถ์ด๊ฒ ๋๋๋ฐ ์ด๋ when์ max ์ ์์ ์กฐ๊ฑด์ ํตํด ์๋์ฒ๋ผ ์ถ๋ ฅ
- ์ฐธ์ด๋ฉด 1
- ๊ฑฐ์ง์ด๋ฉด 0
๋ฐ๋ผ์ max ํจ์์ ์ํด ๋์ฌ์ค์ด ํ๋ฒ์ด๋ผ๋ ์์์ผ๋ฉด 1์ ์ถ๋ ฅํ๊ฒ ๋๋ฉฐ
๊ทธ๊ฒ์ด =1 ์ ์กฐ๊ฑด์ ๋ง์ then '๋์ฌ์ค'์ผ๋ก ๋์ค๊ฒ ๋๋ ๊ฒ
๋์ฌ ๊ธฐ๋ก์ด ์กด์ฌํ๋ ์๋์ฐจ ๋ฆฌ์คํธ ๊ตฌํ๊ธฐ
SELECT DISTINCT(C.CAR_ID) AS CAR_ID
FROM CAR_RENTAL_COMPANY_CAR AS C
JOIN CAR_RENTAL_COMPANY_RENTAL_HISTORY AS H
ON C.CAR_ID = H.CAR_ID
WHERE C.CAR_TYPE = '์ธ๋จ'
AND H.START_DATE LIKE '2022-10%'
ORDER BY CAR_ID DESC
์กฐ๊ฑด์ ๋ง๋ ์ฌ์ฉ์์ ์ด ๊ฑฐ๋๊ธ์ก ์กฐํํ๊ธฐ
SELECT
U.USER_ID
, U.NICKNAME
, SUM(B.PRICE) AS TOTAL_SALES
FROM USED_GOODS_BOARD AS B
JOIN USED_GOODS_USER AS U
ON B.WRITER_ID = U.USER_ID
WHERE B.STATUS = 'DONE'
GROUP BY USER_ID
HAVING TOTAL_SALES >= 700000
ORDER BY TOTAL_SALES
์กฐ๊ฑด์ ๋ง๋ ์ฌ์ฉ์ ์ ๋ณด ์กฐํํ๊ธฐ
SELECT
USER_ID
, NICKNAME
, CONCAT_WS(' ', CITY, STREET_ADDRESS1, STREET_ADDRESS2) AS ์ ์ฒด์ฃผ์
, CONCAT_WS('-', SUBSTR(TLNO, 1, 3), SUBSTR(TLNO, 4, 4), SUBSTR(TLNO, 8, 4)) AS ์ ํ๋ฒํธ
FROM USED_GOODS_USER
WHERE USER_ID IN (
SELECT WRITER_ID
FROM USED_GOODS_BOARD
GROUP BY WRITER_ID
HAVING COUNT(*) >= 3)
ORDER BY USER_ID DESC
CONCAT๊ณผ CONCAT_WS์ ์ฐจ์ด
: ๋ฌธ์์ด์ ๊ฒฐํฉํ๋ ํจ์
- CONCAT
- ์ฌ๋ฌ ๋ฌธ์์ด์ ๋จ์ํ ๋ถ์ฌ์ ์ฌ์ฉ
- ์์ ex) CONCAT(CITY, '-', STREET_ADDRESS1, '-', STREET_ADDRESS2)
- CONCAT_WS
- ๊ตฌ๋ถ์๋ฅผ ์ง์ ํ์ฌ ์ฌ๋ฌ ๋ฌธ์์ด์ ๊ฒฐํฉ
- ์์ ex) CONCAT_WS(' ', CITY, STREET_ADDRESS1, STREET_ADDRESS2)
์์ ์์์ฒ๋ผ 3๊ฐ ์ด์์ ๋ฌธ์์ด์ ๊ฒฐํฉํ ๋๋ concat_ws๊ฐ ๋ ๊ฐํธํ๋ค.
SUBSTR ๋ฒ์
: ๋ฌธ์์ด์ ์๋ฅผ ๋ ์ฌ์ฉ๋๋ ํจ์
- ์ฌ์ฉ๋ฒ : SUBSTR(๋ฌธ์์ด, ์์์์น, ๊ธธ์ด)
* ๋ง์ง๋ง์ด '๋์์น'๊ฐ ์๋๋ผ '๊ธธ์ด'์์ ์ฃผ์ํ๊ธฐ *
์กฐํ์๊ฐ ๊ฐ์ฅ ๋ง์ ์ค๊ณ ๊ฑฐ๋ ๊ฒ์ํ์ ์ฒจ๋ถํ์ผ ์กฐํํ๊ธฐ
SELECT
CONCAT('/home/grep/src/', BOARD_ID, '/', FILE_ID, FILE_NAME, FILE_EXT) AS FILE_PATH
FROM USED_GOODS_FILE
WHERE BOARD_ID = (SELECT BOARD_ID
FROM USED_GOODS_BOARD
ORDER BY VIEWS DESC
LIMIT 1)
ORDER BY FILE_ID DESC
์ ๊ทธ๋ ์ด๋ ํ ์ ์๋ ์์ดํ ๊ตฌํ๊ธฐ
SELECT
I.ITEM_ID
, ITEM_NAME
, RARITY
FROM ITEM_INFO AS I
LEFT JOIN ITEM_TREE AS T
ON I.ITEM_ID = T.PARENT_ITEM_ID
WHERE T.ITEM_ID IS NULL
ORDER BY ITEM_ID DESC
๋ถ์๋ณ ํ๊ท ์ฐ๋ด ์กฐํํ๊ธฐ
SELECT
D.DEPT_ID
, D.DEPT_NAME_EN
, ROUND(AVG(E.SAL), 0) AS AVG_SAL
FROM HR_DEPARTMENT AS D
JOIN HR_EMPLOYEES AS E
ON D.DEPT_ID = E.DEPT_ID
GROUP BY D.DEPT_ID
ORDER BY AVG_SAL DESC
๋ฌผ๊ณ ๊ธฐ ์ข ๋ฅ ๋ณ ๋์ด ์ฐพ๊ธฐ
SELECT
I.ID
, N.FISH_NAME
, I.LENGTH
FROM (SELECT
*
, MAX(LENGTH) OVER (PARTITION BY FISH_TYPE) AS MAX_LENGTH
FROM FISH_INFO
) AS I
JOIN FISH_NAME_INFO AS N
ON I.FISH_TYPE = N.FISH_TYPE
WHERE I.LENGTH = I.MAX_LENGTH
ORDER BY ID
ํน์ ์กฐ๊ฑด์ ๋ง์กฑํ๋ ๋ฌผ๊ณ ๊ธฐ๋ณ ์์ ์ต๋ ๊ธธ์ด ๊ตฌํ๊ธฐ
SELECT
COUNT(*) AS FISH_COUNT
, MAX(LENGTH) AS MAX_LENGTH
, FISH_TYPE
FROM FISH_INFO
GROUP BY FISH_TYPE
HAVING AVG(IFNULL(LENGTH, 10)) >= 33
ORDER BY FISH_TYPE
๋์ฅ๊ท ๋ค์ ์์์ ์ ๊ตฌํ๊ธฐ
SELECT
P.ID
, COUNT(C.ID) AS CHILD_COUNT
FROM ECOLI_DATA AS P
LEFT JOIN ECOLI_DATA AS C
ON P.ID = C.PARENT_ID
GROUP BY ID
ORDER BY ID
ํท๊ฐ๋ ธ๋ ๋ถ๋ถ
SELECT ์ ์ COUNT(C.ID) vs COUNT(P.ID)
- COUNT(P.ID) : ํญ์ ๋ถ๋ชจ์ ํ ์๋ฅผ ๋ฐํ (๋ถ๋ชจ ์์ฒด๋ฅผ ๋ํ๋ด๊ธฐ ๋๋ฌธ์ NULL์ด ์์ ์ ์์ - NULL์ ๊ณ ๋ คX)
- COUNT(C.ID) : ์์์ด ์๋ ๋ถ๋ชจ์ ๋ํด์๋ง, ์์์ ์๋ฅผ ๋ฐํ (NULL์ ๊ณ ๋ คํจ)
* ํท๊ฐ๋ฆฌ๋ ๋ฌธ์ ๋๊น ์ฃผ์ํ๊ธฐ
๋์ฅ๊ท ์ ํฌ๊ธฐ์ ๋ฐ๋ผ ๋ถ๋ฅํ๊ธฐ 1
SELECT
ID
, (CASE
WHEN SIZE_OF_COLONY <= 100 THEN 'LOW'
WHEN SIZE_OF_COLONY > 1000 THEN 'HIGH'
ELSE 'MEDIUM'
END) AS SIZE
FROM ECOLI_DATA
ORDER BY ID
๋์ฅ๊ท ์ ํฌ๊ธฐ์ ๋ฐ๋ผ ๋ถ๋ฅํ๊ธฐ 2
SELECT
ID
, (CASE
WHEN PER_RANK <= 0.25 THEN 'CRITICAL'
WHEN PER_RANK <= 0.5 THEN 'HIGH'
WHEN PER_RANK <= 0.75 THEN 'MEDIUM'
ELSE 'LOW'
END) AS COLONY_NAME
FROM (SELECT
ID,
PERCENT_RANK() OVER (ORDER BY SIZE_OF_COLONY DESC) AS PER_RANK
FROM ECOLI_DATA
) AS D
ORDER BY ID
๐ ๋ณด์ํด์ผ ํ ์
- SELF JOIN ๋ ์ฐ์ตํ๊ธฐ
- PARTITION BY ๋ ์ ๊ทน์ ์ผ๋ก ํ์ฉํด๋ณด๊ธฐ
๋ค๋ฅธ Lv. ๋ก ๋ฐ๋ก๊ฐ๊ธฐ