MySQL 풀이 | Customer Who Visited but Did Not Make Any Transactions

2025. 2. 10. 19:41·SQL

문제 1.  Customer Who Visited but Did Not Make Any Transactions

Table: Visits

+-------------+---------+
| Column Name | Type    |
+-------------+---------+
| visit_id    | int     |
| customer_id | int     |
+-------------+---------+
visit_id is the column with unique values for this table.
This table contains information about the customers who visited the mall.
 

Table: Transactions

+----------------+---------+
| Column Name    | Type    |
+----------------+---------+
| transaction_id | int     |
| visit_id       | int     |
| amount         | int     |
+----------------+---------+
transaction_id is column with unique values for this table.
This table contains information about the transactions made during the visit_id.
 

Write a solution to find the IDs of the users who visited without making any transactions and the number of times they made these types of visits.

Return the result table sorted in any order.

The result format is in the following example.

 
Example 1:

Input: 
Visits
+----------+-------------+
| visit_id | customer_id |
+----------+-------------+
| 1        | 23          |
| 2        | 9           |
| 4        | 30          |
| 5        | 54          |
| 6        | 96          |
| 7        | 54          |
| 8        | 54          |
+----------+-------------+
Transactions
+----------------+----------+--------+
| transaction_id | visit_id | amount |
+----------------+----------+--------+
| 2              | 5        | 310    |
| 3              | 5        | 300    |
| 9              | 5        | 200    |
| 12             | 1        | 910    |
| 13             | 2        | 970    |
+----------------+----------+--------+
Output: 
+-------------+----------------+
| customer_id | count_no_trans |
+-------------+----------------+
| 54          | 2              |
| 30          | 1              |
| 96          | 1              |
+-------------+----------------+

https://leetcode.com/problems/customer-who-visited-but-did-not-make-any-transactions/


💡 문제 풀이 

`내 풀이`

select customer_id, 
    count(customer_id) as count_no_trans
from Visits 
left join Transactions using(visit_id)
where transaction_id is null
group by customer_id

 

`실행 결과`

'SQL' 카테고리의 다른 글
  • PostgreSQL 풀이 | User's Third Transaction (ROW_NUMBER, RANK, DENSE_RANK)
  • PostgreSQL 풀이 | Tweets' Rolling Averages (윈도우함수 ROWS BETWEEN)
  • MySQL 풀이 | Product Sales Analysis I
  • MySQL 풀이 | Replace Employee ID With The Unique Identifier
초담
초담
4년차 마케터입니다
  • 초담
    그로스마케터의 기록
    초담
  • 전체
    오늘
    어제
  • 글쓰기 관리
    • 분류 전체보기 (117)
      • Data Analytics Project (3)
      • SQL (55)
      • Python (43)
      • GA4 (0)
      • Tableau (8)
      • 아티클 스터디 (7)
  • 인기 글

  • 최근 글

  • hELLO· Designed By정상우.v4.10.2
초담
MySQL 풀이 | Customer Who Visited but Did Not Make Any Transactions
상단으로

티스토리툴바