Skip to content

Commit 0e40152

Browse files
committed
task: #177
1 parent a001e82 commit 0e40152

File tree

2 files changed

+36
-0
lines changed

2 files changed

+36
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,7 @@ Useful for preparing for technical interviews and improving your SQL skills.
162162
- [3570. Find Books with No Available Copies](./leetcode/easy/3570.%20Find%20Books%20with%20No%20Available%20Copies.sql)
163163
2. [Medium](./leetcode/medium/)
164164
- [176. Second Highest Salary](./leetcode/medium/176.%20Second%20Highest%20Salary.sql)
165+
- [177. Nth Highest Salary](./leetcode/medium/177.%20Nth%20Highest%20Salary.sql)
165166
- [180. Consecutive Numbers](./leetcode/medium/180.%20Consecutive%20Numbers.sql)
166167
- [184. Department Highest Salary](./leetcode/medium/184.%20Department%20Highest%20Salary.sql)
167168
- [550. Game Play Analysis IV](./leetcode/medium/550.%20Game%20Play%20Analysis%20IV.sql)
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/*
2+
Question 177. Nth Highest Salary
3+
Link: https://leetcode.com/problems/nth-highest-salary/description/?envType=problem-list-v2&envId=database
4+
5+
Table: Employee
6+
7+
+-------------+------+
8+
| Column Name | Type |
9+
+-------------+------+
10+
| id | int |
11+
| salary | int |
12+
+-------------+------+
13+
id is the primary key (column with unique values) for this table.
14+
Each row of this table contains information about the salary of an employee.
15+
16+
17+
Write a solution to find the nth highest distinct salary from the Employee table. If there are less than n distinct salaries, return null.
18+
*/
19+
20+
CREATE OR REPLACE FUNCTION NTHHIGHESTSALARY(N INT) RETURNS TABLE (Salary INT) AS $$ --noqa: CP03 (function NthHighestSalary)
21+
BEGIN
22+
IF N <= 0 THEN
23+
Salary := NULL;
24+
RETURN NEXT;
25+
RETURN;
26+
END IF;
27+
28+
RETURN QUERY (
29+
SELECT DISTINCT e.salary
30+
FROM Employee e
31+
ORDER BY e.salary DESC
32+
LIMIT 1 OFFSET (N - 1)
33+
);
34+
END;
35+
$$ LANGUAGE plpgsql;

0 commit comments

Comments
 (0)