Skip to content

Commit 59ca724

Browse files
committed
updating queries with CTE
1 parent 0fe6590 commit 59ca724

File tree

2 files changed

+13
-0
lines changed

2 files changed

+13
-0
lines changed

leetcode/medium/p0176second_highest_salary.sql

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,3 +114,7 @@ https://www.hackerrank.com/challenges/the-report/problem
114114
/* external reading about dense_rank():
115115
http://www.sql-tutorial.ru/en/book_rank_dense_rank_functions.html
116116
*/
117+
118+
-- using common table expression
119+
with salary_rankings as (select salary , dense_rank() over (order by salary desc) as ranking from employee)
120+
select min(salary) as SecondHighestSalary from salary_rankings where ranking=2;

leetcode/medium/p0177nth_highest_salary.sql

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,3 +68,12 @@ BEGIN
6868
);
6969
END
7070

71+
-- using common table expression
72+
CREATE FUNCTION getNthHighestSalary(N INT) RETURNS INT
73+
BEGIN
74+
RETURN (
75+
with salary_rankings as (select salary , dense_rank() over (order by salary desc) as ranking from employee)
76+
select min(salary) from salary_rankings where ranking=N
77+
78+
);
79+
END

0 commit comments

Comments
 (0)