집계함수란? 인수로 집합을 지정하여 집합함수라고도 불린다. 집합을 특정 방법으로 계산하여 결과를 반환한다.
# 대표적인 집계함수
COUNT(집합)
SUM(집합)
AVG(집합)
MIN(집합)
MAX(집합)
SELECT COUNT(집합) FROM 테이블명
# 행 개수 구하기
mysql> SELECT COUNT(*) FROM sample51;
+----------+
| COUNT(*) |
+----------+
| 5 |
+----------+
1 row in set (0.00 sec)
# 행 개수 구하기
mysql> SELECT * FROM sample51 WHERE name = 'a';
+------+------+----------+
| no | name | quantity |
+------+------+----------+
| 1 | A | 1 |
| 2 | A | 2 |
+------+------+----------+
2 rows in set (0.00 sec)
mysql> SELECT COUNT(*) FROM sample51 WHERE name = 'a';
+----------+
| COUNT(*) |
+----------+
| 2 |
+----------+
1 row in set (0.00 sec)
mysql> SELECT * FROM sample51;
+------+------+----------+
| no | name | quantity |
+------+------+----------+
| 1 | A | 1 |
| 2 | A | 2 |
| 3 | B | 10 |
| 4 | C | 3 |
| 5 | NULL | NULL |
+------+------+----------+
5 rows in set (0.00 sec)
mysql> SELECT COUNT(no), COUNT(name) FROM sample51;
+-----------+-------------+
| COUNT(no) | COUNT(name) |
+-----------+-------------+
| 5 | 4 | 👉 NULL은 무시된다.
+-----------+-------------+
1 row in set (0.01 sec)
mysql> SELECT ALL name FROM sample51;
+------+
| name |
+------+
| A |
| A |
| B |
| C |
| NULL |
+------+
5 rows in set (0.00 sec)
✔️ DISTINCT 지정, 인수가 아니므로 콤마는 붙이지 않는다.
mysql> SELECT DISTINCT name FROM sample51;
+------+
| name |
+------+
| A | 👉 중복 제거
| B |
| C |
| NULL |
+------+
4 rows in set (0.00 sec)
✔️ NULL 값을 제외하고 중복하지 않는 데이터의 개수를 구하는 경우
mysql> SELECT COUNT(ALL name), COUNT(DISTINCT name) FROM sample51;
+-----------------+----------------------+
| COUNT(ALL name) | COUNT(DISTINCT name) |
+-----------------+----------------------+
| 4 | 3 | 👉 NULL을 제외한 값의 개수
+-----------------+----------------------+
1 row in set (0.00 sec)
# 합계 구하기
mysql> SELECT * FROM sample51;
+------+------+----------+
| no | name | quantity |
+------+------+----------+
| 1 | A | 1 |
| 2 | A | 2 |
| 3 | B | 10 |
| 4 | C | 3 |
| 5 | NULL | NULL |
+------+------+----------+
5 rows in set (0.00 sec)
mysql> SELECT SUM(quantity) FROM sample51;
+---------------+
| SUM(quantity) |
+---------------+
| 16 |
+---------------+
1 row in set (0.00 sec)
# 평균값 구하기
mysql> SELECT AVG(quantity), SUM(quantity)/COUNT(quantity) FROM sample51;
+---------------+-------------------------------+
| AVG(quantity) | SUM(quantity)/COUNT(quantity) |
+---------------+-------------------------------+
| 4.0000 | 4.0000 |
+---------------+-------------------------------+
✔️ NULL 값을 포함하여 평균을 내고 싶다면 ?
mysql> SELECT AVG(CASE WHEN quantity IS NULL THEN 0 ELSE quantity END) AS avgnull0
+----------+
| avgnull0 |
+----------+
| 3.2000 |
+----------+
1 row in set (0.00 sec)
# 최솟값 / 최댓값 구하기
mysql> SELECT MIN(quantity), MAX(quantity), MIN(name), MAX(name) FROM sample51;
+---------------+---------------+-----------+-----------+
| MIN(quantity) | MAX(quantity) | MIN(name) | MAX(name) |
+---------------+---------------+-----------+-----------+
| 1 | 10 | A | C |
+---------------+---------------+-----------+-----------+
1 row in set (0.01 sec)
# GROUP BY 구에 name 열 지정하여 그룹화
✔️ SELECT 구에 name 열을 지정하였으므로 그룹화된 name 열의 데이터가 클라이언트로 반환된다.
mysql> SELECT name FROM sample51 GROUP BY name;
+------+
| name |
+------+
| A |
| B |
| C |
| NULL |
+------+
4 rows in set (0.00 sec)
mysql> SELECT * FROM sample51;
+------+------+----------+
| no | name | quantity |
+------+------+----------+
| 1 | A | 1 |
| 2 | A | 2 |
| 3 | B | 10 |
| 4 | C | 3 |
| 5 | NULL | NULL |
+------+------+----------+
5 rows in set (0.00 sec)
mysql> SELECT name, COUNT(name), SUM(quantity)
-> FROM sample51 GROUP BY name;
+------+-------------+---------------+
| name | COUNT(name) | SUM(quantity) |
+------+-------------+---------------+
| A | 2 | 3 |
| B | 1 | 10 |
| C | 1 | 3 |
| NULL | 0 | NULL |
+------+-------------+---------------+
4 rows in set (0.00 sec)
mysql> SELECT name, COUNT(name) FROM sample51 GROUP BY name;
+------+-------------+
| name | COUNT(name) |
+------+-------------+
| A | 2 |
| B | 1 |
| C | 1 |
| NULL | 0 |
+------+-------------+
4 rows in set (0.00 sec)
mysql> SELECT name, COUNT(name) FROM sample51
-> GROUP BY name HAVING COUNT(name) = 1;
+------+-------------+
| name | COUNT(name) |
+------+-------------+
| B | 1 |
| C | 1 |
+------+-------------+
2 rows in set (0.01 sec)
mysql> SELECT no, name, quantity FROM sample51 GROUP BY name;
ERROR 1055 (42000) ❗️
mysql> SELECT * FROM sample51;
+------+------+----------+
| no | name | quantity |
+------+------+----------+
| 1 | A | 1 | 👉 name 열 값이 A인 그룹의 quantity 열 값은 2개
| 2 | A | 2 | ✔️ 어느 값을 출력해야 하는지 모른다.
| 3 | B | 10 |
| 4 | C | 3 |
| 5 | NULL | NULL |
+------+------+----------+
5 rows in set (0.00 sec)
mysql> SELECT MIN(no), name, SUM(quantity) FROM sample51 GROUP BY name;
+---------+------+---------------+
| MIN(no) | name | SUM(quantity) |
+---------+------+---------------+
| 1 | A | 3 |
| 3 | B | 10 |
| 4 | C | 3 |
| 5 | NULL | NULL |
+---------+------+---------------+
4 rows in set (0.00 sec)
# name 열로 그룹화하여 합계를 구하고 내림차순으로 정렬
mysql> SELECT name, COUNT(name), SUM(quantity)
-> FROM sample51 GROUP BY name ORDER BY SUM(quantity) DESC;
+------+-------------+---------------+
| name | COUNT(name) | SUM(quantity) |
+------+-------------+---------------+
| B | 1 | 10 |
| A | 2 | 3 |
| C | 1 | 3 |
| NULL | 0 | NULL |
+------+-------------+---------------+
4 rows in set (0.00 sec)