코드카타/SQL, Pandas

Cities With Completed Trades

susinlee 2025. 3. 9. 21:15

[문제]

https://datalemur.com/questions/completed-trades

 

Robinhood SQL Interview Question | DataLemur

Robinhood SQL Interview Question: Write a query to find the cities with the highest completed trades.

datalemur.com

 

[풀이]

1. 두 테이블을 병합한 후 완료된 주문만 필터링

2. 도시별로 그룹화 한뒤 주문 개수를 계산하고 개수를 기준으로 내림차순 정렬

3. 상위 3개만 반환

 

[코드]

SELECT city, COUNT(order_id) AS total_orders
FROM trades t
JOIN users u
ON t.user_id = u.user_id
WHERE status = 'Completed'
GROUP BY city
ORDER BY total_orders DESC
LIMIT 3