본문 바로가기
IT/@SQL

Oracle 기본함수: 집계함수 (Order by, SUM, AVG, MAX, MIN, COUNT)

by He;story 2020. 5. 26.

SELECT 기본함수

https://awesomek.tistory.com/entry/Select-%EB%AC%B8-%EA%B8%B0%EB%B3%B8-%EB%AA%85%EB%A0%B9%EC%96%B4-%ED%99%9C%EC%9A%A9?category=865508 

 

Select 문 / 기본 명령어 활용

Oracle 에서 제공해주는 HR 계정으로 활용 Oracle과 SQL Developer 를 설치하면 HR 계정을 활용할 수 있습니다. https://awesomek.tistory.com/entry/Oracle-11g-release2-%EC%84%A4%EC%B9%98%ED%95%98%EA%B3%A0-%..

awesomek.tistory.com

 


EMPLOYEES 테이블

대부분의 실습은 EMPLOYEES 테이블로 하려합니다.(Oracle 에서 기본적으로 제공해주는 TABLE)

 

EMPLOYEES 테이블 COMMISSION 컬럼을 보면 NULL 되어 있는 사람도 있고 보너스 받는 사람도 있습니다.

Select distinct commission_pct from employees;

1. 보너스가 없는 사람들을 찾기

Select first_name, last_name, commission_pct from employees where commission_pct is null;

 

2. 보너스를 받는 사람들

Select first_name, last_name, commission_pct from employees where commission_pct is not null;


 

Order by 활용

 

EMPLOYEE_ID가 오림차순 정렬

Select Employee_id, last_name from Employees order by employee_id asc;

EMPLOYEE_ID가 내림차순 정렬

Select Employee_id, last_name from Employees order by employee_id desc;


SUM 함수

EMPLOYEES 테이블

 

EMPLOYEES 테이블에 모든 급여 합계

Select select sum(salary) from employees;


 

Count 함수

EMPLOYEES 테이블의 레코드 수

Select count(*) from Employees;

first_name 레코드 수와 중복 제거한 first_name 레코드 수 비교

select count(all first_name), count(distinct first_name) from employees;


AVG 함수

전체 사원의 연봉 평균

Select avg(salary) from Employees;

Department_id 가 80인 사람들의 연봉 평균

select avg(salary) from employees where department_id = 80;


 

MAX 최댓값 함수

제일 많은 연봉을 받는 금액

select max(salary) from employees;

가장 최근에 입사 날짜

select max(hire_date) from employees;

 

MAX를 활용한 조건부 출력

급여를 가장 많이 받는 사람의 Employee_id, first_name, last_name 출력

Select Employee_id, first_name, last_name from Employees where salary = max(salary); (잘못된 예)

where 절에서는 집계 함수를 사용할 수 없습니다.

 

 

MIN 최소값 함수

제일 적은 연봉을 받는 금액

select min(salary) from employees;

 

 

댓글