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

Suggestions on creating business HTML emails

Through permission-based email marketing, not onl...

Vue implements a movable floating button

This article example shares the specific code of ...

JavaScript DOMContentLoaded event case study

DOMContentLoaded Event Literally, it fires after ...

Vue example code using transition component animation effect

Transition document address defines a background ...

The whole process of configuring reverse proxy locally through nginx

Preface Nginx is a lightweight HTTP server that u...

Use the sed command to modify the kv configuration file in Linux

sed is a character stream editor under Unix, that...

js implements array flattening

Table of contents How to flatten an array 1. Usin...

When is it appropriate to use dl, dt, and dd?

dl:Definition list Definition List dt:Definition t...

Mysql solves the database N+1 query problem

Introduction In orm frameworks, such as hibernate...

Issues installing Python3 and Pip in ubuntu in Docker

text 1) Download the Ubuntu image docker pull ubu...

MySQL SHOW PROCESSLIST assists in the entire process of troubleshooting

1. SHOW PROCESSLIST command SHOW PROCESSLIST show...

How to install Apache service in Linux operating system

Download link: Operating Environment CentOS 7.6 i...

How to build your own Angular component library with DevUI

Table of contents Preface Creating a component li...