当前位置:网站首页>数据库查询——第几高的数据?
数据库查询——第几高的数据?
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
边栏推荐
- LinkedBlockingQueue源码分析-新增和删除
- Introduction to programming hardware
- FFA与ICGA造影
- Install sqlserver2019
- Problems faced when connecting to sqlserver after downloading (I)
- Idea automatically generates serialVersionUID
- @Detailed introduction of configuration annotation
- 一份假Offer如何盗走了「Axie infinity」5.4亿美元?
- Database interview questions + analysis
- Take you hand in hand to build Eureka server with idea
猜你喜欢
Data Lake (XV): spark and iceberg integrate write operations
Rectification characteristics of fast recovery diode
一键免费翻译300多页的pdf文档
Installing gradle
【LeetCode】20、有效的括号
【推荐系统基础】正负样本采样和构造
One click installation with fishros in blue bridge ROS
一鍵免費翻譯300多頁的pdf文檔
Chisel tutorial - 04 Control flow in chisel
关于CH32库函数与STM32库函数的区别
随机推荐
平衡二叉樹【AVL樹】——插入、删除
Aitm3.0005 smoke toxicity test
Install sqlserver2019
Come on, brother
An example analysis of MP4 file format parsing
HB 5469民用飞机机舱内部非金属材料燃烧试验方法
One click installation with fishros in blue bridge ROS
PostGIS learning
Ora-01741 and ora-01704
Benchmarking Detection Transfer Learning with Vision Transformers(2021-11)
Go time package common functions
正畸注意事项(持续更新中)
gorm 关联关系小结
HDU - 1260 tickets (linear DP)
Ping error: unknown name or service
SQL 使用in关键字查询多个字段
go time包常用函数
ping报错:未知的名称或服务
自动化测试:Robot FrameWork框架90%的人都想知道的实用技巧
aws-aws help报错