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

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

zi0_0 2024. 12. 17. 08:38

 

๋ฌธ์ œ ํ’€์ด ๊ธฐ๊ฐ„ : 24๋…„ 12์›” 17์ผ ~ 19์ผ 

 

์ตœ์‹ ์ˆœ์œผ๋กœ ์ •๋ ฌํ•œ ๋ฌธ์ œ ์ˆœ์„œ์ž…๋‹ˆ๋‹ค

 

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

SELECT *
FROM ANIMAL_INS
ORDER BY ANIMAL_ID ASC;

 

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

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
WHERE INTAKE_CONDITION != 'Aged'
ORDER BY ANIMAL_ID;

 

์ด๋ฆ„์ด ์—†๋Š” ๋™๋ฌผ์˜ ์•„์ด๋””

SELECT ANIMAL_ID
FROM ANIMAL_INS
WHERE NAME IS NULL;

 

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

SELECT ANIMAL_ID, 
        NAME
FROM ANIMAL_INS
ORDER BY ANIMAL_ID;

 

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

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

 

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

SELECT NAME
FROM ANIMAL_INS
ORDER BY DATETIME
LIMIT 1;

 

์ด๋ฆ„์ด ์žˆ๋Š” ๋™๋ฌผ์˜ ์•„์ด๋””

SELECT ANIMAL_ID
FROM ANIMAL_INS
WHERE NAME IS NOT NULL

 

์ตœ๋Œ“๊ฐ’ ๊ตฌํ•˜๊ธฐ 

SELECT MAX(DATETIME) AS ์‹œ๊ฐ„
FROM ANIMAL_INS

SELECT DATETIME AS ์‹œ๊ฐ„
FROM ANIMAL_INS
WHERE DATETIME = (SELECT MAX(DATETIME)
                  FROM ANIMAL_INS);

 

๊ฐ•์›๋„์— ์œ„์น˜ํ•œ ์ƒ์‚ฐ๊ณต์žฅ ๋ชฉ๋ก ์ถœ๋ ฅํ•˜๊ธฐ

SELECT WAREHOUSE_ID
     , WAREHOUSE_NAME
     , ADDRESS
     , IFNULL(FREEZER_YN, 'N')
FROM FOOD_WAREHOUSE
WHERE ADDRESS LIKE '๊ฒฝ๊ธฐ%'

% ํ™œ์šฉ๋ฒ•

  • '์†'์œผ๋กœ ์‹œ์ž‘ํ•˜๋Š” 4๊ธ€์ž ๋ฐ์ดํ„ฐ ์กฐํšŒ : ~ LIKE '์†___'
  • '์†'์œผ๋กœ ์‹œ์ž‘ํ•˜๋Š” ๋ชจ๋“  ๋ฐ์ดํ„ฐ ์กฐํšŒ : ~ LIKE '์†%'
  • '๋ธ”'์ด ๋“ค์–ด๊ฐ€๋Š” ๋ชจ๋“  ๋ฐ์ดํ„ฐ ์กฐํšŒ : ~ LIKE '%์†%'

 

๊ฒฝ๊ธฐ๋„์— ์œ„์น˜ํ•œ ์‹ํ’ˆ์ฐฝ๊ณ  ๋ชฉ๋ก ์ถœ๋ ฅํ•˜๊ธฐ

SELECT WAREHOUSE_ID
     , WAREHOUSE_NAME
     , ADDRESS
     , IFNULL(FREEZER_YN, 'N')
FROM FOOD_WAREHOUSE
WHERE ADDRESS LIKE '๊ฒฝ๊ธฐ%'

 

๋‚˜์ด ์ •๋ณด๊ฐ€ ์—†๋Š” ํšŒ์› ์ˆ˜ ๊ตฌํ•˜๊ธฐ

SELECT COUNT(USER_ID) AS USERS
FROM USER_INFO
WHERE AGE IS NULL;

 

์กฐ๊ฑด์— ๋งž๋Š” ํšŒ์›์ˆ˜ ๊ตฌํ•˜๊ธฐ

SELECT COUNT(USER_ID) AS USERS
FROM USER_INFO
WHERE JOINED LIKE '2021%'
AND AGE BETWEEN 20 AND 29
  • AND 20 <= AGE <= 29 : ์ด๋ ‡๊ฒŒ ์—ฐ์†์ ์ธ ์กฐ๊ฑด ๋น„๊ต๋Š” ์•ˆ๋จ 


๊ฐ€์žฅ ๋น„์‹ผ ์ƒํ’ˆ ๊ตฌํ•˜๊ธฐ

SELECT MAX(PRICE) AS MAX_PRICE
FROM PRODUCT


12์„ธ ์ดํ•˜์ธ ์—ฌ์ž ํ™˜์ž ๋ชฉ๋ก ์ถœ๋ ฅํ•˜๊ธฐ

SELECT PT_NAME
     , PT_NO
     , GEND_CD
     , AGE
     , IFNULL(TLNO, 'NONE') 
FROM PATIENT
WHERE AGE <= 12
AND GEND_CD = 'W'
ORDER BY AGE DESC, 
         PT_NAME ASC


ํ‰๋ถ€์™ธ๊ณผ ๋˜๋Š” ์ผ๋ฐ˜์™ธ๊ณผ ์˜์‚ฌ ๋ชฉ๋ก ์ถœ๋ ฅํ•˜๊ธฐ

SELECT DR_NAME
     , DR_ID
     , MCDP_CD
     , DATE_FORMAT(HIRE_YMD, '%Y-%m-%d')
FROM DOCTOR
WHERE MCDP_CD IN ('CS', 'GS')
ORDER BY HIRE_YMD DESC
       , DR_NAME ASC


์ธ๊ธฐ์žˆ๋Š” ์•„์ด์Šคํฌ๋ฆผ

SELECT FLAVOR
FROM FIRST_HALF
ORDER BY TOTAL_ORDER DESC
       , SHIPMENT_ID ASC


๊ณผ์ผ๋กœ ๋งŒ๋“  ์•„์ด์Šคํฌ๋ฆผ ๊ณ ๋ฅด๊ธฐ

SELECT F.FLAVOR
FROM FIRST_HALF F
JOIN ICECREAM_INFO I
ON F.FLAVOR = I.FLAVOR
WHERE F.TOTAL_ORDER > 3000
AND I.INGREDIENT_TYPE = 'fruit_based'
ORDER BY F.TOTAL_ORDER DESC


์กฐ๊ฑด์— ๋งž๋Š” ๋„์„œ ๋ฆฌ์ŠคํŠธ ์ถœ๋ ฅํ•˜๊ธฐ

SELECT BOOK_ID
     , DATE_FORMAT(PUBLISHED_DATE, '%Y-%m-%d')
FROM BOOK 
WHERE PUBLISHED_DATE LIKE '2021%'
AND CATEGORY = '์ธ๋ฌธ'
ORDER BY PUBLISHED_DATE

SELECT BOOK_ID
	 , DATE_FORMAT(PUBLISHED_DATE, '%Y-%m-%d') 
FROM BOOK
WHERE CATEGORY LIKE '์ธ๋ฌธ' 
AND DATE_FORMAT(PUBLISHED_DATE, '%Y') = 2021
ORDER BY PUBLISHED_DATE

๋‘ ๊ฐœ๊ฐ€ ๊ฐ™์Œ!

  • WHERE PUBLISHED_DATE LIKE '2021%'
  • DATE_FORMAT(PUBLISHED_DATE, '%Y') = 2021

 

ํ‰๊ท  ์ผ์ผ ๋Œ€์—ฌ ์š”๊ธˆ ๊ตฌํ•˜๊ธฐ

SELECT ROUND(AVG(DAILY_FEE)) AS AVERAGE_FEE
FROM CAR_RENTAL_COMPANY_CAR
WHERE CAR_TYPE = 'SUV'

 

์ž๋™์ฐจ ๋Œ€์—ฌ ๊ธฐ๋ก์—์„œ ์žฅ๊ธฐ/๋‹จ๊ธฐ ๋Œ€์—ฌ ๊ตฌ๋ถ„ํ•˜๊ธฐ

SELECT HISTORY_ID
     , CAR_ID
     , DATE_FORMAT(START_DATE, '%Y-%m-%d') AS START_DATE
     , DATE_FORMAT(END_DATE, '%Y-%m-%d') AS END_DATE
     , CASE 
            WHEN DATEDIFF(END_DATE, START_DATE) + 1 >= 30 THEN '์žฅ๊ธฐ ๋Œ€์—ฌ'
            ELSE '๋‹จ๊ธฐ ๋Œ€์—ฌ'
       END AS RENT_TYPE
FROM CAR_RENTAL_COMPANY_RENTAL_HISTORY
WHERE DATE_FORMAT(START_DATE, '%Y-%m') = '2022-09'
ORDER BY HISTORY_ID DESC

IF ์ ˆ๋กœ ์‚ฌ์šฉํ•  ๋•Œ

  • IF(DATEDIFF(END_DATE, START_DATE) <29, '๋‹จ๊ธฐ ๋Œ€์—ฌ', '์žฅ๊ธฐ ๋Œ€์—ฌ') AS RENT_TYPE

 

ํŠน์ • ์˜ต์…˜์ด ํฌํ•จ๋œ ์ž๋™์ฐจ ๋ฆฌ์ŠคํŠธ ๊ตฌํ•˜๊ธฐ

SELECT *
FROM CAR_RENTAL_COMPANY_CAR
WHERE OPTIONS LIKE '%๋„ค๋น„๊ฒŒ์ด์…˜%'
ORDER BY CAR_ID DESC

 

์กฐ๊ฑด์— ๋ถ€ํ•ฉํ•˜๋Š” ์ค‘๊ณ ๊ฑฐ๋ž˜ ๋Œ“๊ธ€ ์กฐํšŒํ•˜๊ธฐ

SELECT B.TITLE
     , B.BOARD_ID
     , R.REPLY_ID
     , R.WRITER_ID
     , R.CONTENTS
     , DATE_FORMAT(R.CREATED_DATE, '%Y-%m-%d') AS CREATED_DATE
FROM USED_GOODS_BOARD B
JOIN USED_GOODS_REPLY R
    ON B.BOARD_ID = R.BOARD_ID
WHERE B.CREATED_DATE LIKE '2022-10%'
ORDER BY 6, 1

 

Python ๊ฐœ๋ฐœ์ž ์ฐพ๊ธฐ

SELECT ID
     , EMAIL
     , FIRST_NAME
     , LAST_NAME
FROM DEVELOPER_INFOS
WHERE 'Python' IN (SKILL_1, SKILL_2, SKILL_3)
ORDER BY ID

 

์ž”์ฑ™์ด ์žก์€ ์ˆ˜ ๊ตฌํ•˜๊ธฐ

SELECT COUNT(ID) AS FISH_COUNT
FROM FISH_INFO
WHERE LENGTH IS NULL

 

์žก์€ ๋ฌผ๊ณ ๊ธฐ์˜ ํ‰๊ท  ๊ธธ์ด ๊ตฌํ•˜๊ธฐ

SELECT ROUND(AVG(IFNULL(LENGTH, 10)), 2) AS AVERAGE_LENGTH
FROM FISH_INFO

 

์žก์€ ๋ฌผ๊ณ ๊ธฐ ์ค‘ ๊ฐ€์žฅ ํฐ ๋ฌผ๊ณ ๊ธฐ์˜ ๊ธธ์ด ๊ตฌํ•˜๊ธฐ

SELECT CONCAT(MAX(LENGTH), 'cm') AS MAX_LENGTH
FROM FISH_INFO

 

ํ•œ ํ•ด์— ์žก์€ ๋ฌผ๊ณ ๊ธฐ ์ˆ˜ ๊ตฌํ•˜๊ธฐ

SELECT COUNT(*) AS FISH_COUNT
FROM FISH_INFO
WHERE YEAR(TIME) = 2021

 

๊ฐ€์žฅ ํฐ ๋ฌผ๊ณ ๊ธฐ 10๋งˆ๋ฆฌ ๊ตฌํ•˜๊ธฐ

SELECT 
      ID
    , LENGTH
FROM FISH_INFO
ORDER BY 
      LENGTH DESC
    , ID ASC
LIMIT 10

 

ํŠน์ • ํ˜•์งˆ์„ ๊ฐ€์ง€๋Š” ๋Œ€์žฅ๊ท  ์ฐพ๊ธฐ

-

 


 

Lv. 2๋กœ ๋ฐ”๋กœ๊ฐ€๊ธฐ

 

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

๋ฌธ์ œ ํ’€์ด ๊ธฐ๊ฐ„ : 24๋…„ 12์›” 20์ผ ~  ์ตœ์‹ ์ˆœ์œผ๋กœ ์ •๋ ฌํ•œ ๋ฌธ์ œ ์ˆœ์„œ์ž…๋‹ˆ๋‹ค ์ตœ์†Ÿ๊ฐ’ ๊ตฌํ•˜๊ธฐSELECT MIN(DATETIME) AS ์‹œ๊ฐ„FROM ANIMAL_INS ๊ณ ์–‘์ด์™€ ๊ฐœ๋Š” ๋ช‡ ๋งˆ๋ฆฌ ์žˆ์„๊นŒSELECT ANIMAL_TYPE , COUNT(*) AS countFROM ANIMAL_

thswldud.tistory.com

 

์ฐธ๊ณ ํ•˜๋ฉด ์ข‹์„ ์ž๋ฃŒ
 

SQL ๊ฐ€๋…์„ฑ์„ ๋†’์ด๋Š” ๋‹ค์„ฏ ๊ฐ€์ง€ ์‚ฌ์†Œํ•œ ์Šต๊ด€

์ง€๋…ํ•˜๊ฒŒ ์ฝ๊ธฐ ํž˜๋“  SQL๋ฌธ์„ ํ•ด์„ํ•ด ๋ณธ ์  ์žˆ์œผ์‹ ๊ฐ€์š”?

datarian.io