Today I Learn
: 코드카타, SQL 성취도 평가, 풀이 세션, SQL 달리기반 퀘스트
- 코드카타
- 문제 1 프로그래머스 경기도에 위치한 식품창고 목록 출력하기
1. 문제 링크: https://school.programmers.co.kr/learn/courses/30/lessons/131114
2. 정답 코드:
select warehouse_id, warehouse_name, address, ifnull(freezer_yn,'N')
from food_warehouse
where address like '경기도%'
order by warehouse_id
- 문제 2 프로그래머스 강원도에 위치한 생산공장 목록 출력하기
1. 문제 링크: https://school.programmers.co.kr/learn/courses/30/lessons/131112
2. 정답 코드:
select factory_id, factory_name, address
from food_factory
where address like '강원도%'
order by factory_id asc
- 문제 3 프로그래머스 DATETIME에서 DATE로 형변
1. 문제 링크: https://school.programmers.co.kr/learn/courses/30/lessons/59414#fn1
2. 정답 코드:
select animal_id, name, date_format(datetime, '%Y-%m-%d')
from animal_ins
order by animal_id
- 문제 4 프로그래머스 흉부외과 또는 일반외과 의사 목록 출력하기
1. 문제 링크: https://school.programmers.co.kr/learn/courses/30/lessons/132203
2. 정답 코드:
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
- 문제 5 프로그래머스 가격이 제일 비싼 식품의 정보 출력하기 (서브쿼리 사용)
1. 문제 링크: https://school.programmers.co.kr/learn/courses/30/lessons/131115
2. 정답 코드:
with ranked as (
select product_id, product_name, product_cd, category, price,
row_number() over (order by price desc) as rnk
from food_product
)
select product_id, product_name, product_cd, category, price
from ranked
where rnk = 1
select product_id, product_name, product_cd, category, price
from food_product
where price = (
select max(price)
from food_product
)
- SQL 사전 퀘스트 달리기반
- CTE 두개 중첩
- Lv4. 단골 고객님찾기 2번 문제 저번에 풀다가 포기함
- https://myun0506.tistory.com/17
20251230 TIL
Today I Learn: SQL 4회차 세션 복습 후 과제 제출, SQL 사전 퀘스트 달리기반, 코드카타, 온라인 세션 - 문길래 튜터님 - SQL 세션 4회차 (코드: https://github.com/rladbstj56/2025_main_bootcamp/blob/main/SQL_session/251229_
myun0506.tistory.com
- BUT!!!! 오늘은 성공했지롱
- 정답 쿼리에선 서브쿼리 두번 사용했지만 나는 CTE 두번 사용해서 성공함!!!

# 내가 직접 작성한 쿼리
with ranked as (
select
c.country,
c.customername,
sum(o.totalamount) as top_spent
from customers2 c
left join orders2 o
on c.customerid = o.customerid
group by c.country, c.customername
),
final_ranked as (
select country, customername, top_spent,
row_number() over (
partition by country order by top_spent desc)
as rnk
from ranked
order by country, customername
)
select country, customername, top_spent
from final_ranked
where rnk = 1
- 내 쿼리에서 final_ranked CTE 안에 ORDER BY가 들어가 있는데,
- CTE 내부의 ORDER BY는 최종 결과에 영향을 주지 않거나 엔진에 따라 에러를 낼 수 있음.
- ORDER BY는 가장 마지막 SELECT 문 다음에 쓰는 것이 표준!!!
# 정답 쿼리 -- 오류가 너무 많이 나고 복잡 BAD!!!
SELECT
c.Country,
c.CustomerName AS Top_Customer,
SUM(o.TotalAmount) AS Top_Spent
FROM
Customers c
JOIN
Orders o ON c.CustomerID = o.CustomerID
GROUP BY
c.Country, c.CustomerName
HAVING
SUM(o.TotalAmount) = (
SELECT
MAX(SumSpent)
FROM
(SELECT
SUM(o2.TotalAmount) AS SumSpent
FROM
Customers c2
JOIN
Orders o2 ON c2.CustomerID = o2.CustomerID
WHERE
c2.Country = c.Country
GROUP BY
c2.CustomerID) AS Subquery
);
- SQL 성취도 평가
: Github ( https://github.com/rladbstj56/2025_main_bootcamp/blob/main/SQL_session/260102_exam )
- 온보딩 주차 끝나는 기념 우리 3조 조원들과 티타임 ^^ㅎㅎ

'[데이터분석] 부트캠프 TIL' 카테고리의 다른 글
| 20260106 TIL (0) | 2026.01.06 |
|---|---|
| 20260105 TIL (0) | 2026.01.05 |
| 20260101 TIL (0) | 2026.01.01 |
| 20251231 TIL (0) | 2025.12.31 |
| 20251230 TIL (0) | 2025.12.30 |