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 |
+-----------+

思路:
way 1:
嵌套子查询,适合多表格比较的情况。在Customers表中选择Id不在Orders表的CustomerId集合里面的元组
way 2:
左外连接实现。首先要搞明白普通的连接功能是找到两个表格的公共集合然后形成新的表。
左外连接的关键词是left join或者left outer join,然后再加一个on,其实质就是一种构造基本表的方式
通过左外连接得到新的表后,再选出相应的元组即可

way 1:

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

way 2:

select Name
from Customers as c
left outer join Orders as d
on c.Id = d.CustomerId
where CustomerId is null

SQL-Customers Who Never Order的更多相关文章

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

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

  2. 【SQL】183. Customers Who Never Order

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

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

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

  4. LeeCode(Database)-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. Customers Who Never Order

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

  9. 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 ...

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

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

随机推荐

  1. android如何让service不被杀死

    1.在service中重写下面的方法,这个方法有三个返回值, START_STICKY是service被kill掉后自动重写创建 @Override     public int onStartCom ...

  2. Android - Service启动机制

      以下资料摘录整理自老罗的Android之旅博客,是对老罗的博客关于Android底层原理的一个抽象的知识概括总结(如有错误欢迎指出)(侵删):http://blog.csdn.net/luoshe ...

  3. hdu 2123

    #include <iostream> using namespace std; int main() { int i,t,n,j,k,f; cin>>t; while(t-- ...

  4. android系统360浏览器使用append动态添加元素出现黑色错乱背景

    去掉样式 backface-visibility:hidden;

  5. CentOS5.5下安装Ant

    从yum源直接下ant: [root@master local]$ yum install ant 运行ant,发现报错: java.lang.NoClassDefFoundError: org/ap ...

  6. Css3案例

    <!DOCTYPE html> <html> <meta charset=utf-> <head> <style> body{ backgo ...

  7. RequireJS学习笔记(转)

    前言 进入移动前端是很不错的选择,这块也是我希望的道路,但是不熟悉啊... 现在项目用的是require+backbone,整个框架被封装了一次,今天看了代码搞不清楚,觉得应该先从源头抓起,所以再看看 ...

  8. Poweroff – 很好很强大的定制关机工具

    Poweroff – 很好很强大的定制关机工具 Poweroff 是一个用来管理电脑关机系统的小工具,支持定时,支持远程 作者开放源代码,有兴趣的同学可以尝试着制作一下汉化版本.   可以设定不同时间 ...

  9. 《python基础教程》笔记之 字典

    字典创建 字典由多个键值对组成,每个键和对应值之间用冒号隔开,项之间用逗号隔开,而整个字典用一对大括号括起来,如 >>> phonebook={'alice':'0123', 'Be ...

  10. c语言输入一行未知个数数字存入数组

    一直有个疑问输入一行数字存入数组时若不知道数字的个数怎么办,最容易想到的办法就是接收字符然后转化为数字,但这样太过麻烦. 今天上网查了下,说可以用ungetc()函数将字符送回输入流,在这里总结归纳一 ...