From c629bcb123463301d89b8912055f519d37874f2f Mon Sep 17 00:00:00 2001 From: sagar1023 Date: Wed, 21 May 2025 20:38:52 -0400 Subject: [PATCH 1/3] Done HW1 --- HW1.txt | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 HW1.txt diff --git a/HW1.txt b/HW1.txt new file mode 100644 index 0000000..67c14ad --- /dev/null +++ b/HW1.txt @@ -0,0 +1,26 @@ +# Write your MySQL query statement below + +# Big Countries Solution + +select name, population, area from World where area >='3000000' or population >='25000000'; + +# Nth Highest Salary + +# Nth Highest Salary + +CREATE FUNCTION getNthHighestSalary(N INT) RETURNS INT +BEGIN + RETURN ( + # Write your MySQL query statement below. +WITH CTE AS( + select *,DENSE_RANK() OVER (ORDER BY salary DESC) as rnk from Employee +) +SELECT distinct (ifnull(salary,null)) from CTE where rnk = N + ); +END + + + +# Delete Duplicate Emails + +delete p1 from Person P1 cross join Person p2 where p1.email =p2.email and p1.id > p2.id; \ No newline at end of file From 9e20a9c122ba936bf8591f5ee518290085b6eae5 Mon Sep 17 00:00:00 2001 From: sagar1023 Date: Sun, 25 May 2025 18:02:09 -0400 Subject: [PATCH 2/3] HW2 - Sagar Rijhwani --- HW2.txt | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 HW2.txt diff --git a/HW2.txt b/HW2.txt new file mode 100644 index 0000000..de00b85 --- /dev/null +++ b/HW2.txt @@ -0,0 +1,31 @@ +# Problem Rank Scores + +# Write your MySQL query statement below +select score, DENSE_RANK() OVER(ORDER BY score desc) as 'rank' from Scores; + + + + + # Problem Exchange Seats + + Solution: +select( + case + when mod(id,2)!=0 and id = cnts then id + when mod(id,2)!=0 and id !=cnts then id+1 + else id-1 + end +) as 'id', student from seat, (select count(*) as cnts from Seat) as seat_counts order by id; + + + + + + +# Department top 3 Salaries + +with cte as ( + select d.name as 'Department', e.name as 'Employee', e.salary as 'Salary', DENSE_RANK() OVER (partition by e.departmentId ORDER BY e.salary desc) as 'salary_rank' from employee e left join department d on e.departmentId=d.id + ) + +select Department, Employee, Salary from cte where salary_rank<=3; \ No newline at end of file From 736ffc0bac4ed7ef7ad2131dd154e0a5d33d7e52 Mon Sep 17 00:00:00 2001 From: sagar1023 Date: Sun, 25 May 2025 18:15:07 -0400 Subject: [PATCH 3/3] Added Q3, HW2 Completed Sagar Rijhwani --- HW2.txt | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/HW2.txt b/HW2.txt index de00b85..8fb1d69 100644 --- a/HW2.txt +++ b/HW2.txt @@ -18,8 +18,9 @@ select( ) as 'id', student from seat, (select count(*) as cnts from Seat) as seat_counts order by id; +# Tree Problem - +select id, if(ISNULL(p_id),'Root',if(id in(select distinct p_id from Tree),'Inner','Leaf')) as 'Type' from Tree; # Department top 3 Salaries