Can you solve this real interview question? Nth Highest Salary - Table: Employee +---+---+ | Column Name | Type | +---+---+ | id | int | | salary | int | +---+---+ id is the primary key (column wit
因此,对代码改动如下所示: CREATEFUNCTIONgetNthHighestSalary(NINT)RETURNSINTBEGINDECLAREPINTDEFAULTN-1; IF (P<0)THENRETURNNULL;ELSERETURN( # Write your MySQL query statement below.SELECTIFNULL( (SELECTDISTINCTSalaryFROMEmployeeORDERBYSalaryDESCLIMIT P,1) ,NULL)ASSecondHighestSalary );ENDIF;END PS: ...
Second Highest Salary 参考资料: https://leetcode.com/discuss/88875/simple-answer-with-limit-and-offset https://leetcode.com/discuss/63183/fastest-solution-without-using-order-declaring-variables LeetCode All in One 题目讲解汇总(持续更新中...) - 回复区间【1 - 1350】内任意数字推送对应的题目。
9 CREATEFUNCTIONgetNthHighestSalary(NINT)RETURNSINT BEGIN declareMint; setM=N-1; RETURN( # Write your MySQL query statement below. selectdistinctSalaryfromEmployeeorderbySalarydesclimit N-1, 1 ); END
Leetcode - create function in mysql - 177. Nth Highest Salary 以leetcode上这道题为例: Write a SQL query to get the nth highest salary from the Employee table. For example, given the above Employee table, the nth highest salary where n......
CREATE FUNCTION getNthHighestSalary(N INT) RETURNS INT BEGIN SET N = N - 1; RETURN ( SELECT DISTINCT Salary FROM Employee GROUP BY Salary ORDER BY Salary DESC LIMIT 1 OFFSET N ); END解法二:1 2 3 4 5 6 7 8 9 CREATE FUNCTION getNthHighestSalary(N INT) RETURNS INT ...
解法: 1.对每一个薪水A,只要大于等于A的不同薪水个数等于N即可。 因此子查询求出大于等于A的不同薪水个数B。当B=A时,能得出结果。 CREATEFUNCTIONgetNthHighestSalary(NINT)RETURNSINTBEGINRETURN(selectdistincte1.SalaryfromEmployee e1whereN=(selectcount(distincte2.Salary)fromEmployee e2wheree2.Salary>=e1...
题目链接:https://leetcode.com/problems/nth-highest-salary/description/ 题意:查询出表中工资第N高的值 思路: 1、先按照工资从高到低排序(注意去重) 1 selectdistinctSalaryfromEmployeeorderbySalarydesc 2、使用rownum给查询出的数据标注行号 1 selectrownum ro, s.Salaryfrom(selectdistinctSalaryfromEmployeeorde...
一、Nth Highest Salary Write a SQL query to get the nth highest salary from the Employee table. +---+---+ | Id | Salary | +---+---+ | 1 | 100 | | 2 | 200 | | 3 | 300 | +---+---+ For example, given the above Employee table, the nth highest salary where n = ...
CREATE FUNCTION getNthHighestSalary(N INT) RETURNS INT BEGIN DECLARE P INT DEFAULT N-1; IF (P<0) THEN RETURN NULL; ELSE RETURN ( # Write your MySQL query statement below. SELECT IFNULL( ( SELECT DISTINCT Salary FROM Employee ORDER BY Salary DESC ...