Skip to content

LeetCode 586 - Customer Placing the Largest Number of Orders

Database Language: PostgreSQL

Difficulty: ⭐

Problem Description

Input

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.

Requirement

Write a solution to find the customer_number for the customer who has placed the largest number of orders.

The test cases are generated so that exactly one customer will have placed more orders than any other customer.

The result format is in the following example.

Examples

Example 1

Input

Orders table:

order_number customer_number
1 1
2 2
3 3
4 3
Output
customer_number
3
Explanation

The customer with number 3 has two orders, which is greater than either customer 1 or 2 because each of them only has one order. So the result is customer_number 3.

SQL Schema

CREATE TABLE IF NOT EXISTS orders (order_number INT PRIMARY KEY, 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');

Solution

To find the customer who has placed the largest number of orders, the number of orders placed by each customer needs to be determined first. This can be determined by using the COUNT() aggregate function:

SELECT customer_number, COUNT(order_number) AS order_count
FROM orders
GROUP BY customer_number
customer_number order_count
1 1
2 1
3 2

Since the question wants the customer who placed the largest number of orders, the output of the previous query needs to be sorted by the order_count in descending order:

SELECT customer_number, COUNT(order_number) AS order_count
FROM orders
GROUP BY customer_number
ORDER BY order_count DESC
customer_number order_count
3 2
1 1
2 1

The query above returned all customers but the question only wants a single customer who has placed the most orders. To address the requirement, the output needs to be limited to just 1 row using the LIMIT 1 clause:

SELECT customer_number, COUNT(order_number) AS order_count
FROM orders
GROUP BY customer_number
ORDER BY order_count DESC
LIMIT 1
customer_number order_count
3 2

Lastly, the output needed is just the customer_number without the number of orders made by that customer so the order_count column in the SELECT clause needs to be removed:

SELECT customer_number
FROM orders
GROUP BY customer_number
ORDER BY order_count DESC
LIMIT 1

But this generates the following error because the order_count column is being referenced in the ORDER BY clause:

ERROR:  column "order_count" does not exist
LINE 4: ORDER BY order_count DESC
                 ^ 

SQL state: 42703
Character: 70

To resolve this, the previous expression used to create the order_count column will be used in the ORDER BY clause:

# Final Solution Query
SELECT customer_number
FROM orders
GROUP BY customer_number
ORDER BY COUNT(order_number) DESC
LIMIT 1
customer_number
3

Here's the query plan generated by MySQL for this query:

Limit  (cost=46.90..46.90 rows=1 width=12)
  ->  Sort  (cost=46.90..47.40 rows=200 width=12)
        Sort Key: (count(order_number)) DESC
        ->  HashAggregate  (cost=43.90..45.90 rows=200 width=12)
              Group Key: customer_number
              ->  Seq Scan on orders  (cost=0.00..32.60 rows=2260 width=8)

And here's the fastest runtime for this query:

  • Runtime: 235ms
  • Beats: 92.14% as of July 27, 2024