조건부 로직으로 범주 만들기

AI로 배우는 중급 SQL 쿼리

Jasmin Ludolf

Senior Data Science Content Developer

조건부 로직을 위한 CASE

|duration|
|--------|
|123     |
|110     |
|151     |
...
  • "Short", "Standard", "Epic"으로 분류

  • duration > 90: 예/아니오

  • 조건부 로직 사용

  • 더 읽기 쉽고 전달 쉬움

양동이

AI로 배우는 중급 SQL 쿼리

CASE 문을 위한 나쁜 프롬프트

프롬프트: duration이 90분 미만이면 Short, 150 미만이면 Standard, 그 외는 Epic으로 분류

SELECT 
    id, 
    title, 
    duration,
    CASE 
        WHEN duration < 90 THEN 'Short'
        WHEN duration < 150 THEN 'Standard'
        ELSE 'Epic'
    END AS film_category
FROM films;
AI로 배우는 중급 SQL 쿼리

CASE 문을 위한 나쁜 프롬프트

프롬프트: duration이 90분 미만이면 Short, 150 미만이면 Standard, 그 외는 Epic으로 분류

SELECT 
    id, 
    title, 
    duration,
    CASE 
        WHEN duration < 150 THEN 'Standard'
        WHEN duration < 90 THEN 'Short'
        ELSE 'Epic'
    END AS film_category
FROM films;

$$

  • 오류 위험
  • 둘 다 만족 시 잘못 분류
    • 예: 90 미만이면서 150 미만
AI로 배우는 중급 SQL 쿼리

CASE 문을 위한 더 나은 프롬프트

프롬프트: 90분 미만은 Short, 90~149분은 Standard, 150분 이상은 Epic, 그 외는 unknown으로 분류

SELECT 
    id, 
    title, 
    duration,
    CASE 
        WHEN duration < 90 THEN 'Short'
        WHEN duration BETWEEN 90 AND 149 THEN 'Standard'
        WHEN duration >= 150 THEN 'Epic'
        ELSE 'Unknown'
    END AS category
FROM films;
AI로 배우는 중급 SQL 쿼리

로직 검증

$$

|id|title                                           |duration|category|
|--|------------------------------------------------|--------|--------|
|1 |Intolerance: Love's Struggle Throughout the Ages|123     |Standard|
|2 |Over the Hill to the Poorhouse                  |110     |Standard|
|3 |The Big Parade                                  |151     |Epic    |
|4 |Metropolis                                      |145     |Standard|
...
|id  |title  |duration|category|
|----|-------|--------|--------|
|4396|Destiny|        |Unknown |
...
AI로 배우는 중급 SQL 쿼리

NULL 필터링

프롬프트: NULL duration 제거

SELECT 
    id, 
    title, 
    duration,
    CASE 
        WHEN duration < 90 THEN 'Short'
        WHEN duration BETWEEN 90 AND 149 THEN 'Standard'
        WHEN duration >= 150 THEN 'Epic'
        ELSE 'Unknown'
    END AS category
FROM films

WHERE duration IS NOT NULL;
AI로 배우는 중급 SQL 쿼리

범주의 이점

$$

  • 의미 있는 라벨로 결과 명확화

$$

  • 커뮤니케이션 향상

$$

  • 더 깔끔한 시각화 가능

별

AI로 배우는 중급 SQL 쿼리

연습해 봅시다!

AI로 배우는 중급 SQL 쿼리

Preparing Video For Download...