SQL 코테 준비 (5월 4주)(프로그래머스)

정희철·2026년 5월 27일

5.25

1) 조건에 맞는 아이템들의 가격의 총합 구하기

select sum(price) as total_price
from item_info
where rarity = 'LEGEND'

2) 흉부외과 또는 일반외과 의사 목록 출력하기

select dr_name,
       dr_id,
       mcdp_cd,
       hire_ymd
from doctor
where mcdp_cd = 'CS' or mcdp_cd = 'GS'
order by 4 desc, 1

5.26

1) 경기도에 위치한 식품창고 목록 출력하기

select warehouse_id,
       warehouse_name,
       address, 
       ifnull(freezer_yn, 'N') as freezer_yn
from food_warehouse
where address like '경기도%'
order by 1

2) 카테고리 별 상품 개수 구하기

select left(product_code, 2) as caetgory,
       count(product_id) as products
from product
group by left(product_code, 2)
order by 1

5.27

1) 아픈 동물 찾기

select animal_id,
       name
from animal_ins
where intake_condition = 'sick'
order by 1

2) 동명 동물 수 찾기

select name,
       count(name) as count
from animal_ins
group by name
having count(name) >= 2
order by name

3) 이름에 el이 들어가는 동물 찾기

select animal_id,
       name
from animal_ins
where animal_type = 'dog'
and name like '%EL%' 
order by name

5.28

1) NULL 처리하기

select animal_type,
       ifnull(name, 'No name') as name,
       sex_upon_intake
from animal_ins

2) DATETIME에서 DATE로 형 변환

select animal_id,
       name,
       date(datetime) as 날짜
from animal_ins
order by 1

3) 역순 정렬하기

select name,
       datetime
from animal_ins
order by animal_id desc

5.29

1) 최솟값 구하기

select min(datetime) as 시간
from animal_ins

2) 이름이 없는 동물의 아이디

select animal_id
from animal_ins
where name is null
order by 1

3) 인기있는 아이스크림

select flavor
from first_half
order by total_order desc, shipment_id

4) 가장 비싼 상품 구하기

select max(price) as max_price
from product

5) 중복 제거하기

select count(distinct name) as count
from animal_ins

6) 이름이 있는 동물의 아이디

select animal_id
from animal_ins
where name is not null
order by 1

7) 어린 동물 찾기

select animal_id,
       name
from animal_ins
where intake_condition not in ('Aged')
order by 1

8) 가격이 제일 비싼 식품의 정보 출력하기

select *
from food_product
where price = (select max(price) from food_product)

9) 강원도에 위치한 생산공장 목록 출력하기

select factory_id,
       factory_name,
       address
from food_factory
where address like '강원도%'
order by 1

10) 12세 이하인 여자 환자 목록 출력하기

select pt_name,
       pt_no,
       gend_cd,
       age,
       ifnull(tlno, 'NONE') as tlno
from patient
where age <= 12
and gend_cd = 'W'
order by 4 desc, 1

5.30

1) 조건에 맞는 회원수 구하기

select count(*) as users
from user_info
where joined between '2021-01-01' and '2021-12-31'
and age between 20 and 29

2) 나이 정보가 없는 회원 수 구하기

select count(*) as users
from user_info
where age is null

3) 여러 기준으로 정렬하기

select animal_id,
       name,
       datetime
from animal_ins
order by 2,3 desc

4) 상위 n개 레코드

select name
from animal_ins
where datetime = (select min(datetime) from animal_ins)

5) 동물의 아이디와 이름

select animal_id,
       name
from animal_ins
order by 1

6) 동물 수 구하기

select count(*) as count
from animal_ins

7) 자동차 종류 별 특정 옵션이 포함된 자동차 수 구하기

select car_type,
       count(*) as cars
from car_rental_company_car
where options like '%통풍시트%'
or options like '%열선시트%'
or options like '%가죽시트%'
group by car_type
order by 1

8) 중성화 여부 파악하기

select animal_id,
       name,
       case when sex_upon_intake like 'Neutered%' 
                or sex_upon_intake like 'Spayed%' 
                then 'O' else 'X' end as 중성화
from animal_ins

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

select mcdp_cd as 진료과코드,
       count(*) as 5월예약건수
from appointment
where apnt_ymd between '2022-05-01' and '2022-05-31'
group by mcdp_cd
order by 2,1

10) 조건에 맞는 도서와 저자 리스트 출력하기

select b.book_id,
       a.author_name,
       b.published_date
from book b
join author a using (author_id)
where b.category = '경제'
order by 3

5.31

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

select animal_type,
       count(*) as count
from animal_ins
group by animal_type
order by 1

2) 입양 시각 구하기(1)

select hour(datetime) as hour,
       count(*) as count
from animal_outs
where hour(datetime) between 9 and 19
group by hour
order by 1

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

select i.ingredient_type,
       sum(f.total_order) as total_order
from first_half f
join icecream_info i using(flavor)
group by i.ingredient_type
order by 2

4) 조건별로 분류하여 주문상태 출력하기

select order_id,
       product_id,
       out_date,
       case when out_date > '2022-05-01' then '출고대기'
            when out_date is null then '출고미정' else '출고완료' end as 출고여부
from food_order
order by 1

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

select b.category,
       sum(bs.sales) as total_sales
from book b
join book_sales bs using(book_id)
where bs.sales_date like '2022-01%'
group by category
order by 1

0개의 댓글