SQL : Creating user defined Function for solving Nth Highest Salary problem

 SQL Leetcode Question No. 177Nth Highest Salary

Hello All,

We are back with another SQL problem to gain understanding about SQL. This blog-post focuses on SQL user defined function.

To know more about SQL user defined function, I highly recommend this blog-post.

Learn SQL: User-Defined Functions (sqlshack.com)

Give a try to solve below problem. Solution is provided as usual. Feel free to comment if any queries and also suggest alternate approach to solve this problem. Have a Happy Learning !  

Table: Employee

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

 

Write an SQL query to report the nth highest salary from the Employee table. If there is no nth highest salary, the query should report null.

The query result format is in the following example.

 

Example 1:

Input: 
Employee table:
+----+--------+
| id | salary |
+----+--------+
| 1  | 100    |
| 2  | 200    |
| 3  | 300    |
+----+--------+
n = 2
Output: 
+------------------------+
| getNthHighestSalary(2) |
+------------------------+
| 200                    |
+------------------------+

Example 2:

Input: 
Employee table:
+----+--------+
| id | salary |
+----+--------+
| 1  | 100    |
+----+--------+
n = 2
Output: 
+------------------------+
| getNthHighestSalary(2) |
+------------------------+
| null                   |
+------------------------+

Solution :

CREATE FUNCTION getNthHighestSalary(N INT) RETURNS INT   
BEGIN 
DECLARE M INT; 
SET M = N - 1;
    RETURN (  
        SELECT DISTINCT Salary FROM Employee             
        ORDER BY Salary DESC 
        LIMIT 1 Offset M
                     );
END

Explanation :

Consider a problem where we want to calculate 3rd highest salary. What logic we would be using ?
 
Simple approach will be arranging salary column from highest to lowest and picking 3rd item from that.
So Syntax will be:

SELECT DISTINCT Salary #selecting salary from salary column
FROM Employee                   #Employee as table name 
ORDER BY Salary DESC    #Arranging in Descending order
LIMIT 1 OFFSET 2              #Limit 1 tells to show only 1 result, and OFFSET tells to OFFSET first 2                                                            results and start from 3  

In our case we want Nth highest salary, so further:

SELECT DISTINCT Salary 
FROM Employee                
ORDER BY Salary DESC    
LIMIT 1 OFFSET M             

where M = N - 1, which is further denoted by 
  
DECLARE M INT; 
SET M = N - 1;            

in our final solution. Further function is build around it.

Happy Learning! 

Comments

Popular posts from this blog

SQL for beginners : finding out Employees Earning More Than Their Managers

Answer to SQL Leetcode Question 183. Customers Who Never Order

SQL Medium difficulty practice question : Consecutive Numbers