IT/์ฝ”๋”ฉํ…Œ์ŠคํŠธ

[ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค] MySQL Lv.3 ๋ฌธ์ œํ’€์ด ์ •๋‹ต ๋ชจ์Œ (feat. ์ฝ”๋”ฉํ…Œ์ŠคํŠธ)

zi0_0 2024. 12. 24. 07:33

 

๋ฌธ์ œ ํ’€์ด ๊ธฐ๊ฐ„ : 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. ๋กœ ๋ฐ”๋กœ๊ฐ€๊ธฐ
 

[ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค] SQL Lv.4 ๋ฌธ์ œํ’€์ด ์ •๋‹ต ๋ชจ์Œ (feat. ์ฝ”๋”ฉํ…Œ์ŠคํŠธ)

๋ฌธ์ œ ํ’€์ด ๊ธฐ๊ฐ„ : 24๋…„ 12์›” 27์ผ ~  ์ตœ์‹ ์ˆœ์œผ๋กœ ์ •๋ ฌํ•œ ๋ฌธ์ œ ์ˆœ์„œ์ž…๋‹ˆ๋‹ค  ๋ณดํ˜ธ์†Œ์—์„œ ์ค‘์„ฑํ™”ํ•œ ๋™๋ฌผSELECT I.ANIMAL_ID , I.ANIMAL_TYPE , I.NAMEFROM ANIMAL_INS AS I JOIN ANIMAL_OUTS AS O ON I.ANIMAL_ID = O.ANIMAL_IDWHERE S

thswldud.tistory.com

Copy