Customers Without Orders

MediumSQLDatabaseSorting

Description

Write a SQL query to return the names of customers who have never placed an order, ordered by name ascending. **Output columns (in order):** name

Table:customers
ColumnType
idINT
nameTEXT
Table:orders
ColumnType
idINT
customerIdINT

Examples

Input:CREATE TABLE customers (id INT, name TEXT); CREATE TABLE orders (id INT, customerId INT); INSERT INTO customers VALUES (1,'Ann'),(2,'Ben'),(3,'Cal'); INSERT INTO orders VALUES (1,1),(2,1);
Output:Ben Cal
Explanation:

Customers are matched to their orders and only those with no matching order are kept, by name.

Input:CREATE TABLE customers (id INT, name TEXT); CREATE TABLE orders (id INT, customerId INT); INSERT INTO customers VALUES (1,'A'),(2,'B');
Output:A B
Explanation:

Customers are matched to their orders and only those with no matching order are kept, by name.

Input:CREATE TABLE customers (id INT, name TEXT); CREATE TABLE orders (id INT, customerId INT); INSERT INTO customers VALUES (1,'Zoe'),(2,'Amy'),(3,'Bob'); INSERT INTO orders VALUES (1,2);
Output:Bob Zoe
Explanation:

Customers are matched to their orders and only those with no matching order are kept, by name.

Constraints

  • Use standard SQL (SQLite).

Ready to solve this problem?

Practice solo and sharpen your skills for technical interviews.