1. 程式人生 > 資料庫 >【SQL】LeetCode-Customers Who Never Order

【SQL】LeetCode-Customers Who Never Order

LeetCode 183:Customers Who Never Order

【Description】

Suppose that a website contains two tables, the Customers table and the Orders table. Write a SQL query to find all customers who never order anything.

Table: Customers.
在這裡插入圖片描述
Table: Orders.
在這裡插入圖片描述
Using the above tables as example, return the following:
在這裡插入圖片描述

【Solution】

Personal solution:

select name as customers
from customers as c
left join orders as o
on c.id =o.customerid
where o.id is null

Reference solution:

select customers.name as 'Customers'
from customers
where customers.id not in
(
    select customerid from orders
);