From 6647e07346d653197af670b3e1dfaf102d41706e Mon Sep 17 00:00:00 2001 From: akanksha-vishwak Date: Sat, 31 May 2025 04:09:43 -0400 Subject: [PATCH] completed sql2 problems --- 01-RankScores.sql | 3 ++ 02-ExchangeSeat.sql | 59 +++++++++++++++++++++++++++++++++++ 03-TreeNode.sql | 46 +++++++++++++++++++++++++++ 04-DepartmentTop3Salaries.sql | 9 ++++++ 4 files changed, 117 insertions(+) create mode 100644 01-RankScores.sql create mode 100644 02-ExchangeSeat.sql create mode 100644 03-TreeNode.sql create mode 100644 04-DepartmentTop3Salaries.sql diff --git a/01-RankScores.sql b/01-RankScores.sql new file mode 100644 index 0000000..2a031af --- /dev/null +++ b/01-RankScores.sql @@ -0,0 +1,3 @@ +select score, dense_rank() +over (order by score desc) as 'rank' +from scores \ No newline at end of file diff --git a/02-ExchangeSeat.sql b/02-ExchangeSeat.sql new file mode 100644 index 0000000..5ce451d --- /dev/null +++ b/02-ExchangeSeat.sql @@ -0,0 +1,59 @@ +-- Problem 2 : Exchange Seats (https://leetcode.com/problems/exchange-seats/ ) + +-- solution from the class +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 + +-- I found the above counter intuitive, so I tried this: +with seat_counts as (select count(*) as cnts from seat) +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, seat_counts +order by id + +-- i also thought of the following but realised it will be very slow +-- because the it will run the subquery select max(id) multiple times + +SELECT ( + CASE + WHEN MOD(id, 2) != 0 AND id = (SELECT MAX(id) FROM seat) THEN id + WHEN MOD(id, 2) != 0 AND id != (SELECT MAX(id) FROM seat) THEN id + 1 + ELSE id - 1 + END +) AS 'id', student +FROM seat +ORDER BY id; + +-- solution using XOR + +select s1.id, ifnull(s2.student, s1.student) as student +from seat s1 left join seat s2 on (s1.id+1)^1 - 1 = s2.id + + +/* + +- idea: expression is (x+1)^1 - 1 which return one less when even and one more when odd (check notes) +check id, say 1 -> expression returns 2 -> go to copy, search name belonging to this value and put it with 1 +create two copies of the table and make join +- always be careful when updating the same table that the previous value is not overwritten, therfore create copy of the table + +-- coalesce can be used instead of ifnull, it can handle multiple null cases one by one +-- i think order by is not needed here because of how id is provided (increment continuously) + +select (id+1)^1 - 1 as id, student +from seat +order by id + +only problem is the case with odd number of rows +*/ \ No newline at end of file diff --git a/03-TreeNode.sql b/03-TreeNode.sql new file mode 100644 index 0000000..94ece29 --- /dev/null +++ b/03-TreeNode.sql @@ -0,0 +1,46 @@ +-- Problem 3 : Tree Node (https://leetcode.com/problems/tree-node/) + +/* +solved before class, thought process: +- when pid is null then root +- when both in id and pid then inner +- if only in id then leaf +*/ +select id, ( + case + when p_id is null then 'Root' + when id not in (select distinct p_id from tree where p_id is not null) then 'Leaf' + else 'Inner' + end +) as 'type' +from tree + +-- from class +select id, 'Root' as type from tree where p_id is null +union +select id, 'Leaf' as type from tree where id not in + (select distinct p_id from tree where p_id is not null) and p_id is not null +union +select id, 'Inner' as type from tree where id in + (select distinct p_id from tree) and p_id is not null + +-- case was also covered in class and same solution + +-- with nested if +select id, +if ( isnull(p_id), 'Root', + if(id in (select distinct p_id from tree), 'Inner', 'Leaf' + ) +) as type from tree + +/* +- if (expression, true, false) +- if (expression1, true, if (expression2, true, false) ) + +- short-circuiting: in both case and nested if the condition +is satifies in 1st case it will bot be checked in the next one +but in union it is not the case so we have to write all the conditions again (p_id is not null) + +- union will return single entry however union all will return all the values with duplicated value +as well +*/ \ No newline at end of file diff --git a/04-DepartmentTop3Salaries.sql b/04-DepartmentTop3Salaries.sql new file mode 100644 index 0000000..be06b5f --- /dev/null +++ b/04-DepartmentTop3Salaries.sql @@ -0,0 +1,9 @@ +-- Problem 4 : Deparment Top 3 Salaries (https://leetcode.com/problems/department-top-three-salaries/) + +with cte as (select d.name as Department, e.name as Employee, salary, +dense_rank() over (partition by d.name order by salary desc) as rnk +from employee e +left join department d +on e.departmentId = d.id) +select Department, Employee, salary +from cte where rnk <=3 \ No newline at end of file