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
13 changes: 13 additions & 0 deletions Department Top Three Salaries
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
WITH top_sal AS (
SELECT DISTINCT departmentid,
(CASE WHEN DENSE_RANK() OVER(PARTITION BY departmentid ORDER BY salary DESC) <= 3 THEN salary END) top_3_sal
FROM employee
)
SELECT DISTINCT d.name AS department,
e.name AS employee,
e.salary
FROM employee e
LEFT JOIN department d
ON e.departmentid = d.id
INNER JOIN top_sal t
ON e.departmentid = t.departmentid AND e.salary = t.top_3_sal AND t.top_3_sal IS NOT NULL
7 changes: 7 additions & 0 deletions Exhange Seats
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
SELECT id,
(CASE
WHEN id%2 = 0 THEN COALESCE(LAG(student,1) OVER(ORDER BY id), student)
WHEN id%2 = 1 THEN COALESCE(LEAD(student,1) OVER(ORDER BY id), student)
END) AS student
FROM seat
ORDER BY id
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,5 @@ Problem 2 : Exchange Seats (https://leetcode.com/problems/exchange-seats/ )
Problem 3 : Tree Node (https://leetcode.com/problems/tree-node/ )

Problem 4 : Deparment Top 3 Salaries (https://leetcode.com/problems/department-top-three-salaries/ )

Solutions provided in 4 different SQL files in repository
4 changes: 4 additions & 0 deletions Rank Scores
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
SELECT score,
DENSE_RANK() OVER(ORDER BY score DESC) rank
FROM scores
ORDER BY rank
7 changes: 7 additions & 0 deletions Tree Node
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
SELECT id,
(CASE
WHEN p_id IS NULL THEN 'Root'
WHEN id IN (SELECT DISTINCT p_id FROM tree WHERE p_id IS NOT NULL) THEN 'Inner'
WHEN id NOT IN (SELECT DISTINCT p_id FROM tree WHERE p_id IS NOT NULL) THEN 'Leaf'
END) AS type
FROM tree