当前位置:网站首页>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
边栏推荐
- 【推荐系统基础】正负样本采样和构造
- Redis caching tool class, worth owning~
- @Detailed introduction of configuration annotation
- Go learning notes (2) basic types and statements (1)
- SQL connection problem after downloading (2)
- 关于CH32库函数与STM32库函数的区别
- Teach you to make a custom form label by hand
- Uic564-2 Appendix 4 - flame retardant fire test: flame diffusion
- [programming problem] [scratch Level 2] December 2019 flying birds
- Codeworks 5 questions per day (average 1500) - day 8
猜你喜欢
Magic fast power
保证接口数据安全的10种方案
激光slam学习(2D/3D、偏实践)
Ping error: unknown name or service
快速回复二极管整流特性
Using Google test in QT
ROS from entry to mastery (IX) initial experience of visual simulation: turtlebot3
[question de programmation] [scratch niveau 2] oiseaux volants en décembre 2019
SQL connection problem after downloading (2)
Anaconda+pycharm+pyqt5 configuration problem: pyuic5 cannot be found exe
随机推荐
一鍵免費翻譯300多頁的pdf文檔
DataGuard active / standby cleanup archive settings
如何衡量产品是否“刚需、高频、痛点”
SQL connection problem after downloading (2)
2022.7.7-----leetcode.648
Gorm Association summary
光流传感器初步测试:GL9306
80%的人答错,苹果logo上的叶子到底朝左还是朝右?
Dataguard 主备清理归档设置
Magic fast power
Flash download setup
limit 与offset的用法(转载)
BSS 7230 航空内饰材料阻燃性能测试
蓝桥ROS中使用fishros一键安装
Opengl3.3 mouse picking up objects
2022.7.7-----leetcode. six hundred and forty-eight
Chisel tutorial - 05 Sequential logic in chisel (including explicit multi clock, explicit synchronous reset and explicit asynchronous reset)
HDU - 1260 Tickets(线性DP)
Introduction to programming hardware
数据分析系列 之3σ规则/依据拉依达准则来剔除异常值