Programmers_SQL_LV2_MySQL
2023. 2. 15. 21:44ㆍSQL/Programmers
728x90
- 진료과별 총 예약 횟수 출력하기
-- 코드를 입력하세요
SELECT MCDP_CD AS "진료과코드",
COUNT(MCDP_CD) AS "5월예약건수"
FROM APPOINTMENT
WHERE APNT_YMD like "2022-05%"
GROUP BY MCDP_CD
ORDER BY COUNT(MCDP_CD), MCDP_CD;
- 상품 별 오프라인 매출 구하기
-- 코드를 입력하세요
SELECT p.PRODUCT_CODE AS PRODUCT_CODE,
SUM(p.PRICE * o.SALES_AMOUNT) AS SALES
FROM PRODUCT AS p
LEFT JOIN OFFLINE_SALE AS o
ON p.PRODUCT_ID = o.PRODUCT_ID
GROUP BY PRODUCT_CODE
ORDER BY SALES DESC, PRODUCT_CODE ASC;
- 가격대 별 상품 개수 구하기
-- 코드를 입력하세요
SELECT TRUNCATE(price,-4) AS PRICE_GROUP,
COUNT(*) AS PRODUCTS
FROM PRODUCT
GROUP BY TRUNCATE(price,-4)
ORDER BY PRICE
- 성분으로 구분한 아이스크림 총 주문량
-- 코드를 입력하세요
SELECT i.INGREDIENT_TYPE,
SUM(TOTAL_ORDER) AS TOTAL_ORDER
FROM FIRST_HALF AS f
LEFT JOIN ICECREAM_INFO AS i
ON f.FLAVOR = i.FLAVOR
GROUP BY INGREDIENT_TYPE
ORDER BY TOTAL_ORDER;
- 조건에 맞는 도서와 저자 리스트 출력하기
-- 코드를 입력하세요
SELECT BOOK_ID,
AUTHOR_NAME,
DATE_FORMAT(PUBLISHED_DATE, "%Y-%m-%d") AS PUBLISHED_DATE
FROM BOOK as b
LEFT JOIN AUTHOR as a
ON b.AUTHOR_ID = a.AUTHOR_ID
where b.CATEGORY = "경제"
ORDER BY PUBLISHED_DATE ASC;
- 자동차 종류 별 특정 옵션이 포함된 자동차 수 구하기
-- 코드를 입력하세요
SELECT CAR_TYPE,
COUNT(CAR_ID) AS CARS
FROM CAR_RENTAL_COMPANY_CAR
WHERE OPTIONS like "%통풍시트%" || OPTIONS like "%열선시트%" || OPTIONS like "%가죽시트%"
GROUP BY CAR_TYPE
ORDER BY CAR_TYPE ASC;
- 재구매가 일어난 상품과 회원 리스트 구하기
-- 코드를 입력하세요
SELECT USER_ID,
PRODUCT_ID
FROM ONLINE_SALE
GROUP BY USER_ID, PRODUCT_ID
HAVING COUNT(USER_ID) >= 2
ORDER BY USER_ID ASC, PRODUCT_ID DESC;
- 자동차 평균 대여 기간 구하기
-- 코드를 입력하세요
SELECT CAR_ID,
ROUND(AVG(DATEDIFF(END_DATE, START_DATE)+1), 1) AS AVERAGE_DURATION
FROM CAR_RENTAL_COMPANY_RENTAL_HISTORY
GROUP BY CAR_ID
HAVING AVERAGE_DURATION+1 >=7
ORDER BY AVERAGE_DURATION DESC, CAR_ID DESC;
- 동물 수 구하기
-- 코드를 입력하세요
SELECT count(*) AS 'count' from ANIMAL_INS;
- 최솟값 구하기
-- 코드를 입력하세요
SELECT MIN(DATETIME) AS '시간' from ANIMAL_INS;
- 중복 제거하기
-- 코드를 입력하세요
SELECT count(distinct NAME) from ANIMAL_INS;
- 이름에 el이 들어가는 동물 찾기
-- 코드를 입력하세요
SELECT ANIMAL_ID, NAME from ANIMAL_INS where name like ('%el%') and ANIMAL_TYPE = 'dog' order by NAME;
- 동명 동물 수 찾기
-- 코드를 입력하세요
SELECT NAME, count(NAME) AS 'COUNT' from ANIMAL_INS group by name having count(NAME)>1 order by NAME;
- NULL 처리하기
-- 코드를 입력하세요
SELECT ANIMAL_TYPE, IFNULL(NAME, 'No name') AS 'NAME', SEX_UPON_INTAKE from ANIMAL_INS order by ANIMAL_ID;
- DATETIME에서 DATE로 형 변환
-- 코드를 입력하세요
SELECT ANIMAL_ID, NAME, DATE_FORMAT(DATETIME, '%Y-%m-%d') as '날짜' from ANIMAL_INS order by ANIMAL_ID;
- 가격이 제일 비싼 식품의 정보 출력하기
-- 코드를 입력하세요
SELECT PRODUCT_ID,
PRODUCT_NAME,
PRODUCT_CD,
CATEGORY,
PRICE
FROM FOOD_PRODUCT
WHERE PRICE = (
SELECT max(PRICE)
from FOOD_PRODUCT
)
- 중성화 여부 파악하기
-- 코드를 입력하세요
SELECT ANIMAL_ID, NAME, if(SEX_UPON_INTAKE like 'Neutered%' or SEX_UPON_INTAKE like 'Spayed%', 'O', 'X') as '중성화' from ANIMAL_INS order by ANIMAL_ID;
- 고양이와 개는 몇 마리 있을까
-- 코드를 입력하세요
SELECT ANIMAL_TYPE, count(*) AS 'count' from ANIMAL_INS group by ANIMAL_TYPE;
- 입양 시각 구하기(1)
-- 코드를 입력하세요
SELECT HOUR(DATETIME) AS 'HOUR', count(ANIMAL_ID) AS 'COUNT' from ANIMAL_OUTS group by HOUR having HOUR between 9 and 19 order by HOUR;
- 카테고리 별 상품 개수 구하기
-- 코드를 입력하세요
SELECT LEFT(PRODUCT_CODE,2) AS CATEGORY,
COUNT(*) AS PRODUCTS
FROM PRODUCT
GROUP BY LEFT(PRODUCT_CODE,2)
ORDER BY PRODUCT_CODE;
- 루시와 엘라 찾기
-- 코드를 입력하세요
SELECT ANIMAL_ID, NAME, SEX_UPON_INTAKE from ANIMAL_INS where NAME in ('Lucy', 'Ella', 'Pickle', 'Rogan', 'Sabrina', 'Mitty') order by ANIMAL_ID;
- 3월에 태어난 여성 회원 목록 출력하기
-- 코드를 입력하세요
SELECT MEMBER_ID,
MEMBER_NAME,
GENDER,
DATE_FORMAT(DATE_OF_BIRTH, "%Y-%m-%d") AS DATE_OF_BIRTH
FROM MEMBER_PROFILE
WHERE TLNO IS NOT NULL AND SUBSTRING(DATE_OF_BIRTH, 6, 2) = "03" AND GENDER = "W"
ORDER BY MEMBER_ID ASC;
'SQL > Programmers' 카테고리의 다른 글
Programmers_SQL_LV4_MySQL (0) | 2023.02.16 |
---|---|
Programmers_SQL_LV3_MySQL (0) | 2023.02.15 |
Programmers_SQL_LV1_MySQL (0) | 2023.02.15 |