Watch 10 video solutions for Customers Who Never Order, a easy level problem involving Database. This walkthrough by Frederik Müller has 12,861 views views. Want to try solving it yourself? Practice on FleetCode or read the detailed text solution.
Table: Customers
+-------------+---------+ | Column Name | Type | +-------------+---------+ | id | int | | name | varchar | +-------------+---------+ id is the primary key (column with unique values) for this table. Each row of this table indicates the ID and name of a customer.
Table: Orders
+-------------+------+ | Column Name | Type | +-------------+------+ | id | int | | customerId | int | +-------------+------+ id is the primary key (column with unique values) for this table. customerId is a foreign key (reference columns) of the ID from the Customers table. Each row of this table indicates the ID of an order and the ID of the customer who ordered it.
Write a solution to find all customers who never order anything.
Return the result table in any order.
The result format is in the following example.
Example 1:
Input: Customers table: +----+-------+ | id | name | +----+-------+ | 1 | Joe | | 2 | Henry | | 3 | Sam | | 4 | Max | +----+-------+ Orders table: +----+------------+ | id | customerId | +----+------------+ | 1 | 3 | | 2 | 1 | +----+------------+ Output: +-----------+ | Customers | +-----------+ | Henry | | Max | +-----------+
Problem Overview: You are given two tables: Customers and Orders. Each order references a customer through customerId. The task is to return the names of customers who never placed any order.
Approach 1: LEFT JOIN with IS NULL (O(n + m) time, O(1) extra space)
This approach performs a LEFT JOIN between the Customers table and the Orders table using Customers.Id = Orders.customerId. A left join keeps all rows from the left table and fills unmatched rows with NULL values from the right table. After the join, you filter rows where Orders.customerId IS NULL. Those rows represent customers who have no matching orders. This method is widely used in SQL queries for detecting missing relationships between tables. Query planners typically optimize joins efficiently, which makes this solution both clean and performant.
Approach 2: Subquery for Exclusion (O(n + m) time, O(n) space)
This approach filters customers using a subquery that returns all customerId values from the Orders table. The outer query selects customers whose Id does not appear in that set using NOT IN or NOT EXISTS. Conceptually, you first build a list of customers who placed orders, then exclude them from the result. In Python implementations with pandas or set operations, this translates to building a set of order customer IDs and selecting customers whose IDs are not in that set. Subquery-based filtering is common in database problems where you need to exclude rows that appear in another table.
When using NOT IN, you must ensure the subquery does not produce NULL values because SQL treats comparisons with NULL differently. Many engineers prefer NOT EXISTS because it avoids this issue and works reliably across SQL engines.
Recommended for interviews: The LEFT JOIN + IS NULL solution is the most commonly expected answer. It demonstrates a clear understanding of relational joins and how to detect unmatched rows. Interviewers often want to see that you recognize this as a classic SQL join pattern. The subquery exclusion method is also valid and shows you understand alternative query patterns, but the join-based approach is usually considered the most direct.
| Approach | Time | Space | When to Use |
|---|---|---|---|
| LEFT JOIN + IS NULL | O(n + m) | O(1) | Best general solution for SQL joins when identifying rows with no match in another table |
| Subquery with NOT IN / NOT EXISTS | O(n + m) | O(n) | Useful when filtering records based on exclusion logic or when joins are less readable |