当前位置:网站首页>数据库查询——第几高的数据?
数据库查询——第几高的数据?
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
边栏推荐
- How did a fake offer steal $540million from "axie infinity"?
- c—线性表
- 一键免费翻译300多页的pdf文档
- Go time package common functions
- 507 field D - extraterrestrial relics
- Ping error: unknown name or service
- Chisel tutorial - 04 Control flow in chisel
- Laser slam learning (2d/3d, partial practice)
- Benchmarking Detection Transfer Learning with Vision Transformers(2021-11)
- Get started with mongodb
猜你喜欢

Chisel tutorial - 04 Control flow in chisel

Laser slam learning (2d/3d, partial practice)

archery安装测试

自动化测试:Robot FrameWork框架90%的人都想知道的实用技巧

Dataguard 主备清理归档设置

Pycharm basic settings latest version 2022

At the age of 35, I made a decision to face unemployment

DataGuard active / standby cleanup archive settings

Chisel tutorial - 02 Chisel environment configuration and implementation and testing of the first chisel module

Kubectl 好用的命令行工具:oh-my-zsh 技巧和窍门
随机推荐
HDU - 1260 Tickets(线性DP)
C language learning
[experiment sharing] log in to Cisco devices through the console port
gorm 关联关系小结
BSS 7230 flame retardant performance test of aviation interior materials
企业应用需求导向开发之人力部门,员工考勤记录和实发工资业务程序案例
Go time package common functions
Pigsty:开箱即用的数据库发行版
P1067 [noip2009 popularity group] polynomial output (difficult, pit)
第四期SFO销毁,Starfish OS如何对SFO价值赋能?
An example analysis of MP4 file format parsing
MySQL Architecture
limit 与offset的用法(转载)
Automated testing: robot framework is a practical skill that 90% of people want to know
MongoDB快速入门
神奇快速幂
二叉排序树【BST】——创建、查找、删除、输出
Redis caching tool class, worth owning~
Dependency injection 2 advantage lifecycle
平衡二叉樹【AVL樹】——插入、删除