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. ctime、atime、mtime时间

    区分一个文件或者目录的更改时间(change time,ctime),访问时间(access time,atime)以及修改时间(modify time,mtime)很重要. ctime——在Unix ...

  2. 基于http的多进程并发文件服务器

    1 可以掌握的知识点 (1) 线上部署时的守护应用 (2) 常规的文件操作,配置文件读取 (3) 网络编程,端口复用等文件 (4) 多进程知识 2 代码注释如下 test_httpd.h #inclu ...

  3. Entity Framework Code-First(8):Configure Domain Classes

    Configure Domain Classes in Code-First: We learned default Code-First Conventions in the previous se ...

  4. Struts2学习第五课 通过和ServletAPI耦合的方式获取WEB资源

    与Servlet耦合的访问方式 直接访问Servlet API将使Action与环境Servlet环境耦合在一起,测试时需要有Servlet容器,不便对Action的单元测试. 直接获取HttpSer ...

  5. Linus与陈庆

    Linus 1969年末,李纳斯出生于芬兰的赫尔辛基市,算是赶上了60后的尾巴.小时候他是个其貌不扬的孩子,除了一个鼻子长的「富丽堂皇」之外乏善可陈.他为了让鼻子看上去小一些,经常戴上眼镜就不愿意摘下 ...

  6. Netty 线程模型与Reactor 模式

    前言 Netty 的线程模型是基于NIO的Selector 构建的,使用了异步驱动的Reactor 模式来构建的线程模型,可以很好的支持成百上千的 SocketChannel 连接.由于 READ/W ...

  7. 如何使用ros命令行显示图片

    rosrun image_view image_view image:=[TOPIC] 注意:每次只能显示一个UI.不能在一条命令中订阅多个节点.

  8. Linux查看系统位数

    查看linux是多少位的几位方法   方法/步骤     方法一:getconf LONG_BIT 在linux终端输入getconf LONG_BIT命令 如果是32位机器,则结果为32 Linux ...

  9. [Xcode 实际操作]四、常用控件-(6)UISwitch开关控件的使用

    目录:[Swift]Xcode实际操作 本文将演示开关控件的基本用法. 开关控件有两个互斥的选项,它是用来打开或关闭选项的控件. 在项目导航区,打开视图控制器的代码文件[ViewController. ...

  10. Discuz!快速对接个人支付插件

    ## Discuz!快速对接个人支付插件 由于近期准备使用老牌论坛程序Discuz建立一个交流社区分享一些资源,但是测试了各种支付方式都不满意,偶然发现一个简直不要太完美的解决方案.今天抽时间搭建好并 ...