Customers Who Never Order - LeetCode
Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.
leetcode.com
Table: Customers
+-------------+---------+
| Column Name | Type |
+-------------+---------+
| id | int |
| name | varchar |
+-------------+---------+
id is the primary key column 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 for this table.
customerId is a foreign key 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 an SQL query to report all customers who never order anything.
Return the result table in any order.
The query 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 |
+-----------+
문제 조건
주문한 적이 없는 모든 고객을 출력하시오.
문제 풀이
select c.name as 'Customers'
from customers c
left join orders o on c.id = o.customerId
where o.customerId is null
'SQL > LeetCode' 카테고리의 다른 글
[180_MEDIUM] Consecutive Numbers 다시풀기 (0) | 2022.09.26 |
---|---|
[197_EASY] Rising Temperature (0) | 2022.09.25 |
[178_MEDIUM] Rank Scores (0) | 2022.09.23 |
[177_MEDIUM] Nth Highest Salary (0) | 2022.09.23 |
[176_MEDIUM] Second Highest Salary (0) | 2022.09.23 |