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 |
+-----------+
思路一:用IN
 select Name from Customers where Id not in (select CustomerId from Orders);

思路二:用exists

select Name from Customers c where not exists (select Id from Orders o where c.Id = o.CustomerId);

思路三:用左连接

select Name from Customers c left join Orders o on c.Id = o.CustomerId where o.Id is NULL;

Customers Who Never Order的更多相关文章

  1. [LeetCode] Customers Who Never Order 从未下单订购的顾客

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

  2. LeeCode(Database)-Customers Who Never Order

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

  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. 【SQL】183. Customers Who Never Order

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

  5. LeetCode - Customers Who Never Order

    Description: Suppose that a website contains two tables, the Customers table and the Orders table. W ...

  6. 183. Customers Who Never Order

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

  7. DataBase -- Customers Who Never Order

    Question: Suppose that a website contains two tables, the Customers table and the Orders table. Writ ...

  8. LeetCode——Customers Who Never Order(灵活使用NOT IN以及IN)

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

  9. 分别针对Customers表与Order表的通用查询操作

    1.针对customers表通用的查询操作 CustomerForQuery package com.aff.PreparedStatement; import java.lang.reflect.F ...

随机推荐

  1. assert.strictEqual()

    assert.strictEqual(actual, expected[, message]) 使用全等运算符(===)测试 actual 参数与 expected 参数是否全等. // 格式 ass ...

  2. redis 有用 Sorted-Set 应用场景

    1.1.1Set数据类型的 使用场景 1.可以使用Redis的Set数据类型跟踪一些唯一性数据,比如访问某一博客的唯一IP地址信息.对于此场景,我们仅需在每次访问该博客时将访问者的IP存入Redis中 ...

  3. qboimathtest1 t2 配对

    题目 有1~n一共n个数,n为偶数.小Q要把这n个数随机地两两配对.令每一对的权值为它们两个数的和.小Q想要知道这n/2对里最大的权值的期望是多少.请输出答案对10^9+7取模的值. [输入] 一行一 ...

  4. p2320&bzoj1192 鬼谷子的钱袋

    传送门(洛谷) 传送门(bzoj) 题目 鬼谷子非常聪明,正因为这样,他非常繁忙,经常有各诸侯车的特派员前来向他咨询时政.有一天,他在咸阳游历的时候,朋友告诉他在咸阳最大的拍卖行(聚宝商行)将要举行一 ...

  5. 解决Umbraco中Generated文件夹下面model问题

    在Visual Studio中开发Umbraco项目时,有一个文件夹叫Generated, 在Umbraco 的back office中的Document Type产生的model都会自动进入这个文件 ...

  6. python接口自动化(三十六)-封装与调用--流程类接口关联续集(详解)

    简介 上一篇已经给大家都介绍过了流程类接口关联,但是由于博客的登录机制改变,所以没有办法给小伙伴们实战演练一下,那么这篇就按照上一篇计划的用jenkins来给小伙伴们演示一下流程类接口的封装和调用,其 ...

  7. Docker删除私有仓库镜像

    V2 安装删除脚本 # curl https://raw.githubusercontent.com/burnettk/delete-docker-registry-image/master/dele ...

  8. [Django笔记] django结构分析

    看了一段时间django官方文档,先把自己理解的django目录结构记录一下 目录结构 一个成熟的django项目的结构大致如下: /project/ |-- project/ |-- setting ...

  9. OTRS 二次开发笔记

    公司使用otrs系统处理业务工单,各种事件流.因为是开源免费系统,因此需要在上面做一些功能补充或定制的二次开发. otrs是什么? OTRS 是一个功能强大的工单系统.完美适用于服务台(Help De ...

  10. 【转】processOnServer

    源地址:http://blog.csdn.net/dl020840504/article/details/8856853