From 875b4c007cc2d7886c912e9289b4409e75cb2246 Mon Sep 17 00:00:00 2001 From: Hinduja Cheela Date: Wed, 4 Jun 2025 14:50:47 -0500 Subject: [PATCH] Added two solutions --- CustomersWho.sql | 5 +++++ ProductAnalysis.sql | 19 +++++++++++++++++++ README.md | 3 ++- 3 files changed, 26 insertions(+), 1 deletion(-) create mode 100644 CustomersWho.sql create mode 100644 ProductAnalysis.sql diff --git a/CustomersWho.sql b/CustomersWho.sql new file mode 100644 index 0000000..5f57ed6 --- /dev/null +++ b/CustomersWho.sql @@ -0,0 +1,5 @@ + +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 diff --git a/ProductAnalysis.sql b/ProductAnalysis.sql new file mode 100644 index 0000000..6d8e28f --- /dev/null +++ b/ProductAnalysis.sql @@ -0,0 +1,19 @@ + +-- Solution 1 + +select product_id, year as first_year, quantity, price +from ( + select s.product_id, s.year, s.quantity, s.price, + DENSE_RANK() over (partition by product_id order by year) as rnk + from sales s +) ranked +where rnk = 1; + +-- Solution 2 + +select s.product_id,newtab.first_year,s.quantity,s.price +from (select product_id, min(year) as first_year + from sales + group by product_id) as newtab +join sales s +on newtab.product_id=s.product_id and newtab.first_year=s.year; diff --git a/README.md b/README.md index eceb926..9fd0400 100644 --- a/README.md +++ b/README.md @@ -1 +1,2 @@ -# Mock_SQL2 \ No newline at end of file +# Mock_SQL2 +