Notice
Recent Posts
Recent Comments
Link
| 일 | 월 | 화 | 수 | 목 | 금 | 토 |
|---|---|---|---|---|---|---|
| 1 | 2 | 3 | 4 | 5 | 6 | 7 |
| 8 | 9 | 10 | 11 | 12 | 13 | 14 |
| 15 | 16 | 17 | 18 | 19 | 20 | 21 |
| 22 | 23 | 24 | 25 | 26 | 27 | 28 |
Tags
- 알파회계
- Kernighan의 C언어 프로그래밍
- 처음 만나는 AI 수학 with Python
- network configuration
- 서버설정
- 페이징
- baeldung
- 데비안
- 구멍가게코딩단
- 티스토리 쿠키 삭제
- 네트워크 설정
- 리눅스
- 자료구조와함께배우는알고리즘입문
- iterator
- 자료구조와 함께 배우는 알고리즘 입문
- 자바편
- 선형대수
- 코드로배우는스프링부트웹프로젝트
- 이터레이터
- 코드로배우는스프링웹프로젝트
- 처음 만나는 AI수학 with Python
- 스프링부트핵심가이드
- /etc/network/interfaces
- 친절한SQL튜닝
- 목록처리
- ㅒ
- 스프링 시큐리티
- d
- resttemplate
- GIT
Archives
- Today
- Total
bright jazz music
183. Customers Who Never Order 본문
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 |
+-----------+
---
solution
# Write your MySQL query statement below
select customers.name as 'customers'
from customers
where customers.id not in
(
select customerId from orders
)
위의 쿼리는 정답으로 표기된 쿼리이다. 서브쿼리 방식을 사용하며 109ms가 걸렸다.
내가 사용하려고 했던 쿼리는 아래의 LEFT JOIN 쿼리이다. 왼쪽 테이블의 레코드에 맞춰 오른쪽 테이블의 레코드가 join되는 방식이며, 왼쪽 쿼리의 조건에 맞지 않는 레코드는 NULL 처리한다.
SELECT Name AS 'Customers'
FROM Customers c
LEFT JOIN Orders o
ON c.Id = o.CustomerId
WHERE o.CustomerId IS NULL
이 방식은 128ms가 걸렸다.
'Algorithm Practice > LeetCode' 카테고리의 다른 글
| 605. Can place flowers (0) | 2026.01.13 |
|---|---|
| 1. Two Sum (1) | 2022.11.27 |
| 584. Find Customer Referee (0) | 2022.11.26 |
| 1757. Recyclable and Low Fat Products (0) | 2022.11.26 |
| 595. Big Countries (0) | 2022.11.25 |
Comments