group by 한 컬럼에 연산을 하는 것이 일반적인 사용.
ex1)
문제 : https://school.programmers.co.kr/learn/courses/30/lessons/164668
USED_GOODS_BOARD와 USED_GOODS_USER 테이블에서 완료된 중고 거래의 총금액이 70만 원 이상인 사람의 회원 ID, 닉네임, 총거래금액을 조회하는 SQL문을 작성해주세요. 결과는 총거래금액을 기준으로 오름차순 정렬해주세요.
select user_id, nickname, total_sales
from (SELECT user_id, nickname, sum(price) total_sales
from USED_GOODS_BOARD join USED_GOODS_USER
on USED_GOODS_USER.user_id = USED_GOODS_BOARD.writer_id
where status = 'DONE'
group by user_id) t1
where total_sales >= 700000
order by total_sales

하지만, max와 같이 group by를 썼을시에 max연산을 한 컬럼에만 적용이 된다. max연산을 한 컬럼 + @ 로 join을 해주어야 한다.
ex2)
문제 : https://school.programmers.co.kr/learn/courses/30/lessons/131123

REST_INFO 테이블에서 음식종류별로 즐겨찾기수가 가장 많은 식당의 음식 종류, ID, 식당 이름, 즐겨찾기수를 조회하는 SQL문을 작성해주세요. 이때 결과는 음식 종류를 기준으로 내림차순 정렬해주세요.
select rest_info.food_type, rest_info.rest_id,rest_info.rest_name,rest_info.favorites
from (SELECT food_type, rest_id, rest_name, max(FAVORITES) favorites
from rest_info
group by food_type) tc join rest_info
-- rest_info와 group by를 사용 + max()연산한 컬럼을 조회한 테이블과 join
on tc.favorites=rest_info.favorites and
tc.food_type=rest_info.food_type
order by rest_info.food_type desc
