当前位置:网站首页>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
边栏推荐
- 【编程题】【Scratch二级】2019.03 垃圾分类
- Database interview questions + analysis
- Codeworks 5 questions per day (average 1500) - day 8
- 自动化测试:Robot FrameWork框架90%的人都想知道的实用技巧
- Go time package common functions
- 神奇快速幂
- 数据湖(十五):Spark与Iceberg整合写操作
- Robomaster visual tutorial (10) target prediction
- 一份假Offer如何盗走了「Axie infinity」5.4亿美元?
- c—线性表
猜你喜欢
An example analysis of MP4 file format parsing
[programming problem] [scratch Level 2] December 2019 flying birds
c—线性表
光流传感器初步测试:GL9306
How did a fake offer steal $540million from "axie infinity"?
PostGIS learning
The result of innovation in professional courses such as robotics (Automation)
One click free translation of more than 300 pages of PDF documents
快速回复二极管整流特性
Introduction to programming hardware
随机推荐
One click free translation of more than 300 pages of PDF documents
Flash download setup
【编程题】【Scratch二级】2019.12 飞翔的小鸟
QT creator add custom new file / Project Template Wizard
Kubectl 好用的命令行工具:oh-my-zsh 技巧和窍门
Go learning notes (1) environment installation and hello world
Binary sort tree [BST] - create, find, delete, output
Jisuan Ke - t3104
Introduction knowledge system of Web front-end engineers
受限线性表
保证接口数据安全的10种方案
Visual Studio Deployment Project - Create shortcut to deployed executable
Chisel tutorial - 01 Introduction to Scala
Chisel tutorial - 04 Control flow in chisel
数据湖(十五):Spark与Iceberg整合写操作
Postgres timestamp to human eye time string or millisecond value
Opengl3.3 mouse picking up objects
Using Google test in QT
Use filters to count URL request time
Archery installation test