Leetcode 177 第N高的薪水

2021-09-12 02:30:26 字數 1773 閱讀 1934

詳細見:leetcode 題目總結 - sql

編寫乙個 sql 查詢,獲取 employee 表中第 n 高的薪水(salary)。

+----+--------+

| id | salary |

+----+--------+

| 1 | 100 |

| 2 | 200 |

| 3 | 300 |

+----+--------+

例如上述 employee 表,n = 2時,應返回第二高的薪水 200。如果不存在第 n 高的薪水,那麼查詢應返回 null。

+------------------------+

| getnthhighestsalary(2) |

+------------------------+

| 200 |

+------------------------+

create

table

ifnot

exists employee (idint, salary int);

truncate

table employee;

insert

into employee (id, salary)

values

('1'

,'100');

insert

into employee (id, salary)

values

('2'

,'200');

insert

into employee (id, salary)

values

('3'

,'300'

);

查詢條件:

create

function getnthhighestsalary(n int

)returns

intbegin

set n = n-1;

return

(# write your mysql query statement below.

select

(select

distinct salary

from employee

order

by salary desc

limit

1offset n

));end

注意:limit子句後面不能做運算。

或者可以新定義乙個變數x:

create

function getnthhighestsalary(n int

)returns

intbegin

declare x int

;set x = n-1;

return

(# write your mysql query statement below.

select

(select

distinct salary

from employee

order

by salary desc

limit

1offset x

));end

詳細見:leetcode 題目總結 - sql

LeetCode 177 第N高的薪水

題目 編寫乙個 sql 查詢,獲取 employee 表中第 n 高的薪水 salary 例如 employee 表,n 2 時,應返回第二高的薪水 200。如果不存在第 n 高的薪水,那麼查詢應返回 null。表 employee id salary 1 100 2 200 3 300 返回如下結...

177 第N高的薪水

第n高的薪水 編寫乙個 sql 查詢,獲取 employee 表中第 n 高的薪水 salary id salary 1 100 2 200 3 300 例如上述 employee 表,n 2 時,應返回第二高的薪水 200。如果不存在第 n 高的薪水,那麼查詢應返回 null。getnthhigh...

177 第N高的薪水

編寫乙個 sql 查詢,獲取 employee 表中第 n 高的薪水 salary id salary 1 100 2 200 3 300 例如上述 employee 表,n 2 時,應返回第二高的薪水 200。如果不存在第 n 高的薪水,那麼查詢應返回 null。getnthhighestsala...