데이터 분석 기술 블로그

서브쿼리 기초 2 본문

SQL/문제풀이

서브쿼리 기초 2

데이터분석가 이채은 2024. 12. 26. 19:17

문제 2: 중복 데이터 필터링

데이터셋: orders

order_id customer_id total_amount
1 101 300
2 102 500
3 101 700
4 103 200
5 102 1000

 

문제

각 고객(customer_id)의 **가장 큰 주문 금액(total_amount)**을 출력하세요.

 

CREATE TABLE orders (
    order_id INT PRIMARY KEY,
    customer_id INT,
    total_amount INT
);

INSERT INTO orders (order_id, customer_id, total_amount) VALUES
(1, 101, 300),
(2, 102, 500),
(3, 101, 700),
(4, 103, 200),
(5, 102, 1000);
SELECT customer_id, total_amount
FROM orders o1
WHERE total_amount = (SELECT MAX(o2.total_amount) FROM orders o2 WHERE o1.customer_id = o2.customer_id)

'SQL > 문제풀이' 카테고리의 다른 글

Monthly Transactions I _ Medium. LeetCode *  (0) 2025.01.01
서브쿼리 기초 3 *  (0) 2024.12.27
서브쿼리 기초 1  (0) 2024.12.25
Fix Names in a Table _ Easy. LeetCode *  (0) 2024.11.01
Japan Population _ Easy. HackerRank  (0) 2024.10.31