diff --git a/01-ProductSalesAnalysis-III.sql b/01-ProductSalesAnalysis-III.sql new file mode 100644 index 0000000..1bc83ee --- /dev/null +++ b/01-ProductSalesAnalysis-III.sql @@ -0,0 +1,8 @@ +-- Mock Interview Question 1: Product Sales Analysis III (https://leetcode.com/problems/product-sales-analysis-iii) + +with cte as (select product_id, year, quantity, price, +dense_rank() over(partition by product_id order by year) as rnk +from sales) +select product_id, year as first_year, quantity, price +from cte +where rnk=1 \ No newline at end of file diff --git a/02-CustomersWhoBoughtAllProducts.sql b/02-CustomersWhoBoughtAllProducts.sql new file mode 100644 index 0000000..7150dbe --- /dev/null +++ b/02-CustomersWhoBoughtAllProducts.sql @@ -0,0 +1,23 @@ +-- Mock Interview Question 2: Customers Who Bought All Products https://leetcode.com/problems/customers-who-bought-all-products + +-- solution 1 + +with +p_cte as( + select count(product_key) as tot_pdt + from product +), +c_cte as ( + select customer_id, count(distinct product_key) as c_pdt + from customer group by customer_id +) +select customer_id +from p_cte, c_cte +where tot_pdt=c_pdt + +-- solution 2 + +select customer_id +from customer +group by customer_id +having count(distinct product_key) = (select count(product_key) from product) \ No newline at end of file