Skip to main content

Find Customers With Positive Revenue this Year - Solution & Explanation

EasyPremiumFree on FleetCodeDatabase4 min readAsked at: Google
Practice this problem

Problem Statement

Table: Customers

+--------------+------+
| Column Name  | Type |
+--------------+------+
| customer_id  | int  |
| year         | int  |
| revenue      | int  |
+--------------+------+
(customer_id, year) is the primary key (combination of columns with unique values) for this table.
This table contains the customer ID and the revenue of customers in different years.
Note that this revenue can be negative.

 

Write a solution to report the customers with postive revenue in the year 2021.

Return the result table in any order.

The result format is in the following example.

 

Example 1:

Input: 
Customers table:
+-------------+------+---------+
| customer_id | year | revenue |
+-------------+------+---------+
| 1           | 2018 | 50      |
| 1           | 2021 | 30      |
| 1           | 2020 | 70      |
| 2           | 2021 | -50     |
| 3           | 2018 | 10      |
| 3           | 2016 | 50      |
| 4           | 2021 | 20      |
+-------------+------+---------+
Output: 
+-------------+
| customer_id |
+-------------+
| 1           |
| 4           |
+-------------+
Explanation: 
Customer 1 has revenue equal to 30 in the year 2021.
Customer 2 has revenue equal to -50 in the year 2021.
Customer 3 has no revenue in the year 2021.
Customer 4 has revenue equal to 20 in the year 2021.
Thus only customers 1 and 4 have positive revenue in the year 2021.

Approach Overview

Problem Overview: You have a Customers table containing customer_id, name, year, and revenue. The task is simple: return the IDs of customers who generated positive revenue in the year 2021. Only rows where revenue > 0 and year = 2021 should appear in the result.

This is a straightforward filtering problem in database queries. You don’t need joins, aggregation, or window functions. The query just scans the table and keeps rows that satisfy the required conditions.

Approach 1: WHERE Clause Filtering (O(n) time, O(1) space)

The most direct solution uses a WHERE clause to filter rows based on two conditions: the year must be 2021, and the revenue must be greater than zero. SQL evaluates these conditions for each row and returns only the matching customer IDs.

The key idea is predicate filtering. SQL engines iterate through the rows of the table and apply boolean checks: year = 2021 and revenue > 0. Rows that satisfy both predicates are included in the output. Since no additional structures or intermediate tables are required, the query runs with constant extra memory.

This pattern appears frequently in SQL interview questions. Instead of complex logic, the focus is on understanding table schemas and applying precise filters. The resulting query is concise and easy to read:

SELECT customer_id FROM Customers WHERE year = 2021 AND revenue > 0;

Time complexity is O(n) because the database may scan all rows in the table. Space complexity is O(1) since no additional storage beyond the result set is required.

Approach 2: Indexed Filtering (O(log n + k) time with index, O(1) space)

If the table grows large, database engines often rely on indexes to avoid full table scans. An index on year or a composite index like (year, revenue) allows the engine to quickly locate rows for 2021 and then filter by revenue.

With indexing, the engine performs an index lookup rather than scanning every row. This reduces the search portion to roughly O(log n), followed by returning the matching rows k. While indexing is handled internally by the database, understanding how it affects query performance is a key part of practical WHERE clause optimization.

Recommended for interviews: The standard WHERE clause solution is exactly what interviewers expect. The problem tests whether you can read the schema and translate requirements into a precise SQL filter. Mentioning index usage shows deeper understanding of query performance, but the core solution remains a simple conditional filter.

Solution

We can directly use the WHERE clause to filter out the customers whose year is 2021 and revenue is greater than 0.

Code

MySQL

Try this approach in the editor →

Detailed Complexity Analysis

ApproachTimeSpaceWhen to Use
WHERE Clause Table ScanO(n)O(1)Default solution for small or moderate tables where a full scan is acceptable
Indexed FilteringO(log n + k)O(1)Large datasets where indexes on year or (year, revenue) help avoid scanning the entire table

Video Solution

Advanced SQL Series 50 Pro Interview Questions Explained Step by Step - Leetcode 1821 | Data Science • Everyday Data Science • 31,014 views views

Watch 1 more video solutions →

Frequently Asked Questions

Is Find Customers With Positive Revenue this Year easy or hard?
The problem is categorized as Easy. It requires basic SQL knowledge, specifically writing a SELECT query with a WHERE clause to filter rows based on simple conditions.
Find Customers With Positive Revenue this Year Python/Java solution
This is a SQL database problem rather than a traditional algorithm question in Python or Java. The correct solution is a MySQL query: SELECT customer_id FROM Customers WHERE year = 2021 AND revenue > 0.
How to solve Find Customers With Positive Revenue this Year in O(n)?
Use a WHERE clause that directly filters the rows: SELECT customer_id FROM Customers WHERE year = 2021 AND revenue > 0. The database evaluates both conditions for each row, returning only customers who generated positive revenue in 2021.
What is the best approach for Find Customers With Positive Revenue this Year?
The best approach is a SQL WHERE clause that filters rows by year and revenue. The query checks for year = 2021 and revenue > 0, then returns the customer_id. This runs in O(n) time with O(1) extra space and is the expected solution in most SQL interviews.
Is Find Customers With Positive Revenue this Year asked at Google/Amazon/Meta?
SQL filtering problems like this appear frequently in data-focused interviews at companies such as Amazon, Meta, and Google. They test your ability to read schemas, apply conditions, and write concise queries using WHERE filters.
What data structure is used in Find Customers With Positive Revenue this Year?
The problem operates on a relational database table. Internally, databases may use indexes such as B-trees to speed up filtering on columns like year or revenue, but the logical solution simply uses SQL table filtering.
What is the time complexity of Find Customers With Positive Revenue this Year?
The typical SQL solution runs in O(n) time because the database scans rows and evaluates the WHERE conditions for each one. Space complexity is O(1) since no additional data structures are created beyond the result set.

Ready to solve this problem?

Practice Find Customers With Positive Revenue this Year with our built-in code editor and test cases.

Practice on FleetCode