180606_Combine&Find

合并后,查找未出现的ID

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.

Id Name
1 Joe
2 Henry
3 Sam
4 Max

Table: Customers.

Id Name
1 Joe
2 Henry
3 Sam
4 Max
Id CustomerId
1 3
2 1

Table: Orders.

Id CustomerId
1 3
2 1
Customers
Henry
Max

Using the above tables as example, return the following:

Customers
Henry
Max
SELECT C.Name as Customers
FROM Customers C
WHERE C.Id NOT IN (SELECT CustomerId
                  FROM Orders);
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • rljs by sennchi Timeline of History Part One The Cognitiv...
    sennchi阅读 7,424评论 0 10
  • 感觉忙不完的事,一天下来就是疲倦,总想喝他一杯。 现在生活非常的程式化,早起2小时的英语,然后是上班,不得空就磨洋...
    罗德_80b2阅读 116评论 1 0
  • 假如灵魂独自流浪,我一定得骑上那辆重型机车,背包也许是破旧的,里面会塞满书和感情。我要带把破木吉他,旅途需要不着调...
    二九九呀阅读 413评论 0 1
  • 上一期说到创业初期的市场调查和准备工作,当创业项目成立起来。需要正常运作时,就会形成团队。团队的建设在创业中...
    晋城小新阅读 233评论 0 0
  • 关于爱情 我们总是在寻找一个自己理想中的完美情人 就像购物 女生永远遇不到最爱的那件衣服 男生也买不到自己最想要的...
    IMCAI阅读 184评论 0 0