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

此题,竟然一时间没想到如何合理的解决方案,主要是有较长的时间没有使用INNOT IN.

SQL也是一个手熟的活,需要经常锻炼,以下是解题答案:

# Write your MySQL query statement below
SELECT Customers.Name AS Customers
FROM Customers
WHERE Customers.Id NOT IN (SELECT CustomerId FROM Orders)

PS:

如果您觉得我的文章对您有帮助,请关注我的微信公众号,谢谢!

LeetCode——Customers Who Never Order(灵活使用NOT IN以及IN)的更多相关文章

  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. LeetCode - Customers Who Never Order

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

  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. LeetCode:Binary Tree Level Order Traversal I II

    LeetCode:Binary Tree Level Order Traversal Given a binary tree, return the level order traversal of ...

  5. LeeCode(Database)-Customers Who Never Order

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

  6. 【SQL】183. Customers Who Never Order

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

  7. 183. Customers Who Never Order

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

  8. DataBase -- Customers Who Never Order

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

  9. Customers Who Never Order

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

随机推荐

  1. RabbitMQ获取队列的消息数目

    使用RabbitMQ,业务需求,想要知道队列中还有多少待消费待数据. 方式一: @Value("${spring.rabbitmq.host}") private String h ...

  2. ORA-1562 and ORA-1650 Unable to Extend Rollback Segment (Doc ID 1066542.6)

    ORA-1562 and ORA-1650 Unable to Extend Rollback Segment (Doc ID 1066542.6) APPLIES TO: Oracle Databa ...

  3. node.js中pm2启动应用出错

    在将公司的应用容器化时,遇到的问题. 之前,同事使用node.js 6版本运行,pm2启动js文件,一切正常. 但我一样将node.js 6版本安装,然后npm install pm2完成. 运行pm ...

  4. Leetcode146-lru-cache

    Leetcode146-lru-cache int capacity; int size; Map<Integer, ListNode> map = new HashMap<Inte ...

  5. layui实现分页

    一 准备工作 首先必须先引入layui的完整目录,也就是你下载下来的整个layui的目录都要放在你的资源文件夹下,也就是这个文件目录 刚开始接触layui的时候,以为和jquery,vue等框架一样, ...

  6. 【oracle】查看表空间信息

    -- 数据库查询表空间使用情况SELECT Upper(F.TABLESPACE_NAME) "表空间名", D.TOT_GROOTTE_MB/1024 "表空间大小(G ...

  7. Educational Codeforces Round 63 (Rated for Div. 2) E 带模高斯消元

    https://codeforces.com/contest/1155/problem/E 题意 \(f(x)=a_0+a_1x+a_2x^2+...+a_kx^k,k \leq 10,0 \leq ...

  8. 对比3种接口测试的工具:jmeter+ant;postman;python的requests+unittest或requests+excel

    这篇随笔主要是对比下笔者接触过的3种接口测试工具,从实际使用的角度来分析下3种工具各自的特点 分别为:jmeter.postman.python的requests+unittest或requests+ ...

  9. babel速览

    babel babel初衷 在es6出现之后,由于此版本的巨大改变,给人们带来了很多革命性的技术支持,但是当时很多浏览器对es6支持有限,babel就是为了达到写最新的语法,可以在任意浏览器运行而出现 ...

  10. JWT签名与验签

    签名Token生产 using System; using System.Collections.Generic; using System.IdentityModel.Tokens.Jwt; usi ...