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;
相关推荐
大海星辰79816 分钟前
一次对账SQL“灵异事件”排查:NOT IN返回空集的坑
mysql
Linux-lucky30 分钟前
32-38-Linux学习之旅之MySQL综合
linux·运维·学习·mysql·ubuntu
一 乐2 小时前
二手交易平台|基于springboot + vue二手交易平台(源码+数据库+文档)
java·数据库·vue.js·spring boot·小程序
SelectDB3 小时前
同等资源下 Apache Doris 4.2 vs StarRocks 4.1.1:1TB SSB 与 TPC-H 性能实测
大数据·数据库·数据分析
这个DBA有点耶3 小时前
数据库集群与分布式架构:三条技术路线对比、金仓KES RAC实测数据与决策框架
数据库·程序员·架构
MayBaymax3 小时前
MongoDB 索引与事务
java·数据库·mongodb
SelectDB3 小时前
Doris vs ClickHouse:企业 OLAP 走向下一阶段,两种技术路线如何选择
大数据·数据库·数据分析
vx-程序开发3 小时前
【计算机毕设】django校园跑腿服务系统83141
java·数据库·vue.js·spring boot·spring·elasticsearch·django
&不羁之风&3 小时前
从 MySQL 到 MongoDB:80 万系统日志(sys_log)平滑迁移完整实战(含 Windows 环境 mongoimport 安装与避坑)
mysql·mongodb
Tisfy3 小时前
LeetCode 3498.字符串的反转度:一次遍历
leetcode·字符串·题解·遍历