SQL/문제

SQL | Customer Who Visited but Did Not Make Any Transactions, Rising Temperature

jjangdoll 2025. 1. 5. 12:31

Customer Who Visited but Did Not Make Any Transactions

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


 -- 거래없이 방문만 한 사용자 아이디, 방문한 횟수 조회
SELECT
    v.customer_id,
    count(v.visit_id) count_no_trans
FROM 	-- 거래내역에서 NULL을 찾아야 하기 때문에 LEFT JOIN으로 모든 값 조회
     Visits v LEFT JOIN Transactions t ON t.visit_id = v.visit_id
WHERE t.transaction_id IS NULL
GROUP BY v.customer_id ;

💡 NULL 값을 찾기 위해 LEFT JOIN을 사용해 모든 데이터를 조회


Rising Temperature

https://leetcode.com/problems/rising-temperature/description/


 

-- 어제보다 온도가 높은 모든 날짜 조회
SELECT
   t.id
FROM
    weather t 
    JOIN weather y 
    ON t.recordDate = DATE_ADD(y.recordDate, INTERVAL 1 DAY)	
    -- 같은 테이블을 조인하고 한 테이블 날짜 데이터에 하루씩 추가해서 날짜 차이를 줌
WHERE t.temperature > y.temperature ;

💡 뭔가 CROSS JOIN? 도 활용할 수 있을 것 같음