編寫乙個 sql 查詢,獲取 employee 表中第 n 高的薪水(salary)。
+----+--------+
| id | salary |
+----+--------+
| 1 | 100 |
| 2 | 200 |
| 3 | 300 |
+----+--------+
例如上述 employee 表,n = 2 時,應返回第二高的薪水 200。如果不存在第 n 高的薪水,那麼查詢應返回 null。
+------------------------+
| getnthhighestsalary(2) |
+------------------------+
| 200 |
+------------------------+
第一次寫sql函式,對關鍵字使用不太熟練。
create function getnthhighestsalary(n int) returns int
begin
declare p1 int;
declare p2 int;
if(n<1)
then set p1=0,p2=0;
else set p1=n-1,p2=1;
end if;
return (
# write your mysql query statement below.
select ifnull(
(select distinct salary from employee
order by salary desc
limit p1 ,p2 ),null
) as secondhighestsalary
);end
177 第N高的薪水
第n高的薪水 編寫乙個 sql 查詢,獲取 employee 表中第 n 高的薪水 salary id salary 1 100 2 200 3 300 例如上述 employee 表,n 2 時,應返回第二高的薪水 200。如果不存在第 n 高的薪水,那麼查詢應返回 null。getnthhigh...
Leetcode 177 第N高的薪水
詳細見 leetcode 題目總結 sql 編寫乙個 sql 查詢,獲取 employee 表中第 n 高的薪水 salary id salary 1 100 2 200 3 300 例如上述 employee 表,n 2時,應返回第二高的薪水 200。如果不存在第 n 高的薪水,那麼查詢應返回 n...
(SQL)177 第N高的薪水
編寫乙個 sql 查詢,獲取 employee 表中第 n 高的薪水 salary id salary 1 100 2 200 3 300 例如上述 employee 表,n 2 時,應返回第二高的薪水 200。如果不存在第 n 高的薪水,那麼查詢應返回 null。getnthhighestsala...