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.
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.
We can directly use the WHERE clause to filter out the customers whose year is 2021 and revenue is greater than 0.
MySQL
| Approach | Time | Space | When to Use |
|---|---|---|---|
| WHERE Clause Table Scan | O(n) | O(1) | Default solution for small or moderate tables where a full scan is acceptable |
| Indexed Filtering | O(log n + k) | O(1) | Large datasets where indexes on year or (year, revenue) help avoid scanning the entire table |
Advanced SQL Series 50 Pro Interview Questions Explained Step by Step - Leetcode 1821 | Data Science • Everyday Data Science • 28,009 views views
Watch 1 more video solutions →Practice Find Customers With Positive Revenue this Year with our built-in code editor and test cases.
Practice on FleetCode