Contents
Group by 연산Group by 연산
- 전체 행을 세로 연산 하려면 무엇을 써 ? - sum, max, min, count, avg(그룹 함수)
- 특정 행들을 세로 연산 하려면 무엇을 써 ? - where과 그룹 함수
- 특정 그룹만 세로 연산 하려면 무엇을 써 ? - where과 그룹 함수
- 그룹 별로 세로 연산 하려면 무엇을 써 ? - group by와 그룹 함수
Group by, Union All
-- 세로 연산
-- 1. 그룹 함수
-- 평균
select avg(height)
from student;
-- row 개수
select count(height)
from student;
-- 합
select sum(height)
from student;
-- 최소
select min(height)
from student;
-- 최대
select max(height)
from student;
-- 2. 원하는 행만 세로 연산하기
select avg(weight)
from student
where year(birthday) = 1975;
select *
from professor;
select floor(avg(pay))
from professor
where position = '정교수';
select avg(height)
from student;
select *
from student;
-- 3. 그룹핑 하기
select avg(sal), deptno
from emp
where deptno = 10
union all
select avg(sal), deptno
from emp
where deptno = 20
union all
select avg(sal), deptno
from emp
where deptno = 30;
select deptno,avg(sal)
from emp
group by deptno;
-- 4. 그룹핑 원리 실습
select *
from student;
select grade, avg(height)
from student
group by grade;
select job, avg(sal), deptno
from emp
group by job, deptno;
select job, avg(sal), deptno
from emp
group by job, deptno
having deptno != 10; -- group by의 where절 이라고 생각하면 된다.
Share article