
- 테이블 뭐 있는지 기억 안날 때.
show tables; #또는 select * from information_schema.tables WHERE table_schema = 'sparta'
- 테이블 안에 있는 데이터의 갯수
- 모든 데이터: count(1) or count(*)
- 모든 데이터를 불러 올 시, Null 값 까지 집계 된다.
- 특정 컬럼을 지정하면 Null은 제외
- distinct (컬럼) : 컬럼의 갯수 (중복 된 갯수는 빼고 나타낸다)
select count(1) count_of_orders, count(distinct customer_id) count_of_customers from food_orders
- 가장 최근, 오래된에도 적용 된다.
#최소, 최대 값 select min(컬럼) min_price, max(컬럼) max_price from food_orders \ #평균 값 구하기 select AVG(price) avg_of_koreanFood from food_orders WHERE cuisine_type = 'Korean'
- 여러번의 Query 없이, 카테고리를 지정하여 수식 함수로 연산
#cuisine_type별 음식의 합계 값 select cuisine_type, sum(price) sum_of_price from food_orders group by cuisine_type \ #음식점 별 max 주문 금액 select restaurant_name '가게 이름', max(price) '주문 금액 최댓값' from food_orders group by restaurant_name \ #결제 타입별 가장 최근 결제일 조회 SELECT pay_type '결제수단', max(date) '가장 최근 결제일' FROM payments group by pay_type
- 정렬하기
- 기본값: ASC(오름차순)
- 내림차순: DESC
- 여러개의 컬럼을 정렬 기준으로 둘 수 있다.
#이름으로 정렬한 다음에, 성별로도 정렬 select * FROM customers ORDER BY NAME, gender
SELECT > FROM > WHERE > GROUP BY > ORDER BY
SELECT cuisine_type, sum(delivery_time) total_delivery_time FROM food_orders WHERE day_of_the_week = 'Weekend' GROUP BY cuisine_type ORDER BY sum(delivery_time) desc / SELECT age, count(name) count_of_name FROM customers WHERE age BETWEEN 20 and 40 GROUP BY age ORDER BY age
- 음식 종류별 가장 높은 주문 금액과 가장 낮은 주문금액을 조회
- 가장 낮은 주문금액 순으로 (내림차순) 정렬하기
- 음식 종류 별> Select cuisine_type / Group by cuisine_type
- 가장 높은 금액, 낮은 금액 > max(price), min(price)
- 가장 낮은 주문 금액 순으로 내림차순 > Order by min(price) DESC
select cuisine_type '음식 종류', max(price) '가장 높은 금액', min(price) '가장 낮은 금액' FROM food_orders fo Group by cuisine_type ORDER BY min(price) DESC