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. 奇妙的go语言(网页下载)

    [ 声明:版权全部,欢迎转载,请勿用于商业用途.  联系信箱:feixiaoxing @163.com] 眼下,网上关于网页爬行的代码非常多.可是,自从看了go语言的web下载代码之后,我才发现原来它 ...

  2. C++[类设计] ini配置文件读写类config

      //in Config.h #pragma once #include <windows.h> #include <shlwapi.h> #pragma comment(l ...

  3. 通过模拟器和ida搭建Android动态调试环境的问题

    这几天在学Android的native层逆向.在按照教程用ida搭建动态调试环境时,第一步是把android_server 放到手机里执行,但是在手机里可以,在genymotion模拟器上就提示 no ...

  4. [转] DAG算法在hadoop中的应用

    http://jiezhu2007.iteye.com/blog/2041422 大学里面数据结构里面有专门的一章图论,可惜当年没有认真学习,现在不得不再次捡 起来.真是少壮不努力,老大徒伤悲呀!什么 ...

  5. Android项目中如何用好构建神器Gradle?(转)

    最近在忙团队并行开发的事情,主要是将各个团队的代码分库,一方面可以降低耦合,为后面模块插件化做铺垫,另一方面采用二进制编译,可以加快编译速度.分库遇到了一些问题,很多都要通过Gradle脚本解决,所以 ...

  6. Mui实现退出登录

    document.getElementById("logout").addEventListener("tap",function(){ var btn=[&q ...

  7. 文本溢出、垂直外边距合并、BFC、hasLayout

    今天学习文本溢出,又遇到了一些小问题,先上图: 关于文本溢出推荐:http://www.cnblogs.com/yzg1/p/5089534.html 从里面学习到单行文本和多行文本溢出, overf ...

  8. Android开发手记(9) DatePickerDialog 和 TimePickerDialog

    1.DatePickerDialog  用于获取用户输入的日期信息.其原型为: public DatePickerDialog(Contex contex, DatePickerDialog.OnDa ...

  9. PartialViewResult用法

    后台代码 ) { IList<TestModel> lstTestModel = this.GetModelList(categoryid); return PartialView(lst ...

  10. vsftp的设置选项

    设置匿名用户上传的文件的权限: anon_umask=  匿名用户新增文件的umask 数值.默认值为077.     VSFTPD的设置选项 VSFTPD的配置文件/etc/vsftpd/vsftp ...