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;
相关推荐
该用户已不存在15 分钟前
MySQL 与 PostgreSQL,该怎么选?
数据库·mysql·postgresql
GoldenaArcher36 分钟前
GraphQL 工程化篇 III:引入 Prisma 与数据库接入
数据库·后端·graphql
川石课堂软件测试38 分钟前
自动化测试之 Cucumber 工具
数据库·功能测试·网络协议·测试工具·mysql·单元测试·prometheus
RestCloud1 小时前
StarRocks 数据分析加速:ETL 如何实现实时同步与高效查询
数据库
lang201509281 小时前
MySQL数据类型存储全解析
mysql
野猪亨利6672 小时前
Qt day1
开发语言·数据库·qt
siriuuus2 小时前
Linux MySQL 多实例部署与配置实践
linux·运维·mysql
本就一无所有 何惧重新开始2 小时前
Redis技术应用
java·数据库·spring boot·redis·后端·缓存
isaki1372 小时前
qt day1
开发语言·数据库·qt
吗~喽2 小时前
【LeetCode】四数之和
算法·leetcode·职场和发展