Find the Unique Customer with the Most Orders

Quick Overview

Return customer_number for the customer with the greatest number of Orders rows. The source guarantees exactly one customer has a strictly larger order count than every other customer.

Find the Unique Customer with the Most Orders

Company: Amazon

Role: Software Engineer

Category: Data Manipulation (SQL/Python)

Difficulty: easy

Interview Round: Other

### Problem Return customer_number for the customer with the greatest number of Orders rows. The source guarantees exactly one customer has a strictly larger order count than every other customer. ### Tables Table: `Orders` ```text +-----------------+----------+ | Column Name | Type | +-----------------+----------+ | order_number | int | | customer_number | int | +-----------------+----------+ order_number is the primary key (column with unique values) for this table. This table contains information about the order ID and the customer ID. ``` ### Constraints - In this version, every order has a non-NULL `customer_number` identifying its customer. ### Example ```text Orders (order_number,customer_number): (1,4), (2,7), (3,4), (4,4), (5,7) Output (customer_number): (4) ``` ### Optional follow-up If several customers tie for the largest number of orders, return every customer_number attaining that maximum. ### What a Strong Answer Covers - Count order rows for each customer. - Return only the customer number with the unique highest count. ### Follow-up Questions - How would you return every customer tied for the most orders? - How would you restrict the count to orders placed this year?

Overview: Return customer_number for the customer with the greatest number of Orders rows. The source guarantees exactly one customer has a strictly larger order count than every other customer.

Amazon logo
Amazon
easySoftware EngineerOtherData Manipulation (SQL/Python)
0
0

Problem

Return customer_number for the customer with the greatest number of Orders rows. The source guarantees exactly one customer has a strictly larger order count than every other customer.

Tables

Table: Orders

+-----------------+----------+
| Column Name     | Type     |
+-----------------+----------+
| order_number    | int      |
| customer_number | int      |
+-----------------+----------+
order_number is the primary key (column with unique values) for this table.
This table contains information about the order ID and the customer ID.

Constraints

  • In this version, every order has a non-NULL customer_number identifying its customer.

Example

Orders (order_number,customer_number): (1,4), (2,7), (3,4), (4,4), (5,7)
Output (customer_number): (4)

Optional follow-up

If several customers tie for the largest number of orders, return every customer_number attaining that maximum.

What a Strong Answer Covers Guidance

  • Count order rows for each customer.
  • Return only the customer number with the unique highest count.

Follow-up Questions Guidance

  • How would you return every customer tied for the most orders?
  • How would you restrict the count to orders placed this year?