当前位置:网站首页>SQL练习 2022/7/1
SQL练习 2022/7/1
2022-08-04 05:28:00 【Provence°_博】
SQL练习 2022/7/1
176. 第二高的薪水
题干
代码思路
- 使用子查询和 LIMIT 子句
select
(
select distinct salary
from employee
order by salary desc
limit 1 offset 1
) as SecondHighestSalary
- 使用 IFNULL 和 LIMIT 子句
SELECT
IFNULL(
(SELECT DISTINCT Salary
FROM Employee
ORDER BY Salary DESC
LIMIT 1 OFFSET 1),
NULL) AS SecondHighestSalary
- 使用max函数
比第一大的就是第二大
select max(salary) as SecondHighestSalary
from employee
where salary<(select max(salary) from employee)
177. 第N高的薪水
题干
代码思路
- 使用limit函数
CREATE FUNCTION getNthHighestSalary(N INT) RETURNS INT
BEGIN
SET N := N-1;
RETURN (
# Write your MySQL query statement below.
select
(
select distinct salary
from employee
order by salary desc
limit 1 offset N
) as SecondHighestSalary
);
END
- 使用窗口函数
CREATE FUNCTION getNthHighestSalary(N INT) RETURNS INT
BEGIN
RETURN (
# Write your MySQL query statement below.
SELECT
DISTINCT salary
FROM
(SELECT
salary, dense_rank() over(ORDER BY salary DESC) AS rnk
FROM
employee) tmp
WHERE rnk = N
);
END
- 其他
力扣官方题解