하나 이상의 행을 그룹으로 묶어서 결과를 나타냅니다
SELECT 문 뒤에 작성하고 , 여러 그룹 함수를 쉼표로 구분하여 함께 사용할 수 있습니다
SELECT group_function (column) ...
FROM table_name
그룹 함수는 해당 칼럼 값이 null 인것을 제외하고 계산합니다.
SELECT SUM(sal)
FROM emp;
SELECT TRUNC(AVG(sal),2) "평균 급여"
FROM emp;
SELECT MAX(sal) "최고 급여", MIN(sal) " 최소 급여"
FROM emp;
SELECT COUNT(*) "전체 사원수", COUNT(comm) "커미션 대상"
FROM emp;
SELECT COUNT(DISTINCT job) "업무수" -- DISTINCT 중복제거 사용
FROM emp;
어떤 컬럼 값을 기준으로 그룹 함수를 적용할 수 있습니다
SELECT 컬럼명, 그룹함수
FROM 테이블명
WHERE 조건
GROUP BY 컬럼명
SELECT deptno
FROM emp
GROUP BY deptno;
SELECT deptno , AVG(sal)
FROM emp
GROUP BY deptno;
SELECT deptno, MAX(sal) "최고 급여", MIN(sal) "최소 급여"
FROM emp
GROUP BY deptno;
SELECT deptno, COUNT(*), COUNT(comm)
FROM emp
GROUP BY deptno
ORDER BY deptno;
SELECT deptno, TRUNC(AVG(sal), 1)
FROM emp
GROUP BY deptno
HAVING AVG(sal)>=2000;
SELECT MIN(hiredate) "최근 입사한 사원",MAX(hiredate)"가장 오래된 사원"
FROM emp;
SELECT deptno, COUNT(comm)
FROM emp
GROUP BY deptno;
SELECT AVG(sal), MAX(sal),MIN(sal), SUM(sal)
FROM emp
WHERE job LIKE 'SALESMAN';
SELECT deptno, COUNT(*), TRUNC(AVG(sal), 1), MIN(sal),MAX(sal), SUM(sal)
FROM emp
GROUP BY deptno
ORDER BY SUM(sal) DESC;
SELECT JOB, TRUNC(AVG(sal), 1),SUM(sal)
FROM emp
GROUP BY JOB
HAVING AVG(sal)>=3000;
SELECT job, SUM(sal)
FROM emp
WHERE job NOT LIKE 'SALE%'
GROUP BY job
HAVING SUM(sal) > 4000
ORDER BY SUM(sal) DESC;