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 diff --git a/HW2.txt b/HW2.txt new file mode 100644 index 0000000..8fb1d69 --- /dev/null +++ b/HW2.txt @@ -0,0 +1,32 @@ +# 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; + + +# 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 + +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