Today I Learn
: 코드카타, 5회차 sql 세션 복습 후 과제, 6회차 sql 세션 복습
- 코드카타
- 문제 1 프로그래머스 동명 동물 수 찾기
1. 문제 링크: https://school.programmers.co.kr/learn/courses/30/lessons/59041
2. 정답 코드:
select
name,
count(distinct animal_id) as duplicated_cnt
from animal_ins
where name is not null
group by name
having count(distinct animal_id) >= 2
order by name
3. 오류 상황: 조건을 제대로 못봐서 where절과 order by절을 빠뜨림
4. 시도 방법: 문제와 조건을 다시 읽고 조건문을 추가함
- 문제 2 프로그래머스 상위 n개 레코드
1. 문제 링크: https://school.programmers.co.kr/learn/courses/30/lessons/59405
2. 정답 코드:
select name
from animal_ins
order by datetime
limit 1
- 문제 3 프로그래머스 최솟값 구하기
1. 문제 링크: https://school.programmers.co.kr/learn/courses/30/lessons/59038
2. 정답 코드:
select datetime
from animal_ins
order by datetime
limit 1
select min(datetime)
from animal_ins
- 문제 4 프로그래머스 어린 동물 찾기
1. 문제 링크: https://school.programmers.co.kr/learn/courses/30/lessons/59037
2. 정답 코드:
select animal_id, name
from animal_ins
where intake_condition <> 'aged'
order by animal_id
- SQL 5회차 세션
(코드: https://github.com/rladbstj56/2025_main_bootcamp/blob/main/SQL_session/251230_5th)
- case
case 컬럼
when 값1 then 결과1
when 값2 then 결과2
else 결과3
end
case
when 조건1 then 결과1
when 조건2 then 결과2
else 결과3
end
- 조건부 카운트 (case를 '집계'에 섞어보기)
SELECT
COUNT(*)AS total_enrollments,
SUM(CASE WHEN coupon_code IS NOT NULL THEN 1 ELSE 0 END)AS coupon_used_cnt,
SUM(CASE WHEN coupon_code IS NULL THEN 1 ELSE 0 END)AS no_coupon_cnt
FROM basic.enrollments;
- Subquery, 상관 서브쿼리, FROM 서브쿼리
- EXISTS / NOT EXISTS
- CTE (WITH)
https://myun0506.tistory.com/50
서브쿼리(상관/FROM), IN, EXISTS 함수, CTE(WITH)
- SubqueryScalar Subquery 스칼라 서브쿼리서브쿼리 안의 결과는 값 1개여야함# 미니 실습 4 select enrollment_id, final_price from basic.enrollments where final_price > (select avg(final_price) from basic.enrollments) order by final_pr
myun0506.tistory.com
- SQL 6회차 세션
(코드: https://github.com/rladbstj56/2025_main_bootcamp/blob/main/SQL_session/251231_6th)
- ifnull / coalesce 함수
- 윈도우 함수
MySQL에서 윈도우함수는 select 리스트 / order by 딱 이렇게 두가지 경우에서만 사용 가능함!
https://myun0506.tistory.com/51
Ifnull / Coalesce 함수, 윈도우 함수
- ifnull / coalesce 함수윈도우 함수를 쓸 때 null이 자주 발생하므로 ifnull 함수와 coalesce 함수 자주 함께 사용ifnull : 값 1개만 넣고 그 값이 null일 경우 치환할 다른 한 값 1개coalesce : 값 여러개 넣을 수
myun0506.tistory.com
'[데이터분석] 부트캠프 TIL' 카테고리의 다른 글
| 20260102 TIL (1) | 2026.01.02 |
|---|---|
| 20260101 TIL (0) | 2026.01.01 |
| 20251230 TIL (0) | 2025.12.30 |
| 20251229 TIL (0) | 2025.12.29 |
| 20251226 TIL (1) | 2025.12.26 |