본문 바로가기

SQL

(SQL) - LeetCode (easy) 1084. Sales Analysis III

반응형

https://leetcode.com/problems/sales-analysis-iii/description/

 

Sales Analysis III - LeetCode

Can you solve this real interview question? Sales Analysis III - Table: Product +--------------+---------+ | Column Name | Type | +--------------+---------+ | product_id | int | | product_name | varchar | | unit_price | int | +--------------+---------+ pro

leetcode.com

join과 group by를 사용해본 문제였습니다.

📕 풀이방법

📔 풀이과정

sales과 product table을 join 후에 봄에 팔린 적이 없는 product_id, product_name을 select해줍니다.


📕 Code

📔 ANSI SQL

select p.product_id, p.product_name from sales s
join product p
on p.product_id = s.product_id
group by p.product_id
having sum(case when s.sale_date not between '2019-01-01' and '2019-03-31' then 1 else 0 end) = 0

*더 나은 내용을 위한 지적, 조언은 언제나 환영합니다.