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 시작하기

6장. 그룹화 기준 및 주문 기준

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

데이터집계(데이터 롤업, 요약 또는 그룹화라고도 함)는 여러 레코드에서 일종의 합계를 만드는 것입니다. 합계, 최소, 최대, 카운트 및 평균은 일반적인 집계 연산입니다. SQL에서는 이러한 합계를 지정된 열에 그룹화할 수 있으므로 이러한 집계 범위를 쉽게 제어할 수 있습니다.

레코드 그룹화

먼저 가장 간단한 집계, 즉 테이블의 레코드 수를 계산합니다. SQL 편집기를 열고 station data 에 대한 레코드 수 를 계산합니다:

SELECT COUNT(*) AS record_count FROM station_data;

COUNT(*) 은 레코드를 계산하는 것을 의미합니다. WHERE 과 같은 다른 SQL 연산과 함께 사용할 수도 있습니다. 토네이도가 있었던 레코드 수를 계산하려면 다음과 같이 입력합니다:

SELECT COUNT(*) AS record_count FROM station_data
WHERE tornado = 1;

토네이도가 발생한 3,000개의 레코드를 확인했습니다. 하지만 그 수를 연도별로 구분하고 싶다면 어떻게 해야 할까요(그림 6-1)? 이 쿼리로도 그렇게 할 수 있습니다:

SELECT year, COUNT(*) AS record_count FROM station_data
WHERE tornado = 1
GROUP BY year;
Getting a tornado count by year
그림 6-1. 연도별 토네이도 횟수 파악하기

이 데이터가 갑자기 더 의미 있게 느껴집니다. 이제 토네이도 목격 횟수를 연도별로 볼 수 있습니다. 이 쿼리를 분석하여 어떻게 이런 일이 발생했는지 알아보겠습니다.

먼저 year 를 선택한 다음 레코드에서 COUNT(*) 을 선택하고 tornado 이 참인 레코드만 필터링합니다:

SELECT year, COUNT(*) AS record_count FROM station_data
WHERE tornado = 1
GROUP BY year;

그러나 year 에서 그룹화한다고 명시합니다. 이렇게 하면 연도별로 레코드 수를 효과적으로 계산할 수 있습니다. 굵게 강조 표시된 마지막 줄이 이 그룹화를 수행합니다:

SELECT year, COUNT(*) AS record_count FROM station_data
WHERE tornado = 1
GROUP BY year;

이 데이터를 둘 이상의 필드로 분할할 수 있습니다. yearmonth 을 기준으로 카운트를 원한다면 month 필드에서도 그룹화할 수 있습니다(그림 6-2):

SELECT year, month, COUNT(*) AS record_count FROM station_data
WHERE tornado = 1
GROUP BY year
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