[LeetCode] 183.Customers Who Never OrderSuppose 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.
Table: Orders.
Using the above tables as example, return the following:
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:
|
>>: HTML pop-up transparent layer instance size can be set and can be proportional
Through permission-based email marketing, not onl...
This article example shares the specific code of ...
DOMContentLoaded Event Literally, it fires after ...
Transition document address defines a background ...
Preface Nginx is a lightweight HTTP server that u...
Mysql left join is invalid and how to use it When...
sed is a character stream editor under Unix, that...
Table of contents How to flatten an array 1. Usin...
Generally, on national days of mourning, days of ...
dl:Definition list Definition List dt:Definition t...
Introduction In orm frameworks, such as hibernate...
text 1) Download the Ubuntu image docker pull ubu...
1. SHOW PROCESSLIST command SHOW PROCESSLIST show...
Download link: Operating Environment CentOS 7.6 i...
Table of contents Preface Creating a component li...