Skip to main content

The Number of Rich Customers - Solution & Explanation

EasyPremiumFree on FleetCodeDatabase3 min readAsked at: Athenahealth
Practice this problem

Problem Statement

Table: Store

+-------------+------+
| Column Name | Type |
+-------------+------+
| bill_id     | int  |
| customer_id | int  |
| amount      | int  |
+-------------+------+
bill_id is the primary key (column with unique values) for this table.
Each row contains information about the amount of one bill and the customer associated with it.

 

Write a solution to report the number of customers who had at least one bill with an amount strictly greater than 500.

The result format is in the following example.

 

Example 1:

Input: 
Store table:
+---------+-------------+--------+
| bill_id | customer_id | amount |
+---------+-------------+--------+
| 6       | 1           | 549    |
| 8       | 1           | 834    |
| 4       | 2           | 394    |
| 11      | 3           | 657    |
| 13      | 3           | 257    |
+---------+-------------+--------+
Output: 
+------------+
| rich_count |
+------------+
| 2          |
+------------+
Explanation: 
Customer 1 has two bills with amounts strictly greater than 500.
Customer 2 does not have any bills with an amount strictly greater than 500.
Customer 3 has one bill with an amount strictly greater than 500.

Approach Overview

Problem Overview: The table Store records purchases with columns customer_id and amount. A customer is considered rich if they made a purchase strictly greater than 500. The task is to return how many unique customers satisfy this condition.

Approach 1: Filter + DISTINCT Count (O(n) time, O(1) extra space)

The direct solution filters rows where amount > 500 and counts unique customers. Use COUNT(DISTINCT customer_id) so each qualifying customer is counted once even if they have multiple purchases above 500. The database scans the table, applies the filter, and performs a distinct aggregation. This approach is concise and matches how SQL engines are optimized to handle filtering and aggregation operations. Works well for problems involving uniqueness in database queries.

Approach 2: GROUP BY + HAVING (O(n) time, O(k) space)

Another option groups rows by customer_id, then filters groups using a HAVING clause. The query evaluates whether any purchase in each group exceeds 500 using MAX(amount) > 500. After grouping, count the remaining rows. This approach explicitly models the logic per customer and is useful when the definition of "rich" depends on aggregated metrics like maximum, sum, or average. It demonstrates deeper understanding of SQL grouping operations in SQL.

Approach 3: Subquery with DISTINCT (O(n) time, O(k) space)

A subquery first selects distinct customer_id values where amount > 500. The outer query simply counts the rows returned. Internally this behaves similarly to the DISTINCT aggregation approach but separates filtering and counting into two logical steps. This structure is sometimes preferred for readability when building layered queries or composing logic with other filters and joins in larger aggregation pipelines.

Recommended for interviews: The COUNT(DISTINCT customer_id) solution is the expected answer. It shows you understand filtering and uniqueness directly in SQL. The GROUP BY version is still valid and useful when interviewers want to see familiarity with aggregation patterns.

Solution

Code

MySQL

Try this approach in the editor →

Detailed Complexity Analysis

ApproachTimeSpaceWhen to Use
Filter + COUNT(DISTINCT)O(n)O(1)Best general solution. Simple query and optimized by SQL engines.
GROUP BY + HAVINGO(n)O(k)When defining conditions per customer using aggregated values.
Subquery with DISTINCTO(n)O(k)Useful when composing queries or separating filtering and counting logic.

Video Solution

LeetCode 2082 Interview SQL Question with Detailed Explanation | Practice SQL • Everyday Data Science • 5,040 views views

Frequently Asked Questions

Is The Number of Rich Customers easy or hard?
The Number of Rich Customers is categorized as an Easy SQL problem. It mainly tests your understanding of filtering rows and counting unique values using COUNT(DISTINCT) or GROUP BY.
The Number of Rich Customers Python/Java solution
This is a SQL database problem, so the primary solution is written in MySQL or another SQL dialect. In application code such as Python or Java, you would execute the SQL query against the database and retrieve the resulting count.
How to solve The Number of Rich Customers in O(n)?
Filter rows with amount > 500 and count distinct customer IDs. Example: SELECT COUNT(DISTINCT customer_id) FROM Store WHERE amount > 500;. The database processes the table once, making the solution O(n) time with efficient built‑in aggregation.
What is the best approach for The Number of Rich Customers?
The most efficient solution filters rows where amount > 500 and counts unique customers using COUNT(DISTINCT customer_id). This runs in O(n) time because the database scans the table once and performs a distinct aggregation. It is also the shortest and most common SQL solution used in interviews.
Is The Number of Rich Customers asked at Google/Amazon/Meta?
This type of SQL filtering and aggregation problem appears in database interview rounds at companies like Amazon, Google, and Meta. While the exact question may vary, counting distinct entities after applying a filter is a common pattern tested in data engineering and backend interviews.
What data structure is used in The Number of Rich Customers?
The problem relies on relational database operations rather than traditional data structures. Internally, SQL engines use hashing or sorting to implement DISTINCT and aggregation, but from a query perspective the main concepts are filtering and counting unique values.
What is the time complexity of The Number of Rich Customers?
The query typically runs in O(n) time where n is the number of rows in the Store table. The database scans each row to check the amount condition and performs a DISTINCT aggregation on customer_id. Space usage depends on the query plan but is usually minimal.

Ready to solve this problem?

Practice The Number of Rich Customers with our built-in code editor and test cases.

Practice on FleetCode