[2주차]SQL_sum, avg, count, min, max, groub by, order by

김수경·2023년 12월 6일

SQL

목록 보기
2/6

1. 기본연산 : sum, avg, count, min, max

  • 합계 : sum(colum)
  • 평균 : avg(colum)
  • 갯수세기 : count
    데이터 갯수 : count(colum) 컬럼명 대신 1 혹은 * 사용 가능
    몇개의 값을 가지고 있는지 구할 때 : count(distinct colum)
    ex) 주문건수와 주문한 고객 수 구하기
    select count(1) count_of_orders,
    count(distinct customer_id) count_of_customers
    form food_orders
    

2. group by, order by

  • group by : 카테고리 컬럼을 묶음
    ex) 음식 종류별 주문금액 합계
    select cuisine_type,
    sum(price) sum_of_price
    from food_orders
    group by cuisine_type
  • order by : 컬럼 값 오름/내림차순 (내림 : desc)
    ex) 음식점별 주문 금액 최댓값 조회하기 - 최댓값 기준으로 내림차순 정렬
    select restaurant_name,
       max(price) "최대 주문금액"
       from food_orders
       group by restaurant_name
       order by max(price) desc

[실습] 음식 종류별 가장 높은 주문 금액과 가장 낮은 주문금액을 조회하고, 가장 낮은 주문금액 순으로 (내림차순) 정렬하기

select cuisine_type,
max(price) max_price,
min(price) min_price
from food_orders
group by cuisine_type 
order by min_price DESC 

profile
잘 하고 있는겨?

0개의 댓글