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
3 changes: 3 additions & 0 deletions 01-RankScores.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
select score, dense_rank()
over (order by score desc) as 'rank'
from scores
59 changes: 59 additions & 0 deletions 02-ExchangeSeat.sql
Original file line number Diff line number Diff line change
@@ -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
*/
46 changes: 46 additions & 0 deletions 03-TreeNode.sql
Original file line number Diff line number Diff line change
@@ -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
*/
9 changes: 9 additions & 0 deletions 04-DepartmentTop3Salaries.sql
Original file line number Diff line number Diff line change
@@ -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