当前位置:网站首页>Database query - what is the highest data?
Database query - what is the highest data?
2022-07-07 23:57:00 【Liu Chu, Ge Nian】
List of articles
Data sheet
Employee surface :
+-------------+------+
| Column Name | Type |
+-------------+------+
| id | int |
| salary | int |
+-------------+------+
id It's the primary key of this table .
Each row of the table contains the salary information of the employee .
subject 2: The second highest salary
Write a SQL Inquire about , Get and return Employee The second highest salary on the list . If there is no second highest salary , The query should return null .
The query result is shown in the following example .
Example 1:
Input :
Employee surface :
+----+--------+
| id | salary |
+----+--------+
| 1 | 100 |
| 2 | 200 |
| 3 | 300 |
+----+--------+
Output :
+---------------------+
| SecondHighestSalary |
+---------------------+
| 200 |
+---------------------+
Example 2:
Input :
Employee surface :
+----+--------+
| id | salary |
+----+--------+
| 1 | 100 |
+----+--------+
Output :
+---------------------+
| SecondHighestSalary |
+---------------------+
| null |
+---------------------+
answer
select ifNull(
(select distinct salary
from Employee
order by Salary Desc
limit 1,1),null
) as SecondHighestSalary;
Look at this :https://leetcode.cn/problems/second-highest-salary/solution/tu-jie-sqlmian-shi-ti-ru-he-cha-zhao-di-ngao-de-sh/
subject 3: The first N High salary
Write a SQL Query to report Employee No n High wages . If there is no n A maximum wage , The query should be reported as null .
The query result format is as follows .
Example 1:
Input :
Employee table:
+----+--------+
| id | salary |
+----+--------+
| 1 | 100 |
| 2 | 200 |
| 3 | 300 |
+----+--------+
n = 2
Output :
+------------------------+
| getNthHighestSalary(2) |
+------------------------+
| 200 |
+------------------------+
Example 2:
Input :
Employee surface :
+----+--------+
| id | salary |
+----+--------+
| 1 | 100 |
+----+--------+
n = 2
Output :
+------------------------+
| getNthHighestSalary(2) |
+------------------------+
| null |
+------------------------+
answer
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
边栏推荐
- 激光slam学习(2D/3D、偏实践)
- 关于组织2021-2022全国青少年电子信息智能创新大赛西南赛区(四川)复赛的通知
- 95. (cesium chapter) cesium dynamic monomer-3d building (building)
- How to put recyclerview in nestedscrollview- How to put RecyclerView inside NestedScrollView?
- Rock-paper-scissors
- Traduction gratuite en un clic de plus de 300 pages de documents PDF
- 如何衡量产品是否“刚需、高频、痛点”
- P5594 [xr-4] simulation match
- Chisel tutorial - 02 Chisel environment configuration and implementation and testing of the first chisel module
- Robomaster visual tutorial (11) summary
猜你喜欢
Problems faced when connecting to sqlserver after downloading (I)
面试题详解:用Redis实现分布式锁的血泪史
[programming problem] [scratch Level 2] March 2019 draw a square spiral
One click installation with fishros in blue bridge ROS
Using Google test in QT
数据湖(十五):Spark与Iceberg整合写操作
Pypharm uses, and the third-party library has errors due to version problems
一份假Offer如何盗走了「Axie infinity」5.4亿美元?
【编程题】【Scratch二级】2019.12 绘制十个正方形
How did a fake offer steal $540million from "axie infinity"?
随机推荐
如何衡量产品是否“刚需、高频、痛点”
AWS AWS help error
【史上最详细】信贷中逾期天数统计说明
Problems faced when connecting to sqlserver after downloading (I)
Magic fast power
Tools for debugging makefiles - tool for debugging makefiles
Is it safe to buy funds online?
2022.7.7-----leetcode.648
Robomaster visual tutorial (0) Introduction
archery安装测试
用语雀写文章了,功能真心强大!
正畸注意事项(持续更新中)
LinkedBlockingQueue源码分析-新增和删除
Chisel tutorial - 05 Sequential logic in chisel (including explicit multi clock, explicit synchronous reset and explicit asynchronous reset)
企业应用需求导向开发之人力部门,员工考勤记录和实发工资业务程序案例
limit 与offset的用法(转载)
Redis caching tool class, worth owning~
C语言学习
Archery installation test
Ping error: unknown name or service