当前位置:网站首页>Leetcode MySQL database topic 177
Leetcode MySQL database topic 177
2022-06-29 09:58:00 【Water like ice】
177. Nth Highest Salary
1、 Questions and answers
Table:Employee
+----+--------+
| Id | Salary |
+----+--------+
| 1 | 100 |
| 2 | 200 |
| 3 | 300 |
+----+--------+
Write a SQL query to get the nth highest salary from the Employee table.
To write SQL from Employee Get the number in the table n The highest salary
answer :delimiter $$ create function getNthHighestSalary(N int) returns int begin declare n1 int; set n1=N-1; return ( select distinct Salary from Employee order by Salary desc limit n1,1 ); end $$
2、 Summary of knowledge points
- delimiter
- create function
- declare、set
1、 Why delimiter Because MySQL By default, a semicolon is used to end a command , When a command in the body of the function we define is written, it will end with a semicolon , and MySQL It will be mistaken that the function body has been defined , An error will be reported during operation . To do this, we need to define a new identifier to identify the end of a command .
2、
Create a new function :
Create function function_name(parameter_type)
returns return_type
begin
#Function_content
end
Related instructions :
Function name : Should be a legal identifier , And should not conflict with existing keywords . A function should belong to a database , have access to db_name.funciton_name Execute the database to which the current function belongs , Otherwise, it defaults to the current database .
parameter list : You can have one or more function parameters , Even without parameters . For each parameter , It consists of parameter name and parameter type .
Return value : Indicates the return value class type
The body of the function : The function body of a custom function consists of several available MySQL sentence , Process control , Variable declaration and other statements . It should be noted that the function body must contain return Return statement .
3、
mysql Stored procedure , There are two ways to define variables :
- Use set or select Direct assignment , The variable name is @ start .
for example :set @var=1;
You can declare... Anywhere in a conversation , The scope is the entire session , Called session variable . - With declare Keyword declared variables , Can only be used in stored procedures , Called a stored procedure variable ,
for example :
DECLARE var1 INT DEFAULT 0; Mainly used in the storage process , Or send parameters to storage .
Add
LeetCode Answers on :
select IFNULL((select distinct(Salary)
from Employee
order by Salary desc
limit 1,1),null) as SecondHighestSalary
边栏推荐
- gcc与makefile
- LiferayPortal JSONWS反序列化漏洞(CVE-2020-7961)分析
- CROSSFORMER: A VERSATILE VISION TRANSFORMER BASED ON CROSS-SCALE ATTENTION
- Wechat applet implements the data listener watch, including the watch that destroys the watch and sub attributes
- 2020-09-21 Visual Studio头文件和库目录配置
- Deep Learning-based Automated Delineation of Head and Neck Malignant Lesions from PET Images
- [Huawei certification] the most complete and selected question bank in hcia-datacom history (with answer analysis)
- Surveiller l'utilisation du pool de connexion des sources de données
- 【技术开发】酒精测试仪解决方案开发设计
- Data governance: data standard management (Part III)
猜你喜欢

FreeRTOS(九)——队列

【华为认证】HCIA-DATACOM史上最全精选题库(附答案解析)

linux环境下安装配置redis,并设置开机自启动

How to set Google Chrome as the default browser

Gross Tumor Volume Segmentation for Head and Neck Cancer Radiotherapy using Deep Dense Multi-modalit

UE4 material UV texture does not stretch with model scale

IPC(进程间通信)之管道详解

完美二叉树、完全二叉树、完满二叉树

Gross Tumor Volume Segmentation for Head and Neck Cancer Radiotherapy using Deep Dense Multi-modalit

2020-09-29 非商品模板化代码层次 rapidjson库
随机推荐
leetcode MYSQL数据库题目178
Is it safe to open an account for stock speculation? Is it reliable?
内网穿透工具frp使用入门
官方stm32芯片包下载地址 stm32f10x stm32f40x下载
分布式和集群分不清,我们讲讲两个厨子炒菜的故事
你知道BFD是什么吗?一文详解BFD协议原理及使用场景
Mysql5.7 installation tutorial in centos7 under Linux
XML布局中Button总是在最上层显示
UE4 blueprint modify get a copy in array to reference
2020-09-29 非商品模板化代码层次 rapidjson库
Data governance: the solution of data governance in the data Arena
Gross Tumor Volume Segmentation for Head and Neck Cancer Radiotherapy using Deep Dense Multi-modalit
Student增删gaih
Segmentation of Head and Neck Tumours Using Modified U-net
sublime text3设置运行自己的Makefile
Install and configure redis in the Linux environment, and set the boot auto start
自定义mvc框架实现
Student addition / deletion gaih
Introduction to intranet penetration tool FRP
Could not open JDBC connection for transaction