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       |
+-----------+

题目大意:

假设一个网站包含两个表, 顾客表Customers和订单表Orders。编写一个SQL查询找出所有从未下过订单的顾客。

解题思路:

使用NOT IN,NOT EXISTS,或者LEFT JOIN均可。

SQL语句:

使用NOT IN(Accepted 777ms):

# Write your MySQL query statement below
SELECT Name FROM Customers c WHERE c.Id NOT IN (SELECT CustomerId FROM Orders o);

使用NOT EXISTS(Accepted 745ms):

# Write your MySQL query statement below
SELECT Name FROM Customers c WHERE NOT EXISTS (SELECT CustomerId FROM Orders o WHERE o.CustomerId = c.id);

使用LEFT JOIN(Accepted 726ms):

# Write your MySQL query statement below
SELECT Name FROM Customers c LEFT JOIN Orders o ON c.Id = o.CustomerId WHERE o.Id IS NULL;
时间: 2024-10-13 00:18:00

leetcode 183: Customers Who Never Order的相关文章

LeetCode 183. Customers Who Never Order (从不订购的客户)

题目标签: 题目给了我们 Customers 和 Orders 两个表格,让我们找到 从没订购过的客户. 首先从Orders 得到 订购过的CustomerId,然后再去Customers 里找 没有出现过的 Id,返回名字. Java Solution: Runtime:  264 ms, faster than 53 % Memory Usage: N/A 完成日期:06/01/2019 关键点:NOT IN # Write your MySQL query statement below

[LeetCode][SQL]Customers Who Never Order

https://leetcode.com/problems/customers-who-never-order/ 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: Customer

【Leetcode】Customers Who Never Order

题目链接:https://leetcode.com/problems/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

183. Customers Who Never Order

# Write your MySQL query statement below select Name Customers from Customers where Customers.Id not in ( select Customers.Id from Orders,Customers where Customers.Id=Orders.CustomerId )

[LeetCode] 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

【leetcode SQL】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

LeetCode - Customers Who Never Order

Description: 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 一种比较高效的方法是左连接,左连接之后任然使用被连接的两个表或多个表来定位连接之后的表的数据. # Write your MySQL query statement bel

[leetcode]Binary Tree Zigzag Level Order Traversal

Binary Tree Zigzag Level Order Traversal Given a binary tree, return the zigzag level order traversal of its nodes' values. (ie, from left to right, then right to left for the next level and alternate between). For example:Given binary tree {3,9,20,#

LeetCode OJ - Binary Tree Level Order Traversal 1 && 2

BFS以及它的扩展,我发现栈是个很好用的数据结构,特别是对于顺序需要颠倒的时候!!! 这里有个重要的信息:可以用null来标识一个level的结束!!! 下面是AC代码: 1 /** 2 * Given a binary tree, return the bottom-up level order traversal of its nodes' values. 3 * (ie, from left to right, level by level from leaf to root). 4 *