Skip to main content

Sellers With No Sales - Solution & Explanation

EasyPremiumFree on FleetCodeDatabase5 min read
Practice this problem

Problem Statement

Table: Customer

+---------------+---------+
| Column Name   | Type    |
+---------------+---------+
| customer_id   | int     |
| customer_name | varchar |
+---------------+---------+
customer_id is the column with unique values for this table.
Each row of this table contains the information of each customer in the WebStore.

 

Table: Orders

+---------------+---------+
| Column Name   | Type    |
+---------------+---------+
| order_id      | int     |
| sale_date     | date    |
| order_cost    | int     |
| customer_id   | int     |
| seller_id     | int     |
+---------------+---------+
order_id is the column with unique values for this table.
Each row of this table contains all orders made in the webstore.
sale_date is the date when the transaction was made between the customer (customer_id) and the seller (seller_id).

 

Table: Seller

+---------------+---------+
| Column Name   | Type    |
+---------------+---------+
| seller_id     | int     |
| seller_name   | varchar |
+---------------+---------+
seller_id is the column with unique values for this table.
Each row of this table contains the information of each seller.

 

Write a solution to report the names of all sellers who did not make any sales in 2020.

Return the result table ordered by seller_name in ascending order.

The result format is in the following example.

 

Example 1:

Input: 
Customer table:
+--------------+---------------+
| customer_id  | customer_name |
+--------------+---------------+
| 101          | Alice         |
| 102          | Bob           |
| 103          | Charlie       |
+--------------+---------------+
Orders table:
+-------------+------------+--------------+-------------+-------------+
| order_id    | sale_date  | order_cost   | customer_id | seller_id   |
+-------------+------------+--------------+-------------+-------------+
| 1           | 2020-03-01 | 1500         | 101         | 1           |
| 2           | 2020-05-25 | 2400         | 102         | 2           |
| 3           | 2019-05-25 | 800          | 101         | 3           |
| 4           | 2020-09-13 | 1000         | 103         | 2           |
| 5           | 2019-02-11 | 700          | 101         | 2           |
+-------------+------------+--------------+-------------+-------------+
Seller table:
+-------------+-------------+
| seller_id   | seller_name |
+-------------+-------------+
| 1           | Daniel      |
| 2           | Elizabeth   |
| 3           | Frank       |
+-------------+-------------+
Output: 
+-------------+
| seller_name |
+-------------+
| Frank       |
+-------------+
Explanation: 
Daniel made 1 sale in March 2020.
Elizabeth made 2 sales in 2020 and 1 sale in 2019.
Frank made 1 sale in 2019 but no sales in 2020.

Approach Overview

Problem Overview: You have two tables: Seller and Orders. Each order records the seller and the sale date. The task is to return the names of sellers who did not make any sales during the year 2020.

Approach 1: LEFT JOIN + GROUP BY + FILTER (O(n + m) time, O(1) extra space)

This approach joins every seller with their orders using a LEFT JOIN. A left join keeps all rows from the Seller table even when no matching order exists. After joining, group the results by seller and count only the orders that fall in the 2020 date range. Sellers whose count is zero are exactly the ones with no sales during that year.

The key insight: LEFT JOIN preserves sellers without matching rows, and conditional aggregation lets you filter by year while still keeping the join intact. Using GROUP BY seller_id plus a HAVING condition ensures you only return sellers whose filtered order count equals zero. This pattern appears frequently in SQL interview problems involving missing relationships.

This technique is common when solving problems involving SQL joins and GROUP BY aggregation. It performs well because the database scans both tables once and aggregates results efficiently.

Approach 2: NOT EXISTS Subquery (O(n + m) time, O(1) extra space)

Another clean solution uses a correlated subquery with NOT EXISTS. For each seller, check whether an order exists where the seller matches and the sale date falls in 2020. If such a row exists, that seller is excluded. If the subquery returns no rows, the seller qualifies.

The insight here is that NOT EXISTS is optimized by most SQL engines and stops searching as soon as a match is found. This often performs similarly to a join-based solution but can be easier to read because it expresses the condition directly: “no orders in 2020.”

Both approaches rely on core database query patterns: joining relational tables and filtering records based on time conditions.

Recommended for interviews: The LEFT JOIN + GROUP BY approach is the one most interviewers expect. It demonstrates that you understand join semantics and conditional aggregation. Mentioning the NOT EXISTS alternative shows deeper SQL knowledge and awareness of query optimization patterns.

Solution

We can use a left join to join the Seller table with the Orders table on the condition seller_id, and then group by seller_id to count the number of sales for each seller in the year 2020. Finally, we can filter out the sellers with zero sales.

Code

MySQL

Try this approach in the editor →

Detailed Complexity Analysis

ApproachTimeSpaceWhen to Use
LEFT JOIN + GROUP BY + HAVINGO(n + m)O(1)Most common SQL interview pattern for detecting missing related rows
NOT EXISTS SubqueryO(n + m)O(1)Cleaner logic when expressing "no matching records" conditions
LEFT JOIN + WHERE IS NULLO(n + m)O(1)Useful when filtering rows with completely missing matches instead of conditional aggregation

Video Solution

LeetCode 1607 "Sellers With No Sales" Interview SQL Question with Detailed ExplanationEveryday Data Science1,639 views views

Watch 1 more video solutions →

Frequently Asked Questions

Is Sellers With No Sales easy or hard?
Sellers With No Sales is considered an Easy SQL problem. It mainly tests understanding of LEFT JOIN semantics and filtering aggregated results using GROUP BY and HAVING.
Sellers With No Sales Python/Java solution
This problem is typically solved using SQL rather than Python or Java because it operates directly on relational tables. The core solution uses a LEFT JOIN with GROUP BY or a NOT EXISTS subquery to filter sellers without orders in 2020.
How to solve Sellers With No Sales in O(n + m)?
Join the Seller table with Orders using a LEFT JOIN so that sellers without orders are still included. Group by seller and count orders where the sale date falls in 2020. Use HAVING count = 0 to filter sellers with no qualifying sales.
What is the best approach for Sellers With No Sales?
The most common solution uses a LEFT JOIN between the Seller and Orders tables, followed by GROUP BY and a HAVING filter. Count only the orders that occur in 2020 and return sellers whose count is zero. This runs in O(n + m) time where n is the number of sellers and m is the number of orders.
Is Sellers With No Sales asked at Google/Amazon/Meta?
Database filtering and join problems like this appear frequently in SQL interview rounds at companies such as Amazon, Google, and Meta. Candidates are often tested on LEFT JOIN behavior, conditional aggregation, and filtering records that lack related rows.
What data structure is used in Sellers With No Sales?
The problem relies on relational database tables and SQL join operations rather than traditional data structures. Concepts like joins, grouping, and filtering are the primary tools used to derive the result set.
What is the time complexity of Sellers With No Sales?
Most SQL solutions run in O(n + m) time because the query scans the Seller table and the Orders table once before performing aggregation or existence checks. With proper indexing on seller_id and sale_date, the database optimizer can execute the join efficiently.

Ready to solve this problem?

Practice Sellers With No Sales with our built-in code editor and test cases.

Practice on FleetCode