SQL: Find Customers with No Orders
SELECT c.customer_id, c.customer_name FROM Customers c LEFT JOIN Orders o ON c.customer_id = o.customer_id WHERE o.order_id IS NULL;
This SQL query identifies customers who have never placed an order. It uses a `LEFT JOIN` to include all customers and then filters for those where no matching order was found. This is useful for re-engagement campaigns...
The query starts by selecting `customer_id` and `customer_name` from the `Customers` table. A `LEFT JOIN` is performed with the `Orders` table on `customer_id`. This ensures that all rows from the `Customers` table are included, and if a customer has no matching orders, the columns from the `Orders` table will be `NULL`. The `WHERE o.order_id IS NULL` clause filters the results to only include those customers for whom no order was found (i.e., `order_id` is `NULL`). The time complexity is typically O(N + M) where N is the number of customers and M is the number of orders, assuming efficient indexing. Space complexity is O(N) for the result set.
SELECT customer_id, customer_name FROM Customers LEFT JOIN Orders ON Customers.customer_id = Orders.customer_id WHERE Orders.order_id IS NULL