LeetCode //MySQL - 183. Customers Who Never Order

183. Customers Who Never Order

Table: Customers

±------------±--------+

| Column Name | Type |

±------------±--------+

| id | int |

| name | varchar |

±------------±--------+

id is the primary key (column with unique values) for this table.

Each row of this table indicates the ID and name of a customer.

Table: Orders

±------------±-----+

| Column Name | Type |

±------------±-----+

| id | int |

| customerId | int |

±------------±-----+

id is the primary key (column with unique values) for this table.

customerId is a foreign key (reference columns) of the ID from the Customers table.

Each row of this table indicates the ID of an order and the ID of the customer who ordered it.

Write a solution to find all customers who never order anything.

Return the result table in any order.

The result format is in the following example.

Example 1:

Input:

Customers table:

±---±------+

| id | name |

±---±------+

| 1 | Joe |

| 2 | Henry |

| 3 | Sam |

| 4 | Max |

±---±------+

Orders table:

±---±-----------+

| id | customerId |

±---±-----------+

| 1 | 3 |

| 2 | 1 |

±---±-----------+
Output:

±----------+

| Customers |

±----------+

| Henry |

| Max |

±----------+

From: LeetCode

Link: 183. Customers Who Never Order


Solution:

Ideas:
  1. LEFT JOIN is used to join the Customers table with the Orders table. This will include all rows from the Customers table and the matching rows from the Orders table.
  2. The WHERE O.customerId IS NULL clause filters out customers who have placed orders (i.e., those who have matching rows in the Orders table).
Code:
sql 复制代码
SELECT C.name AS Customers
FROM Customers C
LEFT JOIN Orders O ON C.id = O.customerId
WHERE O.customerId IS NULL;
相关推荐
rannn_1111 分钟前
【Redis|实战篇4】黑马点评|分布式锁
java·数据库·redis·分布式·后端
2301_793804691 分钟前
如何从Python初学者进阶为专家?
jvm·数据库·python
爬山算法1 分钟前
MongoDB(55)如何监控分片集群?
数据库·mongodb
m0_518019481 分钟前
用Python实现自动化的Web测试(Selenium)
jvm·数据库·python
云和数据.ChenGuang8 分钟前
AI向量数据库chromadb的swagger-ui的运行使用教程
大数据·数据库·人工智能·ui·向量数据库·向量计算
gjc5928 分钟前
【MySQL安全】密码插件指南:从配置到踩坑
数据库·mysql·安全
你才是臭弟弟11 分钟前
PostgreSQL + PostGIS(SQL操作)
数据库·sql·postgresql
book123_0_9914 分钟前
如何查看PostgreSQL的版本
数据库·postgresql
七七powerful17 分钟前
GreatSQL 深度解析:金融级开源数据库全攻略
数据库·金融·开源
麦聪聊数据17 分钟前
金融外包场景下的数据库权限管控:基于 B/S 架构的访问隔离与审计实践
数据库·sql·低代码·金融·restful