[MySQL] ALL

테이블

orders

order_number customer_number
1 1
2 2
3 3
4 3

문제

  • 가장 주문 횟수가 많은 고객을 뽑으시오

-- 1위 한 명만
SELECT o.customer_number
FROM orders o 
GROUP BY o.customer_number 
ORDER BY COUNT(*) DESC 
LIMIT 1
;

-- 1위 여러 명
SELECT customer_number 
FROM orders 
GROUP BY customer_number
HAVING COUNT(customer_number) >= ALL 
(SELECT COUNT(customer_number) FROM orders GROUP BY customer_number)
;

스키마

Create table If Not Exists orders (order_number int, customer_number int);
Truncate table orders;
insert into orders (order_number, customer_number) values (1, 1);
insert into orders (order_number, customer_number) values (2, 2);
insert into orders (order_number, customer_number) values (3, 3);
insert into orders (order_number, customer_number) values (4, 3);

links

social