SQL 집계 함수
1. SUM()
- department별 총합을 반환
SELECT department, SUM(salary) AS total_salary
FROM employees
GROUP BY department;
# 결과
+------------+--------------+
| department | total_salary |
+------------+--------------+
| HR | 150000 |
| IT | 200000 |
| Sales | 250000 |
+------------+--------------+
2. AVG()
- department별 평균을 반환
SELECT department, AVG(salary) AS avg_salary
FROM employees
GROUP BY department;
# 결과
+------------+------------+
| department | avg_salary |
+------------+------------+
| HR | 30000 |
| IT | 25000 |
| Sales | 25000 |
+------------+------------+
3. MIN()
- department별 최솟값을 반환
SELECT department, MIN(salary) AS min_salary
FROM employees
GROUP BY department;
# 결과
+------------+------------+
| department | min_salary |
+------------+------------+
| HR | 25000 |
| IT | 20000 |
| Sales | 22000 |
+------------+------------+
3. MAX()
- department별 최댓값을 반환
SELECT department, MAX(salary) AS max_salary
FROM employees
GROUP BY department;
# 결과
+------------+------------+
| department | max_salary |
+------------+------------+
| HR | 50000 |
| IT | 40000 |
| Sales | 30000 |
+------------+------------+
5. COUNT()
- department별 레코드 수를 반환
SELECT department, COUNT(*) AS employee_nums
FROM employees
GROUP BY department;
# 결과
+------------+---------------+
| department | employee_nums |
+------------+---------------+
| HR | 5 |
| IT | 8 |
| Sales | 10 |
+------------+---------------+
'Language > MySQL' 카테고리의 다른 글
이동 평균, 누적 합계 (2) | 2024.01.31 |
---|---|
SQL 순위 함수 (0) | 2024.01.28 |
SQL 일반 함수 (0) | 2024.01.22 |
SQL 날짜형 함수 (0) | 2024.01.21 |
SQL 문자형 함수 (0) | 2024.01.20 |