Skip to content

task: #177 #81

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 22, 2025
Merged
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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@ Useful for preparing for technical interviews and improving your SQL skills.
- [3570. Find Books with No Available Copies](./leetcode/easy/3570.%20Find%20Books%20with%20No%20Available%20Copies.sql)
2. [Medium](./leetcode/medium/)
- [176. Second Highest Salary](./leetcode/medium/176.%20Second%20Highest%20Salary.sql)
- [177. Nth Highest Salary](./leetcode/medium/177.%20Nth%20Highest%20Salary.sql)
- [180. Consecutive Numbers](./leetcode/medium/180.%20Consecutive%20Numbers.sql)
- [184. Department Highest Salary](./leetcode/medium/184.%20Department%20Highest%20Salary.sql)
- [550. Game Play Analysis IV](./leetcode/medium/550.%20Game%20Play%20Analysis%20IV.sql)
Expand Down
35 changes: 35 additions & 0 deletions leetcode/medium/177. Nth Highest Salary.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
Question 177. Nth Highest Salary
Link: https://leetcode.com/problems/nth-highest-salary/description/?envType=problem-list-v2&envId=database

Table: Employee

+-------------+------+
| Column Name | Type |
+-------------+------+
| id | int |
| salary | int |
+-------------+------+
id is the primary key (column with unique values) for this table.
Each row of this table contains information about the salary of an employee.


Write a solution to find the nth highest distinct salary from the Employee table. If there are less than n distinct salaries, return null.
*/

CREATE OR REPLACE FUNCTION NTHHIGHESTSALARY(N INT) RETURNS TABLE (Salary INT) AS $$ --noqa: CP03 (function NthHighestSalary)
BEGIN
IF N <= 0 THEN
Salary := NULL;
RETURN NEXT;
RETURN;
END IF;

RETURN QUERY (
SELECT DISTINCT e.salary
FROM Employee e
ORDER BY e.salary DESC
LIMIT 1 OFFSET (N - 1)
);
END;
$$ LANGUAGE plpgsql;
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy