diff --git a/Department Top Three Salaries b/Department Top Three Salaries new file mode 100644 index 0000000..abfdbde --- /dev/null +++ b/Department Top Three Salaries @@ -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 \ No newline at end of file diff --git a/Exhange Seats b/Exhange Seats new file mode 100644 index 0000000..5899a18 --- /dev/null +++ b/Exhange Seats @@ -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 \ No newline at end of file diff --git a/README.md b/README.md index c2318de..ba46f19 100644 --- a/README.md +++ b/README.md @@ -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 \ No newline at end of file diff --git a/Rank Scores b/Rank Scores new file mode 100644 index 0000000..75d0795 --- /dev/null +++ b/Rank Scores @@ -0,0 +1,4 @@ +SELECT score, + DENSE_RANK() OVER(ORDER BY score DESC) rank +FROM scores +ORDER BY rank \ No newline at end of file diff --git a/Tree Node b/Tree Node new file mode 100644 index 0000000..043af6b --- /dev/null +++ b/Tree Node @@ -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 \ No newline at end of file