diff --git a/sql2 done b/sql2 done new file mode 100644 index 0000000..48627ba --- /dev/null +++ b/sql2 done @@ -0,0 +1,51 @@ +Q1:Rank Scores +--Using window functions +select score, + dense_rank() over (order by score desc) as 'rnk' +from scores +order by rnk; + +--Without using window functions +select t.score, + (select count(distinct s.score) + from scores s + where s.score>=t.score) as 'rnk' +from scores t +order by rnk; + +Q2:Exchange Seats +SELECT + CASE + WHEN mod(id 2) !=0 AND id != cnts THEN id + 1 + WHEN mod(id 2) != 0 AND id = cnts THEN id + ELSE id - 1 + END + AS id, + student +FROM Seat, (select count(*) as cnts from seat) as Seat_Counts +ORDER BY id; + +Q3:TREE NODE +select distinct t1.id, + case + when t1.p_id is null then 'root' + when t2.id is null then 'leaf' + else 'inner' + end as type +from tree t1 +left join tree t2 +on t1.id=t2.p_id; + +Q4:DEPARTMENT TOP3 SALARIES +# Write your MySQL query statement below +SELECT Department, Employee, Salary +FROM ( + SELECT d.name AS Department, + e.name AS Employee, + e.salary, + DENSE_RANK() OVER (PARTITION BY e.departmentId ORDER BY e.salary DESC) AS dr + FROM Employee e + LEFT JOIN Department d ON e.departmentId = d.id +) ranked +WHERE dr <= 3 +ORDER BY Department, Salary DESC, Employee;