♟️ 알고리즘/Leetcode

[MySQL][Leet Code] 1581. Customer Who Visited but Did Not Make Any Transactions (Easy)

Jerry_K 2025. 2. 17. 20:57

https://leetcode.com/problems/customer-who-visited-but-did-not-make-any-transactions/description/?envType=study-plan-v2&envId=top-sql-50


🗒️SQL 코드 풀이 

SELECT customer_id, COUNT(*) count_no_trans 
FROM Visits v LEFT JOIN Transactions t ON v.visit_id = t.visit_id
WHERE t.visit_id IS NULL
GROUP BY customer_id

 

1. LEFT JOIN을 하고 , visit_id 컬럼이 2개가 나오는데, 그 중 Transactions의 컬럼의 visit_id 의 NULL 값을 조회한다.

 

2. 이후 원하는 값을 출력하기 위해서는 Group by를 해야한다. 

 

 

📌 문제 코멘트 

Group by 없이 COUNT를 하려다 오류가 발생했다.

Group by도 잘 기억해보자. 


📚문제