Domain 1EasyCORE

Customers Who Never Order

The problem asks us to find customers who have never placed an order. We are given two tables: `Customers` and `Orders`. The `Customers` table contains customer IDs and names, while the `Orders` table contains order IDs and corresponding customer IDs. Our task is to return a list of customer names from the `Customers` table that do not appear in the `Orders` table.

Domain: Easy•Updated: 2026-08-31
#Algorithms#LeetCode#qwen2.5-coder:14b#**FAANG asked:** 0

183. Customers Who Never Order

Model: qwen2.5-coder:14b
Topics:
FAANG asked: 0
Acceptance rate: 57.5%


Problem Breakdown

The problem asks us to find customers who have never placed an order. We are given two tables: Customers and Orders. The Customers table contains customer IDs and names, while the Orders table contains order IDs and corresponding customer IDs. Our task is to return a list of customer names from the Customers table that do not appear in the Orders table.

Inputs:

  • Two tables:
    • Customers: columns customer_id (int), name (string)
    • Orders: columns order_id (int), customer_id (int)

Outputs:

  • A list of customer names from the Customers table that have no corresponding entries in the Orders table.

Implicit Constraints or Tricky Edge Cases:

  • The Customers table may be empty.
  • The Orders table may be empty.
  • There could be duplicate customer IDs in either table, but since they are primary keys, duplicates should not exist.
  • Customer names can be any string, including special characters.

Intuition

A brute-force approach would involve iterating over each customer and checking if their ID exists in the Orders table. This would result in a time complexity of O(n * m), where n is the number of customers and m is the number of orders. This is inefficient for large datasets.

The key insight is to use a set operation to find the difference between the set of all customer IDs and the set of customer IDs that have placed orders. This can be done efficiently using SQL’s NOT IN or LEFT JOIN operations, which reduces the time complexity to O(n + m).

Approach

  1. Identify the Set Difference: We need to find customers whose IDs are not present in the Orders table.
  2. Use SQL Set Operations:
    • We can use a subquery with NOT IN to filter out customers who have placed orders.
    • Alternatively, we can use a LEFT JOIN and check for null values in the joined result.

Algorithm Steps:

  1. Select all customer names from the Customers table.
  2. Use a subquery to exclude those whose IDs are present in the Orders table using NOT IN.

Complexity Analysis

  • Time Complexity: O(n + m), where n is the number of customers and m is the number of orders. This is because we need to scan both tables once.
  • Space Complexity: O(k), where k is the number of customers who never ordered, as this is the size of the result set.

Solution

# SQL query to find customers who never order
SELECT name FROM Customers WHERE customer_id NOT IN (SELECT customer_id FROM Orders);

Edge Cases Handled

  • Empty Customers Table: The query will return an empty list since there are no customers.
  • Empty Orders Table: All customers will be returned since none have placed orders.
  • No Matching Customer IDs: If all customer IDs in Customers are unique and do not exist in Orders, all customer names will be returned.