Skip to content
BytePatterns

Customers Who Never Ordered

EasySQL#left-join#anti-join~15m

Problem

Two tables: customer(id, name) and orders(id, customer_id), where orders.customer_id points at customer.id. Guest checkouts are stored with a NULL customer_id. Write a query that returns the names of the customers who have never placed an order, as a single column name, sorted by name.

Examples

Input:  customer = [(1, "Ana"), (2, "Ben"), (3, "Cy")]
        orders   = [(10, 1), (11, 1), (12, 3)]
Output: [("Ben",)]
Why:    Ana has two orders and Cy has one
Input:  customer = [(1, "Ana"), (2, "Ben")]
        orders   = [(10, 1), (11, None)]
Output: [("Ben",)]
Why:    order 11 was a guest checkout and belongs to nobody
Input:  customer = [(1, "Ana"), (2, "Ben")]
        orders   = []
Output: [("Ana",), ("Ben",)]
Why:    edge case, with no orders at all every customer qualifies

Hints

0 / 3

Stuck on the idea rather than the code? INNER and OUTER JOINs covers it.