题目链接: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

思考

  1. 通过 not exists 实现,效率最高;
  2. 通过 left join 关联之后为空实现;

LeetCode:183.从不订购的客户的更多相关文章

  1. 力扣(LeetCode)从不订购的客户-数据库题 个人题解

    SQL架构 某网站包含两个表,Customers 表和 Orders 表.编写一个 SQL 查询,找出所有从不订购任何东西的客户. Customers 表: +----+-------+ | Id | ...

  2. sql 183. 从不订购的客户

    SQL架构 某网站包含两个表,Customers 表和 Orders 表.编写一个 SQL 查询,找出所有从不订购任何东西的客户. Customers 表: +----+-------+ | Id | ...

  3. [SQL]LeetCode183. 从不订购的客户 | Customers Who Never Order

    Suppose that a website contains two tables, the Customers table and the Orders table. Write a SQL qu ...

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

    题目标签: 题目给了我们 Customers 和 Orders 两个表格,让我们找到 从没订购过的客户. 首先从Orders 得到 订购过的CustomerId,然后再去Customers 里找 没有 ...

  5. [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 ...

  6. leetcode 183. Customers Who Never Order

    select Name as Customers from Customers where Id not in (select CustomerId from Orders);

  7. LeetCode:数据库技术【180-185】

    LeetCode:数据库技术[180-185] 180.连续出现的数字 题目描述 编写一个 SQL 查询,查找所有至少连续出现三次的数字. +----+-----+ | Id | Num | +--- ...

  8. leetcode题库

    leetcode题库 #题名题解通过率难度出现频率  1 两数之和     46.5%简单2 两数相加     35.5%中等3 无重复字符的最长子串     31.1%中等4 寻找两个有序数组的中位 ...

  9. leetcode 0218

    目录 ✅ 1200. 最小绝对差 描述 解答 cpp py ✅ 897. 递增顺序查找树 描述 解答 cpp 指针问题? fuck ptr py ✅ 183. 从不订购的客户 描述 解答 sql to ...

随机推荐

  1. 前端知识点回顾——Javascript篇(二)

    JavaScript的解析顺序 第一阶段:编译期 寻找关键字声明的变量.函数声明的变量,同时会对变量进行作用域的绑定 var声明的变量,在编译期会赋一个默认值undefined,变量提升的特性. ES ...

  2. mybatis一对多映射【班级与学生】

    1)如图 2)创建grades.sql和students.sql drop table students; drop table grades; create table grades( gid in ...

  3. chrome调试笔记

    F12启动调试 1.右键加载按钮可以清空缓存并重新加载,有时候浏览器有缓存,代码更新不会及时反映出来. 2.performance mointer实时查看performance 点击三个竖着的小点,选 ...

  4. maven项目无法导入Oracle的jdbc连接jar包【我】

    导入一个maven项目,启动一直报错 找不到Oracle的jdbc连接驱动Class, 用Eclipse导入 ojdbc14.jar 连接包,各种方法都无效,无法引入到部署后的lib目录中,可以直接放 ...

  5. Django学习之缓存和信号

    Django学习之缓存和信号   一 缓存 由于Django是动态网站,所有每次请求均会去数据进行相应的操作,当程序访问量大时,耗时必然会更加明显,最简单解决方式是使用:缓存,缓存将一个某个views ...

  6. SCI EI 检索的会议期刊

    SCI EI 收录 SCI EI 收录 SCI Impact Factor Search: http://www.heavyoil.cn/if.aspx SCI indexed journal lis ...

  7. SQL引擎及事务支持

    查看当前表引擎 SHOW CREATE TABLE table_name MyISAM类型不支持事务处理等高级处理,而InnoDB类型支持.MyISAM类型的表强调的是性能,其执行数度比InnoDB类 ...

  8. Linux服务器上安装openoffice,以及安装字体文件

    1.安装openoffice (1)将openoffice的linux安装包放到linux指定的文件下(一般放在opt下) (2)在安装包的目录下执行命令:tar -zxvf 对应的压缩包名字 (3) ...

  9. Windows To Go 制作详解

    拥有 Mac 的同学大概都会碰到一个头疼的问题,那就是使用 Windows 的使用需求.macOS 虽好,不过总是会有一些讨厌的软件没有 Mac 版本,这时就不得不在 Mac 上跑 Windows 了 ...

  10. MariaDB基本知识点总结01--介绍+语句

    一.概念 1.数据库介绍: 简单的说,数据库就是一个存放数据的仓库,这个仓库是按照一定的数据结构(数据结构是指数据的组织形式或数据之间的联系)来组织,存储的,我们可以通过数据库提供的多种方法来管理数据 ...