[SQL] LeetCode 177. Nth Highest Salary



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.


①関数生成DDLコマンド
  • CREATE文のみ使用中に関数を再コンパイル中にエラーが発生したため、REPLACE構文を使用します.
  • ②関数名の定義
    ③関数パリ表宣言
  • パラメータ1、パラメータ2、パラメータ3...宣言
  • ④戻りタイプの指定
  • VARCHAR 2、NUMBER、DATEなど、返すデータ型を指定します.
  • ⑤関数で使用する変数を宣言する
    ⑤関数結果を返す
    create function getNthHighestSalary(N IN NUMBER) 
       	return NUMBER
    is
    	result NUMBER;
    BEGIN
       
    	select salary into result
    	from (select rownum,salary 
    	from (select distinct salary 
    	from employee order by salary desc))
    	where rownum = N;
        
        RETURN result;
        
    END;