Member-only story

Postgres Practice — Customer Placing the Largest Number of Orders (Easy) #9

--

Tables

The Problem

Example

Solution

We need to find the customer who has placed the most orders. Since we have records in the table, this suggests using GROUP BY. When we perform GROUP BY, we need to order the data by COUNT from largest to smallest.

Then, we add LIMIT to get only one result.

SELECT customer_number
FROM Orders
GROUP BY customer_number
ORDER BY COUNT(*) DESC
LIMIT 1;

Data

I will add the queries to create tables and insert data if you want to try it yourself.

DROP TABLE IF EXISTS Orders CASCADE;

CREATE TABLE Orders (
order_number INT PRIMARY KEY,
customer_number INT
);

INSERT INTO Orders (order_number, customer_number) VALUES
(1, 1),
(2, 2),
(3, 3),
(4, 3);

Leetcode Challenge — https://leetcode.com/problems/customer-placing-the-largest-number-of-orders/description/

--

--

Tomas Svojanovsky
Tomas Svojanovsky

Written by Tomas Svojanovsky

I'm a full-stack developer. Programming isn't just my job but also my hobby. I like developing seamless user experiences and working on server-side complexities

No responses yet