List Top Customers and Monthly Order Counts in SQL
Company: Amazon
Role: Data Scientist
Category: Data Manipulation (SQL/Python)
Difficulty: medium
Interview Round: Technical Screen
Overview: This question evaluates a candidate's ability to perform SQL aggregation, grouping, top‑N selection, and date-based summarization to compute customer spend and chronological monthly order counts.
Top 3 customers by total spend
Tables
Orders(order_id INTEGER, customer_id INTEGER, order_date DATE, amount DECIMAL(10,2))
Hints
- Use SUM(amount) grouped by customer_id to calculate total spend per customer.
- Order by total_spend descending and customer_id ascending, then LIMIT 3.
Monthly order counts for 2023
Tables
Orders(order_id INTEGER, customer_id INTEGER, order_date DATE, amount DECIMAL(10,2))
Hints
- Filter the date range to 2023 using order_date >= DATE '2023-01-01' and < DATE '2024-01-01'.
- Use DATE_TRUNC to group by month and TO_CHAR to format the month as YYYY-MM.
Community answers
Answer by sharmilisusmi95