[DB]4. SELECT 복수행 함수

김주희's avatar
Feb 26, 2025
[DB]4. SELECT 복수행 함수

1. 세로 연산 원리

그룹 함수를 쓰면 세로 연산된다.
세로 연산되면 새로운 하나의 줄을 만들어낸다 (프로젝션 아님)
새로운 줄에, 다른 컬럼이 표현되고, 값이 같으면 squash 된다.
그룹핑 과정 예시
 

2. Group by 해서 연산하기

  • 전체 행을 세로 연산하려면? - sum, max, min, count, avg (그룹 함수)
  • 특정 행들을 세로 연산하려면? - where & 그룹 함수
  • 특정 그룹만 세로 연산하려면? - where & 그룹 함수
  • 그룹별로 세로 연산하려면? - group by & 그룹 함수
Group by, Union All
 

3. 복수행 함수

1. 그룹 함수

-- 세로 연산 -- 1. 그룹 함수 select avg(height) from student; select count(height) from student; select sum(height) from student; select min(height) from student; select max(height) from student;
notion image
notion image
notion image
notion image
notion image
 

2. 원하는 행만 세로 연산하기

-- 2. 원하는 행만 세로 연산하기 select avg(weight) from student where year(birthday) = 1975;
notion image
 
select floor(avg(pay)) from professor where position = "정교수";
notion image
 
-- projection(X) 연산(O) -> 그룹함수 옆에 컬럼 수가 불가 -- 새로운 행에 avg연산 결과가 있는 것 select avg(height) from student;
notion image
 

3. 그룹핑 하기

-- 3. 그룹핑 하기 (서브쿼리로 해결하는게 좋다) -- 스쿼시 가능하기 때문에 deptno 출력됨 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 avg(sal), deptno from emp group by deptno;
notion image
 

4. 그룹핑 원리 실습

-- 4. 그룹핑 원리 실습 -- 학년별 키의 평균 select avg(height), grade from student group by grade;
notion image
 
주민번호(jumin) 앞 두 자리 또한 학년(grade)에 따라 같은 값이므로 주민번호(앞 두 자리) 하나의 값으로 squash 가능하다.
-- group by -> grade로 정렬부터 됨 select avg(height), grade, substr(jumin,1,2) from student group by grade, substr(jumin,1,2);
notion image
 
select job, deptno, avg(sal) from emp group by job, deptno;
notion image
 

+)having

  • having: 그룹핑된 거에 대한 where 조건
-- 그룹핑된 거에 대해서 where과 같은 기능을 쓰고 싶을 때 = having select job, deptno, avg(sal) from emp group by job, deptno having deptno != 10;
notion image
 
-- 평균 연봉이 2000이상인 부서는? select avg(sal), deptno from emp group by deptno having avg(sal) > 2000;
notion image
 
 
전체
-- 세로 연산 -- 1. 그룹 함수 select avg(height) from student; 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 floor(avg(pay)) from professor where position = "정교수"; -- projection(X) 연산(O) -> 그룹함수 옆에 컬럼 수가 불가 -- 새로운 행에 avg연산 결과가 있는 것 select avg(height) from student; -- 3. 그룹핑 하기 (서브쿼리로 해결하는게 좋다) -- 스쿼시 가능하기 때문에 deptno 출력됨 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 avg(sal), deptno from emp group by deptno; select * from emp; -- 4. 그룹핑 원리 실습 -- 학년별 키의 평균 select avg(height), grade from student group by grade; -- group by -> grade로 정렬부터 됨 select avg(height), grade, substr(jumin,1,2) from student group by grade, substr(jumin,1,2); select job, deptno, avg(sal) from emp group by job, deptno; -- 그룹핑된 거에 대해서 where과 같은 기능을 쓰고 싶을 때 = having select job, deptno, avg(sal) from emp group by job, deptno having deptno != 10; -- 평균 연봉이 2000이상인 부서는? select avg(sal), deptno from emp group by deptno having avg(sal) > 2000;
Share article

jay0628