๋ฌธ์ ํ์ด ๊ธฐ๊ฐ : 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. ์ฝ๋ฉํ ์คํธ) (1) | 2024.12.20 |
---|