customers 테이블
orders 테이블

각 고객별로 주문 금액 최대값이 600달러 이상인 데이터만 가져와서 내림차순으로 정렬하려고 한다.

이때 group by 뒤에는 where이 아니라 having을 써준다.

select c.first_name, c.last_name, max(o.amount) as MAX
from customers c
join orders o
	on c.id = o.customer_id
group by c.id having max(o.amount) >= 600
order by MAX desc;

+ Recent posts