[TIL]_2025.04.09 본캠프 52일차 (1) : 코드카타 SQL

JIYUU·2025년 4월 9일

문제 99번. [Number of Unique Subjects Taught by Each Teacher]
(https://leetcode.com/problems/number-of-unique-subjects-taught-by-each-teacher/)

  • 문제 요구 사항
    • 교수별로 중복 없이 과목 수 세기
    • 결과 순서는 자유
select teacher_id, count(distinct subject_id) as cnt
from teacher
group by teacher_id

문제 100번. [User Activity for the Past 30 Days I]
(https://leetcode.com/problems/user-activity-for-the-past-30-days-i/description/)

  • 문제 요구 사항
    • 2019-06-28 ~ 2019-07-27 기간 동안
    • 날짜별로 활동한 사용자 수 세기
    • 결과 순서는 자유
select 
    activity_date as day,
    count(distinct user_id) as active_users
from activity
where activity_date >= '2019-06-28' and activity_date < '2019-07-28'
group by activity_date

문제 101번. [Product Sales Analysis III]
(https://leetcode.com/problems/product-sales-analysis-iii/description/)

  • 문제 요구 사항
    • 각 product_id마다
    • 가장 처음 판매된 연도(year)의
    • product_id, year, quantity, price를 선택
    • 정렬 순서는 자유
select product_id, year as first_year, quantity, price
from sales
where (product_id, year) in (
    select product_id, min(year) 
    from sales 
    group by product_id)

이 문제는 약간 함정이 있었는데, 테이블은 두 개가 주어지지만 join을 하지 않아도 풀 수 있는 문제였다.

0개의 댓글