当前位置:网站首页>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
边栏推荐
- Les mots ont été écrits, la fonction est vraiment puissante!
- Ping error: unknown name or service
- C - linear table
- 正畸注意事项(持续更新中)
- [programming problem] [scratch Level 2] December 2019 flying birds
- @Detailed introduction of configuration annotation
- The result of innovation in professional courses such as robotics (Automation)
- limit 与offset的用法(转载)
- 【史上最详细】信贷中逾期天数统计说明
- C language learning
猜你喜欢
随机推荐
One click installation with fishros in blue bridge ROS
Download AWS toolkit pycharm
Visual Studio Deployment Project - Create shortcut to deployed executable
Robomaster visual tutorial (10) target prediction
Set up personal network disk with nextcloud
10 schemes to ensure interface data security
The result of innovation in professional courses such as robotics (Automation)
Robomaster visual tutorial (1) camera
limit 与offset的用法(转载)
Install sqlserver2019
[programming problem] [scratch Level 2] December 2019 flying birds
Archery installation test
P1308 [noip2011 popularity group] count the number of words
Go time package common functions
Go learning notes (1) environment installation and hello world
80%的人答错,苹果logo上的叶子到底朝左还是朝右?
May day d-light
Gorm Association summary
【推荐系统基础】正负样本采样和构造
DataGuard active / standby cleanup archive settings









