LeetCode - 183. Customers Who Never Order(Database)*

YAMAMAMO·2022년 4월 10일
0

LeetCode

목록 보기
50/100

문제

SQL Schema
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.

SQL 쿼리를 작성하여 아무것도 주문하지 않는 모든 고객을 보고합니다.
결과 테이블을 임의의 순서로 반환합니다.
쿼리 결과 형식은 다음 예제와 같습니다.

https://leetcode.com/problems/customers-who-never-order/

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       |
+-----------+

풀이

MySql 입니다.

# Write your MySQL query statement below
SELECT A.Name AS Customers from Customers A
WHERE NOT EXISTS (SELECT 1 FROM Orders B WHERE A.Id = B.CustomerId)

SELECT A.Name from Customers A
LEFT JOIN Orders B on  a.Id = B.CustomerId
WHERE b.CustomerId is NULL

SELECT A.Name AS Customers from Customers A
WHERE A.Id NOT IN (SELECT B.CustomerId from Orders B)
profile
안드로이드 개발자

0개의 댓글