题目链接: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. 前端知识点回顾——HTML,CSS篇

    前端知识点回顾篇--是我当初刚转行为了面试而将自己学过的前端知识整理成的一份笔记,个人目的性很强,仅供参考. doctype 有什么用 doctype是一种标准通用标记语言的文档类型声明,目的是告诉标 ...

  2. ios-动态添加方法,交换方法,重定向方法

    新建一个类Person,Person.h 不写代码,Person.m 有如下两个方法: - (void)eat { NSLog(@"xxx eat===="); } [动态添加方法 ...

  3. leetcode1283 使结果不超过阈值的最小除数

    这道题第一思路是用二分查找 因为使用二分法:所以复杂度为O(n*logk), k介于 left=sum/threshold(向下取整) 和 right=num_max之间:而right<=10^ ...

  4. 001-软件架构概览、maven补充【分包工程、合并包、web容器插件】、git补充

    一.整体概述 1.1.共性问题 技术瓶颈.不成体系.不能实际使用.不能落地.无法入门 1.2.目标-软件架构 专注于构建:高可扩展.高性能.大数据量.高并发.分布式的系统架构. 各项技术.组合构建分布 ...

  5. VUE中v-for和v-if不能同时使用的问题

    摘抄自:https://www.cnblogs.com/showjs/p/11376446.html 在开发时候碰到了一个问题:v-if和v-for不能同时使用: <h-tab-pane v-f ...

  6. [Feature] Build pipeline

    准备数据集 一.数据集 Ref: 6. Dataset loading utilities[各种数据集选项] 第一部分,加载原始iris数据集的数据: 第二部分,先增加一行,再增加一列: #%% pa ...

  7. nodejs的事件循环1

    JavaScript的学习零散而庞杂,因此很多时候我们学到了一些东西,但是却没办法感受到自己的进步,甚至过了不久,就把学到的东西给忘了.为了解决自己的这个困扰,在学习的过程中,我一直试图在寻找一条核心 ...

  8. delphi数据集查找不定位

    procedure TForm1.Button2Click(Sender: TObject); var R: Variant; begin R := MemTableEh1.Lookup('Name' ...

  9. web页面找不到资源文件,报404,但是资源文件存在且路径没错

    如题 , 今天遇到这个问题,maven项目导入本地myeclipse,正常跑起来之后,在web端存在部分页面资源加载不进来. 但是项目资源确实存在,一开始以为是myeclipse开发环境搭建错误导致, ...

  10. SpringBoot: 8.整合freemarker(转)

    1.创建maven项目,添加pom依赖 <!--springboot项目依赖的父项目--> <parent> <groupId>org.springframewor ...