Find Customers With Positive Revenue this Year - Problem

You're working as a data analyst for a growing e-commerce company that needs to identify their most valuable customers for the year 2021. The finance team has provided you with a database table containing customer revenue data across multiple years.

Given a Customers table with the following structure:

Column NameType
customer_idint
yearint
revenueint

Where (customer_id, year) forms the primary key, your task is to find all customers who generated positive revenue in 2021. Note that revenue can be negative (perhaps due to returns or refunds exceeding purchases).

Goal: Write a SQL query to return the customer_id of all customers with positive revenue in 2021. The result can be returned in any order.

This problem tests your ability to filter data based on multiple conditions and work with business logic in SQL queries.

Input & Output

example_1.sql โ€” Basic Case
$ Input: Customers table: | customer_id | year | revenue | |-------------|------|----------| | 1 | 2021 | 300 | | 2 | 2021 | -150 | | 3 | 2021 | 450 | | 1 | 2020 | 500 | | 2 | 2020 | 200 |
โ€บ Output: | customer_id | |-------------| | 1 | | 3 |
๐Ÿ’ก Note: Customer 1 had $300 positive revenue in 2021, Customer 3 had $450 positive revenue in 2021. Customer 2 had negative revenue (-$150) in 2021, so they're excluded. Records from 2020 are not considered.
example_2.sql โ€” All Negative Revenue
$ Input: Customers table: | customer_id | year | revenue | |-------------|------|----------| | 1 | 2021 | -100 | | 2 | 2021 | -250 | | 3 | 2020 | 400 |
โ€บ Output: | customer_id | |-------------| (Empty result)
๐Ÿ’ก Note: No customers had positive revenue in 2021. Customer 3 had positive revenue, but it was in 2020, not 2021. Customers 1 and 2 both had negative revenue in 2021.
example_3.sql โ€” Mixed Years
$ Input: Customers table: | customer_id | year | revenue | |-------------|------|----------| | 1 | 2021 | 1000 | | 1 | 2020 | -500 | | 2 | 2019 | 800 | | 2 | 2021 | 150 | | 3 | 2021 | 0 |
โ€บ Output: | customer_id | |-------------| | 1 | | 2 |
๐Ÿ’ก Note: Customer 1: $1000 in 2021 (positive). Customer 2: $150 in 2021 (positive). Customer 3: $0 in 2021 (not positive, so excluded). Previous years' data doesn't affect the 2021 results.

Constraints

  • 1 โ‰ค customer_id โ‰ค 105
  • 2019 โ‰ค year โ‰ค 2023
  • -1000 โ‰ค revenue โ‰ค 10000
  • Each (customer_id, year) combination is unique
  • Focus on year 2021 only

Visualization

Tap to expand
Customer Revenue Analysis Process๐Ÿ“Š Customer DatabaseID:1, 2020, $500ID:1, 2021, $300 โœ“ID:2, 2021, -$150 โœ—ID:3, 2021, $750 โœ“ID:4, 2019, $200Apply Filters๐ŸŽฏ Filtered ResultsWHERE year = 2021AND revenue > 0ID:1, 2021, $300ID:3, 2021, $750Extract IDsโœ… Final OutputCustomer ID: 1Customer ID: 3SQL Query BreakdownSELECT customer_idFROM CustomersWHERE year = 2021 AND revenue > 0;โ† Choose what to returnโ† Specify data sourceโ† Apply filtering conditions
Understanding the Visualization
1
Access Database
Connect to the Customers table containing all customer revenue records across multiple years
2
Apply Year Filter
Filter records to only include data from 2021, ignoring all other years
3
Apply Revenue Filter
From the 2021 records, keep only those where revenue is greater than 0
4
Extract Customer IDs
Return the customer_id values from the filtered results
Key Takeaway
๐ŸŽฏ Key Insight: SQL filtering with multiple conditions in a WHERE clause is the most efficient approach for this type of data selection problem - no complex algorithms needed!
Asked in
Amazon 45 Google 38 Microsoft 32 Meta 28
34.7K Views
High Frequency
~8 min Avg. Time
892 Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen