题目链接: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. Android 网络请求Retrofit + RxJava

    一.背景 经常看到项目用Retrofit+RxJava+RxAndroid的框架,为了看懂项目的结构.现在来了解一下,Retrofit: Retrofit是Square 公司开发的一款正对Androi ...

  2. CentOS7设置开机启动方式(图形界面/命令行界面)

    CentOS 7由于使用systemd而不是init,所以不能通过修改inittab文件来修改开机启动模式. 先使用ctrl+alt+f2切换到命令行模式,然后输入命令:systemctl set-d ...

  3. 订阅发布模式eventEmiter

    // 订阅发布模式 class EventEmitter { constructor() { this._events = {}; } on(name, callback) { if (this._e ...

  4. Servlet 异常处理( 配置错误页面)

    当一个 Servlet 抛出一个异常时,Web 容器在使用了 exception-type 元素的 web.xml 中搜索与抛出异常类型相匹配的配置. 您必须在 web.xml 中使用 error-p ...

  5. Android 单元测试学习计划

    网上查了一下Android单元测试相关的知识点,总结了一个学习步骤: 1. 什么是单元测试2. 单元测试正反面: 2.1. 重要性 2.2. 缺陷 2.3. 策略3. 单元测试的基础知识: 3.1. ...

  6. java 增强for循环对于空集和null的判断

    List<String> list = null; for (String str : list) {//会报空指针异常 System.out.println(str); } List&l ...

  7. 一些php常用函数积累

    本文链接 <?php // id: ecffe70d3af54df9bad97b61918ace7d global $ct_path, $ct_log_path; $log_path = &qu ...

  8. Django:(03)请求和响应

    一.HttpRequest 客户端传参的几种方式 传递方式 示例 后端获取方式 数据类型 url路径(path) /news/1/2 正则匹配 str 查询字符串 /news2?category=1& ...

  9. 2019 Unreal Open Day —— 英特尔携手 UE 助力游戏开发生态建设

    2019 年 5 月 8 日-5 月 9 日,一年一度的 Unreal Open Day 虚幻引擎技术开放日在上海举办,该活动由 Epic Games 中国倾力打造,是面向虚幻引擎开发者规格最高.规模 ...

  10. nginx+gunicorn拓扑

    加装Nginx是否有必要 Nginx作为代理服务器,监听来自外部的80端口的请求:而Gunicorn负责监听本地机8000端口的请求.Nginx会把来自外部的请求转发给Gunicorn处理,接收到响应 ...