From 34f4478abda0c001f706f48a7afa840c733fd0e2 Mon Sep 17 00:00:00 2001 From: Feminto Date: Sat, 24 May 2025 12:04:35 -0700 Subject: [PATCH 1/2] Adding 4 SQL files for 4 problems mentioned in the README file --- Department Top Three Salaries | 13 +++++++++++++ Exhange Seats | 7 +++++++ Rank Scores | 4 ++++ Tree Node | 7 +++++++ 4 files changed, 31 insertions(+) create mode 100644 Department Top Three Salaries create mode 100644 Exhange Seats create mode 100644 Rank Scores create mode 100644 Tree Node 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/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 From 74b2deac6e9436323ff8dffbd40c0ddcecb8aa5c Mon Sep 17 00:00:00 2001 From: Feminto Date: Sat, 24 May 2025 12:08:24 -0700 Subject: [PATCH 2/2] Adding info in README file --- README.md | 2 ++ 1 file changed, 2 insertions(+) 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