LeetCode:183.从不订购的客户
题目链接:https://leetcode-cn.com/problems/customers-who-never-order/
题目
某网站包含两个表 Customers 表和 Orders 表。编写一个 SQL 查询,找出所有从不订购任何东西的客户。
Customers 表:
+----+-------+
| Id | Name |
+----+-------+
| 1 | Joe |
| 2 | Henry |
| 3 | Sam |
| 4 | Max |
+----+-------+
Orders 表:
+----+------------+
| Id | CustomerId |
+----+------------+
| 1 | 3 |
| 2 | 1 |
+----+------------+
例如给定上述表格,你的查询应返回:
+-----------+
| Customers |
+-----------+
| Henry |
| Max |
+-----------+
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/customers-who-never-order
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
解答
通过 not exists 实现。
---- oracle ----
/* Write your PL/SQL query statement below */
select a.Name as Customers
from Customers a
where not exists(select 1 from Orders b where a.Id = b.CustomerId) ---- 909ms
通过 left join 实现。
---- oracle ----
/* Write your PL/SQL query statement below */
select a.Name as Customers
from Customers a
left join Orders b
on a.Id = b.CustomerId
where b.Id is null ---- 1246ms
通过子查询和 not in 实现。
---- oracle ----
/* Write your PL/SQL query statement below */
select a.Name as Customers
from Customers a
where Id not in (select CustomerId from Orders) ---- 1172ms
思考
- 通过
not exists实现,效率最高; - 通过
left join关联之后为空实现;
LeetCode:183.从不订购的客户的更多相关文章
- 力扣(LeetCode)从不订购的客户-数据库题 个人题解
SQL架构 某网站包含两个表,Customers 表和 Orders 表.编写一个 SQL 查询,找出所有从不订购任何东西的客户. Customers 表: +----+-------+ | Id | ...
- sql 183. 从不订购的客户
SQL架构 某网站包含两个表,Customers 表和 Orders 表.编写一个 SQL 查询,找出所有从不订购任何东西的客户. Customers 表: +----+-------+ | Id | ...
- [SQL]LeetCode183. 从不订购的客户 | Customers Who Never Order
Suppose that a website contains two tables, the Customers table and the Orders table. Write a SQL qu ...
- LeetCode 183. Customers Who Never Order (从不订购的客户)
题目标签: 题目给了我们 Customers 和 Orders 两个表格,让我们找到 从没订购过的客户. 首先从Orders 得到 订购过的CustomerId,然后再去Customers 里找 没有 ...
- [LeetCode] 183. Customers Who Never Order_Easy tag: SQL
Suppose that a website contains two tables, the Customers table and the Orders table. Write a SQL qu ...
- leetcode 183. Customers Who Never Order
select Name as Customers from Customers where Id not in (select CustomerId from Orders);
- LeetCode:数据库技术【180-185】
LeetCode:数据库技术[180-185] 180.连续出现的数字 题目描述 编写一个 SQL 查询,查找所有至少连续出现三次的数字. +----+-----+ | Id | Num | +--- ...
- leetcode题库
leetcode题库 #题名题解通过率难度出现频率 1 两数之和 46.5%简单2 两数相加 35.5%中等3 无重复字符的最长子串 31.1%中等4 寻找两个有序数组的中位 ...
- leetcode 0218
目录 ✅ 1200. 最小绝对差 描述 解答 cpp py ✅ 897. 递增顺序查找树 描述 解答 cpp 指针问题? fuck ptr py ✅ 183. 从不订购的客户 描述 解答 sql to ...
随机推荐
- CNN基础框架简介
卷积神经网络简介 卷积神经网络是多层感知机的变种,由生物学家休博尔和维瑟尔在早期关于猫视觉皮层的研究发展而来.视觉皮层的细胞存在一个复杂的构造,这些细胞对视觉输入空间的子区域非常敏感,我们称之为感受野 ...
- 阿里云window server2012 r2服务器安装sql server2014配置成本地management工具连接
1.关服务器防火墙. 2.阿里云管理控制台配置安全组, 3.进入服务器,打开
- 哈希算法MD5和SHA1的C#实现
/* * 哈希算法MD5和SHA1的C#实现 * * * 夏春涛 Email:xChuntao@163.com * Blog:http://bluesky521.cnblogs.com * 运行 ...
- Git安装以及配置SSH Key——Windows
安装 安装 Git 官网下载一个Windows版本的Git. 然后一直下一步即可,如下图 环境变量自动配好的,可以去检查一下环境变量中PATH中有没有Git的环境变量 然后在桌面右击鼠标,选择Git ...
- CenterNet算法笔记(目标检测论文)
论文名称:CenterNet: Keypoint Triplets for Object Detectiontection 论文链接:https://arxiv.org/abs/1904.08189 ...
- Python 清华镜像设置
大家在通过pip 或conda 下载一些很大的第三方库时是不是有一种等到坟头的草都三尺高了,还没下载完的感觉,而且大的第三方库长时间下载,可能会导致超时自动中断下载,感谢清华的大佬们 临时使用: pi ...
- Shell脚本无限调用
#! /bin/bash # this shell can run endlessfully echo "i love you ! " sh ./run 通过echo来显示了无限调 ...
- php screw加密与破解
一.破解工具之php-screw-brute 1.项目地址 https://github.com/securifybv/php-screw-brute 2.项目介绍 此脚本可以恢复/爆破php scr ...
- springboot使用elasticsearch的客户端操作eslaticsearch
一 ES客户端 ES提供多种不同的客户端: 1.TransportClient ES提供的传统客户端,官方计划8.0版本删除此客户端. 2.RestClient RestClient是官方推荐使用的 ...
- freeRTOS学习8-21
不能再中断服务程序调用该函数 应该调用xQueueSendFromISR()