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;
相关推荐
DemonAvenger7 分钟前
数据库日志系统深度解析:从binlog到redo/undo日志的实践指南
数据库·mysql·性能优化
潘潘潘潘潘潘潘潘潘潘潘潘31 分钟前
【MySQL】从零开始学习MySQL:基础与安装指南
linux·运维·服务器·数据库·学习·mysql
元闰子33 分钟前
OLTP上云,哪种架构最划算?·VLDB'25
数据库·后端·云原生
寻星探路38 分钟前
数据库造神计划第八天---增删改查(CRUD)(4)
数据库·sql·mysql
小猪咪piggy39 分钟前
【算法】day2 双指针+滑动窗口
数据结构·算法·leetcode
Java烘焙师40 分钟前
架构师必备:缓存更新模式总结
mysql·缓存
马走日mazouri41 分钟前
深入理解MySQL主从架构中的Seconds_Behind_Master指标
数据库·分布式·mysql·系统架构·数据库架构
0_0梅伊阁诗人2 小时前
Flask
开发语言·数据库·python·flask
小兜全糖(xdqt)6 小时前
pyspark 从postgresql读取数据
数据库·postgresql
姓刘的哦8 小时前
Qt中的QWebEngineView
数据库·c++·qt