当前位置:网站首页>数据库查询——第几高的数据?
数据库查询——第几高的数据?
2022-07-07 21:54:00 【流楚丶格念】
数据表
Employee 表:
+-------------+------+
| Column Name | Type |
+-------------+------+
| id | int |
| salary | int |
+-------------+------+
id 是这个表的主键。
表的每一行包含员工的工资信息。
题目2:第二高的薪水
编写一个 SQL 查询,获取并返回 Employee 表中第二高的薪水 。如果不存在第二高的薪水,查询应该返回 null 。
查询结果如下例所示。
示例 1:
输入:
Employee 表:
+----+--------+
| id | salary |
+----+--------+
| 1 | 100 |
| 2 | 200 |
| 3 | 300 |
+----+--------+
输出:
+---------------------+
| SecondHighestSalary |
+---------------------+
| 200 |
+---------------------+
示例 2:
输入:
Employee 表:
+----+--------+
| id | salary |
+----+--------+
| 1 | 100 |
+----+--------+
输出:
+---------------------+
| SecondHighestSalary |
+---------------------+
| null |
+---------------------+
解答
select ifNull(
(select distinct salary
from Employee
order by Salary Desc
limit 1,1),null
) as SecondHighestSalary;
题目3:第N高的薪水
编写一个SQL查询来报告 Employee 表中第 n 高的工资。如果没有第 n 个最高工资,查询应该报告为 null 。
查询结果格式如下所示。
示例 1:
输入:
Employee table:
+----+--------+
| id | salary |
+----+--------+
| 1 | 100 |
| 2 | 200 |
| 3 | 300 |
+----+--------+
n = 2
输出:
+------------------------+
| getNthHighestSalary(2) |
+------------------------+
| 200 |
+------------------------+
示例 2:
输入:
Employee 表:
+----+--------+
| id | salary |
+----+--------+
| 1 | 100 |
+----+--------+
n = 2
输出:
+------------------------+
| getNthHighestSalary(2) |
+------------------------+
| null |
+------------------------+
解答
CREATE FUNCTION getNthHighestSalary(N INT) RETURNS INT
BEGIN
declare m int;
SET m = N-1;
RETURN (
# Write your MySQL query statement below.
select ifNull(
(
select distinct salary
from Employee
order by salary desc
limit 1 offset m
),null)
);
END
边栏推荐
猜你喜欢
BSS 7230 flame retardant performance test of aviation interior materials
蓝桥ROS中使用fishros一键安装
Seven years' experience of a test engineer -- to you who walk alone all the way (don't give up)
Aitm3.0005 smoke toxicity test
MongoDB快速入门
一鍵免費翻譯300多頁的pdf文檔
Traduction gratuite en un clic de plus de 300 pages de documents PDF
c—线性表
用语雀写文章了,功能真心强大!
Arbre binaire équilibré [Arbre AVL] - Insérer et supprimer
随机推荐
35岁那年,我做了一个面临失业的决定
One click installation with fishros in blue bridge ROS
数据分析系列 之3σ规则/依据拉依达准则来剔除异常值
Flash download setup
C - minute number V3
LinkedBlockingQueue源码分析-新增和删除
mysql8.0 ubuntu20.4
Introduction to programming hardware
【LeetCode】20、有效的括号
webflux - webclient Connect reset by peer Error
AITM3.0005 烟雾毒性测试
Pigsty: out of the box database distribution
UIC564-2 附录4 –阻燃防火测试:火焰的扩散
通达信买基金安全吗?
Pycharm essential plug-in, change the background (self use, continuous update) | CSDN creation punch in
One click free translation of more than 300 pages of PDF documents
P2141 [noip2014 popularization group] abacus mental arithmetic test
Pypharm uses, and the third-party library has errors due to version problems
codeforces每日5题(均1500)-第八天
c—线性表