Customers Who Never Order is a very easy and simple question which is for beginners. This question will be solved with subqueries.
Let's take the SQL table schema of Customers:
CREATE TABLE Customers (id int, name varchar(255))
INSERT INTO Customers (id, name) values ('1', 'Joe')
INSERT INTO Customers (id, name) values ('2', 'Henry')
INSERT INTO Customers (id, name) values ('3', 'Sam')
INSERT INTO Customers (id, name) values ('4', 'Max')
Now, the Customers table will look like this:
id | name |
---|---|
1 | Joe |
2 | Henry |
3 | Sam |
4 | Max |
The SQL table schema of Orders:
CREATE TABLE Orders (id int, customerId int)INSERT INTO Orders (id, customerId) values ('1', '3')INSERT INTO Orders (id, customerId) values ('2', '1')
Now, the Orders table will look like this:
id | customerId |
---|---|
1 | 3 |
2 | 1 |
The required Output should look like this:
Customers |
---|
Henry |
Max |
Write an SQL query to report all customers who never order anything.
Let's start with the algorithm of SQL Query:
SELECT name as Customers FROM Customers WHERE id NOT IN (SELECT customerId FROM orders);
This is the query for the required output.
0 Comments