Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions HW1.txt
Original file line number Diff line number Diff line change
@@ -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;
32 changes: 32 additions & 0 deletions HW2.txt
Original file line number Diff line number Diff line change
@@ -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;