当前位置:网站首页>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
边栏推荐
- 快速回复二极管整流特性
- 每日刷题记录 (十六)
- Archery installation test
- Uic564-2 Appendix 4 - flame retardant fire test: flame diffusion
- 2022.7.7-----leetcode. six hundred and forty-eight
- [programming problem] [scratch Level 2] March 2019 draw a square spiral
- @Configuration注解的详细介绍
- 机器人(自动化)等专业课程创新的结果
- 一鍵免費翻譯300多頁的pdf文檔
- go time包常用函数
猜你喜欢
Introduction to programming hardware
Preliminary test of optical flow sensor: gl9306
10 schemes to ensure interface data security
第四期SFO销毁,Starfish OS如何对SFO价值赋能?
Basic learning of SQL Server -- creating databases and tables with code
Binary sort tree [BST] - create, find, delete, output
Chisel tutorial - 02 Chisel environment configuration and implementation and testing of the first chisel module
一鍵免費翻譯300多頁的pdf文檔
自动化测试:Robot FrameWork框架90%的人都想知道的实用技巧
ROS from entry to mastery (IX) initial experience of visual simulation: turtlebot3
随机推荐
How to put recyclerview in nestedscrollview- How to put RecyclerView inside NestedScrollView?
网上买基金安全么?
【编程题】【Scratch二级】2019.09 制作蝙蝠冲关游戏
Benchmarking Detection Transfer Learning with Vision Transformers(2021-11)
Archery installation test
第四期SFO销毁,Starfish OS如何对SFO价值赋能?
Resolve the URL of token
Solutions to problems in sqlserver deleting data in tables
80%的人答错,苹果logo上的叶子到底朝左还是朝右?
The result of innovation in professional courses such as robotics (Automation)
Codeworks 5 questions per day (average 1500) - day 8
Introduction knowledge system of Web front-end engineers
95.(cesium篇)cesium动态单体化-3D建筑物(楼栋)
Connect diodes in series to improve voltage withstand
【编程题】【Scratch二级】2019.12 绘制十个正方形
Is it safe for tongdaxin to buy funds?
DataGuard active / standby cleanup archive settings
HDU - 1260 Tickets(线性DP)
Chisel tutorial - 00 Ex.scala metals plug-in (vs Code), SBT and coursier exchange endogenous
Restricted linear table