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;
相关推荐
今年不养猪只除草1 小时前
windows版本的时序数据库TDengine安装以及可视化工具
数据库·时序数据库·tdengine
极限实验室3 小时前
Easysearch 磁盘水位线注意事项
数据库
月落星还在4 小时前
Redis 单线程架构:化繁为简的性能哲学
数据库·redis·架构
十五年专注C++开发4 小时前
SQLiteStudio:一款免费开源跨平台的SQLite管理工具
数据库·c++·qt·sqlite
haoqi好奇4 小时前
uniapp+node+mysql接入deepseek实现流式输出
android·mysql·uni-app
啥都想学的又啥都不会的研究生4 小时前
Redis设计与实现-服务器中的数据库
运维·服务器·数据库·redis·笔记·缓存·性能优化
m0_748229994 小时前
redis 使用
数据库·redis·缓存
Foolforuuu4 小时前
什么样的场景适用redis?redis缓存是什么?
数据库·redis·缓存
m0_748234084 小时前
redis 清理缓存
数据库·redis·缓存
智享AI5 小时前
阿里云工作空间与Ollama(一)
数据库·阿里云·云计算