SQL implementation of LeetCode (183. Customers who have never placed an order)

SQL implementation of LeetCode (183. Customers who have never placed an order)

[LeetCode] 183.Customers Who Never Order

Suppose that a website contains two tables, the Customers table and the Orders table. Write a SQL query to find all customers who never order anything.

Table: Customers.

+----+-------+
| Id | Name |
+----+-------+
| 1 | Joe |
| 2 | Henry |
| 3 | Sam |
| 4 | Max |
+----+-------+

Table: Orders.

+----+------------+
| Id | CustomerId |
+----+------------+
| 1 | 3 |
| 2 | 1 |
+----+------------+

Using the above tables as example, return the following:

+-----------+
| Customers |
+-----------+
| Henry |
| Max |
+-----------+

This question gives us a Customers table and an Orders table. Let us find customers who have never placed an order. Then our most direct method is to find the customer ID that does not appear in the Orders table, using the Not in keyword, as shown below:

Solution 1:

SELECT Name AS Customers FROM Customers 
WHERE Id NOT IN (SELECT CustomerId FROM Orders);

Or we can also use a left join to join the two tables. We just need to find out the customers whose CustomerId on the right is Null, which means they have not placed an order:

Solution 2:

SELECT Name AS Customers FROM Customers
LEFT JOIN Orders ON Customers.Id = Orders.CustomerId
WHERE Orders.CustomerId IS NULL;

We can also use the Not exists keyword, which works similarly to Not in. See the following code:

Solution 3:

SELECT Name AS Customers FROM Customers c
WHERE NOT EXISTS (SELECT * FROM Orders o WHERE o.CustomerId = c.Id);

References:

https://leetcode.com/discuss/22624/three-accepted-solutions

https://leetcode.com/discuss/53213/a-solution-using-not-in-and-another-one-using-left-join

This is the end of the article about SQL implementation of LeetCode (182. Customers who have never placed an order). For more relevant content about SQL implementation of customers who have never placed an order, please search 123WORDPRESS.COM's previous articles or continue to browse the following related articles. I hope everyone will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • SQL implementation of LeetCode (196. Delete duplicate mailboxes)
  • SQL implementation LeetCode (185. Top three highest salaries in the department)
  • SQL implementation of LeetCode (184. The highest salary in the department)
  • SQL implementation of LeetCode (182. Duplicate mailboxes)
  • SQL implementation of LeetCode (181. Employees earn more than managers)
  • SQL implements LeetCode (180. Continuous numbers)
  • C++ implementation of LeetCode (179. Maximum number of combinations)
  • SQL implementation of LeetCode (197. Rising temperature)

<<:  A detailed explanation of the overlapping and soft color matching method in web page color matching

>>:  HTML pop-up transparent layer instance size can be set and can be proportional

Recommend

Should I use UTF-8 or GB2312 encoding when building a website?

Often when we open foreign websites, garbled char...

How to clean up data in MySQL online database

Table of contents 01 Scenario Analysis 02 Operati...

How to use the Linux seq command

1. Command Introduction The seq (Sequence) comman...

How to try to add sticky effect to your CSS

Written in front I don’t know who first discovere...

HTML+CSS to achieve text folding special effects example

This article mainly introduces the example of rea...

MySQL triggers: creating and using triggers

This article uses examples to describe the creati...

Introduction to fork in multithreading under Linux

Table of contents Question: Case (1) fork before ...

Before making a web page, let’s take a look at these so-called specifications

This article has compiled some so-called specific...

How to display the border when td is empty

Previously, I summarized how to use CSS to achieve...

Linux system dual network card binding configuration implementation

System version [root@ ~]# cat /etc/redhat-release...

Nginx tp3.2.3 404 problem solution

Recently I changed Apache to nginx. When I moved ...

Docker uses Supervisor to manage process operations

A Docker container starts a single process when i...

Methods and steps for Etcd distributed deployment based on Docker

1. Environmental Preparation 1.1 Basic Environmen...