목차
- 연산을 통해 새로운 Column 만들기
- 사칙연산, 합계와 평균, 데이터 갯수, 최솟값과 최댓값
사칙연산
종류

예시
select food_preparation_time,
delivery_time,
food_preparation_time + delivery_time as total_time
// 두 값을 더한 값을 total_time이라는 Column으로 부르기
from food_orders
합계와 평균
합계
sum(컬럼)
평균
avg(컬럼)
예시
select sum(food_preparation_time) as total_food_preparation_time,
// 합계를 total_food_preparation_time이라는 Column으로 부르기
avg(delivery_time) as avg_food_preparation_time
// 평균을 avg_food_preparation_time이라는 Column으로 부르기
from food_orders
데이터 갯수세기 : count(컬럼)
사용하는 경우
- 테이블 안의 모든 데이터의 개수를 구하고 싶을 경우 사용
사용 방법
count(1) 혹은 count(*) 사용
예시
- count_of_orders : 전체 주문 수
- count_of_customers : 주문한 손님의 수
select count(1) as count_of_orders,
count(distinct customer_id) as count_of_customers
// customer_id의 종류의 개수를 count_of_customers라는 Column으로 부르기
// customer_id의 전체 개수가 아닌 종류의 개수를 구하고 싶을 때 distinct 사용
from food_orders
데이터 중복 제거하기 : Distinct
최솟값과 최댓값
최솟값
min(컬럼)
최댓값
max(컬럼)
예시
select min(price) as min_price,
max(price) as max_price
from food_orders
참고사항
- select에서 연산을 수행할 값을 사용할 경우
해당 Query에서 사용 가능한 Column이 생성되는데 as를 통해 별명을 지정해주지 않으면
이름은 연산식으로 되므로 가급적 별명을 지어주자