Skip to Content
SQL 시작하기
book

SQL 시작하기

by Thomas Nield
May 2025
Beginner to intermediate
134 pages
1h 46m
Korean
O'Reilly Media, Inc.
Content preview from SQL 시작하기

7장. 사례 설명

이 작품은 AI를 사용하여 번역되었습니다. 여러분의 피드백과 의견을 환영합니다: translation-feedback@oreilly.com

이제 SQL의 진정한 정의 기능인 JOIN 연산자를 배울 준비가 거의 다 되었습니다. 하지만 그 전에 장에서 CASE 이라는 매우 편리한 연산자에 대해 간단히 살펴볼 필요가 있습니다. 이 명령을 사용하면 하나 이상의 조건에 따라 열 값을 다른 값으로 바꿀 수 있습니다.

사례 설명

CASE 문을 사용하면 하나 이상의 조건을 각 조건에 해당하는 값에 매핑할 수 있습니다. CASE 이라는 단어로 CASE 문을 시작하고 END 으로 마무리합니다. 이러한 키워드 사이에 각 조건을 지정합니다. WHEN [condition] THEN [value]로 각 조건을 지정하며, 여기서 [condition] 와 해당 [value] 를 입력합니다. 조건-값 쌍을 지정한 후 조건이 충족되지 않을 경우 기본값으로 사용할 포괄적인 값을 ELSE 에 지정할 수 있습니다. 예를 들어 wind_speedwind_severity 카테고리로 분류할 수 있으며(그림 7-1), 여기서 40보다 큰 속도는 'HIGH', 30~40은 'MODERATE', 그 미만은 'LOW' 입니다:

SELECT report_code, year, month, day, wind_speed,

CASE
    WHEN wind_speed >= 40 THEN 'HIGH'
    WHEN wind_speed >= 30 AND wind_speed < 40 THEN 'MODERATE'
    ELSE 'LOW'
END as wind_severity

FROM station_data
Categorizing wind severity into HIGH, MODERATE, and LOW
그림 7-1. 강풍의 강도를 높음, 보통, 낮음으로 분류하기

AND wind_speed < 40 조건은 실제로 생략할 수 있습니다. 그 이유는 기계가 CASE 문을 위에서 아래로 읽은 후 가장 먼저 참이라고 판단되는 조건을 사용하는 것이기 때문입니다(이후 조건은 평가하지 않습니다). 따라서 wind_speed 이 43인 레코드가 있다면 'HIGH' 으로 평가될 것이라고 확신할 수 있습니다. 30보다 크더라도 그 지점까지 도달하지 않으므로 'MODERATE' 으로 할당되지 않습니다. 이를 알면 조금 더 효율적인 쿼리를 만들 수 있습니다:

SELECT report_code, year, month, day, wind_speed,

CASE
    WHEN wind_speed >= 40 THEN 'HIGH'
    WHEN wind_speed >= 30 THEN 'MODERATE'
    ELSE 'LOW'
END as wind_severity

FROM station_data

사례 문 그룹화

CASE 문을 만들고 그룹화하면 매우 강력한 변환을 만들 수 있습니다. 값을 집계하기 전에 하나 이상의 조건에 따라 값을 변환하면 데이터를 흥미로운 방식으로 ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

Oracle Database 11g Release 2 Performance Tuning Tips & Techniques

Oracle Database 11g Release 2 Performance Tuning Tips & Techniques

Richard Niemiec

Publisher Resources

ISBN: 9798341656307