SQL

group by, order by

content0474 2024. 8. 28. 14:09

SQL문의 구조

select  컬럼

from  테이블

where  조건

group by 컬럼

order by  컬럼

 

group by: 카테고리별로 

 

select restaurant_name, max(price)

from food orders

group by restaurant_name

:각 레스토랑마다 가장 비싼 음식가격을 보여줘

 

select cuisine_type, sum(price)

from food orders

group by cuisine_type

: 음식 종류별로 주문금액 합계를 알려줘

 

order by: 순서대로

 

select restaurant_name, max(price)

from food orders

group by restaurant_name

order by max(price)

:각 레스토랑마다 가장 비싼 음식가격을 보여주는데 값이 낮은것에서 높은것 순으로 오름차순으로 보여줘

 

select cuisine_type, sum(price)

from food orders

group by cuisine_type

order by sum(price) desc

: 음식 종류별로 주문금액 합계를 알려주는데 값이 높은것에서 낮은것 순으로 내림차순으로 보여줘

 

정렬 순서를 여러개로 만들수도 있다

 

select *

from customers

order by gender, name

: customers 테이블 전체를 보여주는데 gender 별로 정렬하고 gender 내에서 이름순으로 정렬해줘

 

select *

from customers

order by name, gender

: customers 테이블 전체를 보여주는데 이름순으로 정렬하고 이름이 같으면 gender별로 정렬해줘

'SQL' 카테고리의 다른 글

if, case  (0) 2024.08.29
replace, substring, concat  (1) 2024.08.28
기본 연산, 최대/최소, 개수 구하기  (0) 2024.08.27
where 절의 비교연산, between, in, like, 논리연산  (0) 2024.08.27
select, from  (0) 2024.08.27