코테 준비/프로그래머스

[SQL_GROUP BY]

쿠쿠*_* 2023. 6. 12. 21:09

📌성분으로 구분한 아이스크림 총 주문량

SELECT A.INGREDIENT_TYPE, SUM(B.TOTAL_ORDER) AS TOTAL_ORDER
FROM ICECREAM_INFO A, FIRST_HALF B
WHERE A.FLAVOR=B.FLAVOR
GROUP BY A.INGREDIENT_TYPE
ORDER BY B.TOTAL_ORDER

📌가격대 별 상품 개수 구하기

SELECT TRUNCATE(PRICE,-4) AS PRICE_GROUP, COUNT(PRODUCT_ID) AS PRODUCTS
FROM PRODUCT
GROUP BY PRICE_GROUP
ORDER BY PRICE_GROUP

📌입양 시각 구하기(1)

SELECT HOUR(DATETIME) AS HOUR, COUNT(DATETIME) AS COUNT
FROM ANIMAL_OUTS
WHERE HOUR(DATETIME) >= 9
  AND HOUR(DATETIME) <= 19
GROUP BY HOUR(DATETIME)
ORDER BY HOUR ASC;
#참고
SELECT
    ANIMAL_ID
    ,NAME
    ,DATE_FORMAT(DATETIME, '%Y-%m-%d') AS 날짜
FROM ANIMAL_INS
ORDER BY ANIMAL_ID

📌동명 동물 수 찾기

SELECT NAME,COUNT(NAME) 
FROM ANIMAL_INS 
GROUP BY NAME
HAVING COUNT(NAME)>1
ORDER BY NAME

📌고양이와 개는 몇 마리 있을까

SELECT ANIMAL_TYPE ,
COUNT(ANIMAL_TYPE) AS count 
FROM ANIMAL_INS
GROUP BY ANIMAL_TYPE
ORDER BY ANIMAL_TYPE

📌자동차 대여 기록에서 대여중 / 대여 가능 여부 구분하기

SELECT CAR_ID,
CASE WHEN CAR_ID IN (SELECT CAR_ID
                    FROM CAR_RENTAL_COMPANY_RENTAL_HISTORY
                    WHERE '2022-10-16' BETWEEN START_DATE AND END_DATE) THEN '대여중'
                    ELSE '대여 가능'
                END 'AVAILABILITY'
FROM CAR_RENTAL_COMPANY_RENTAL_HISTORY
GROUP BY CAR_ID
ORDER BY CAR_ID DESC

📌카테고리 별 도서 판매량 집계하기

SELECT b.CATEGORY, SUM(bs.SALES) AS TOTAL_SALES
FROM BOOK b
JOIN BOOK_SALES bs
ON b.BOOK_ID = bs.BOOK_ID
WHERE bs.SALES_DATE LIKE '2022-01%'
GROUP BY b.CATEGORY
ORDER BY b.CATEGORY

#또다른 정답 코드
SELECT b.CATEGORY, SUM(bs.SALES) AS TOTAL_SALES
FROM BOOK b, BOOK_SALES bs
WHERE b.BOOK_ID = bs.BOOK_ID
AND bs.SALES_DATE LIKE '2022-01%'
GROUP BY b.CATEGORY
ORDER BY b.CATEGORY

📌진료과별 총 예약 횟수 출력하기

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 A.INGREDIENT_TYPE, SUM(B.TOTAL_ORDER) AS TOTAL_ORDER
FROM ICECREAM_INFO A, FIRST_HALF B
WHERE A.FLAVOR=B.FLAVOR
GROUP BY A.INGREDIENT_TYPE
ORDER BY B.TOTAL_ORDER

 

 

[SQL] 프로그래머스 DATETIME에서 DATE로 형변환

https://programmers.co.kr/learn/courses/30/lessons/59414?language=mysql 코딩테스트 연습 - DATETIME에서 DATE로 형 변환 ANIMAL_INS 테이블은 동물 보호소에 들어온 동물의 정보를 담은 테이블입니다. ANIMAL_INS 테이블 구

pig-programming.tistory.com

 

 

[SQL] join할때 on 과 where의 차이

on : join 전에 조건을 필터링 where : join 후에 조건을 필터링inner join 하면 둘다 차이 없음outer join 시 on으로 해야 원하는 결과를 얻을 수 있음(outer table에 null값 포함하여 행들이 다 살아있는 상태)on

velog.io

 

'코테 준비 > 프로그래머스' 카테고리의 다른 글

[SQL_JOIN]  (0) 2023.06.13
[SQL_IS NULL]  (0) 2023.06.13
[SQL_SUM,MAX,MIN]  (0) 2023.06.12
[SQL_SELECT]  (1) 2023.06.12
[level 2] 호텔 대실  (0) 2023.05.26