找第二大的数SQL-Second Highest Salary

1: 找小于最大的最大的

select max(Salary) from Employee where Salary<(select MAX(Salary) from Employee);

2. 排序

select  Salary from Employee where Salary not in (select MAX(Salary)from Employee) order by Salary desc limit 1;

  

select (

select distinct Salary from Employee order by Salary Desc limit 1 offset 1

) as SecondHeighestSalary;

  

找第n个数:

CREATE FUNCTION getNthHighestSalary(N INT) RETURNS INT
BEGIN
DECLARE M INT;
set N=N-1;
  RETURN (

      # Write your MySQL query statement below.
      select  Salary from Employee order by Salary desc limit 1 offset N
  );
END

  

CREATE FUNCTION getNthHighestSalary(N INT) RETURNS INT
BEGIN
DECLARE M INT;
set N=N-1;
  RETURN (

      # Write your MySQL query statement below.
      select  distinct Salary from Employee order by Salary desc limit 1 offset N
  );
END

  不能在limit 里N-1, 因为limit里不计算

哈哈: distinct :在表中可能包含重复值,返回唯一不同的值,

时间: 2024-08-26 03:28:11

找第二大的数SQL-Second Highest Salary的相关文章

面试算法题:16个数,最多用20次比较,找出第二大的数?

这道题最笨的方法就是先从16个数中选出最大的数,然后再从剩下的15个数中选出最大数就可得到答案,因此,需要15+14=29次比较. 既然这道题要求在20次比较之内就能找出第二大的数,那我们就想能简单的方法. 假设16个数中最大的是A,第二大的是B. 首先将16个数两两进行比较,较大者胜出,然后再在胜出者中进行两两比较,按此方法,最后选出最大数A,如下所示 红色路线是最大数经过的路径. 接下来分析B可能存在的位置. 将16个数分为左部分和右部分各8位: 1.如果A和B在两个不同的部分(A在左部分,

如何找出数组中第二大的数

1.最容易想到的办法 我们可以用最简单的办法来找到一个数组中任意大小的数字,那就是按照某一个排序方式将数组的所有元素进行排序,然后按需取出来就可以,知识这种方式的时间复杂度和空间复杂度比较大,所以,有了下面这种方式 2.通过设置两个变量来进行判断 这种方式可以只通过一遍扫描数组即可找到第二大数,具体的形式如下:先定义两个变量:一个变量用来存储数组的最大数,初始值为数组首元素,另一个变量用来存储第二大的数,初始值为最小负整数,然后遍历数组元素,如果数组元素的值比最大数变量还大,更新最大数:若数组元

求出整形数组中第二大的数的值

1 int findsecond(int a[], int size) 2 { 3 int max = a[0]; 4 int second = a[1]; 5 int index = 0; 6 for (int i = 0; i < size; ++i) 7 { 8 if (max < a[i]) 9 { 10 second = max; 11 index = i; 12 max = a[i]; 13 } 14 else if (max > a[i]) 15 { 16 if (seco

求数组第二大的数(选择排序)

定义一个最大 和第二大的数 每次循环都判断数组中是否有比最大的数大的有则交换两者的值同时 把原来最大数的值赋值给第二大的 public class SecondMax { public static int FindSecMax(int[] data) { int count = data.length; int maxnumber = data[0]; int sec_max = Integer.MIN_VALUE; for(int i = 1;i<count;i++) { if(data[i

[LeetCode][SQL]Nth Highest Salary

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 highe

[LeetCode][SQL]Second Highest Salary

https://leetcode.com/problems/second-highest-salary/ Second Highest Salary Write a SQL query to get the second highest salary from the Employee table. +----+--------+ | Id | Salary | +----+--------+ | 1 | 100 | | 2 | 200 | | 3 | 300 | +----+--------+

python找出数组中第二大的数

#!usr/bin/env python #encoding:utf-8 ''''' __Author__:沂水寒城 功能:找出数组中第2大的数字 ''' def find_Second_large_num(num_list):   '''''   找出数组中第2大的数字   '''   #直接排序,输出倒数第二个数即可   tmp_list=sorted(num_list)   print 'Second_large_num is:', tmp_list[-2]   #设置两个标志位一个存储最

sql求倒数第二大的数,效率不高,但写法新颖

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ConsoleApplication9 { class Program { static void Main(string[] args) { // sql如下,我把它翻译成代码,加深理解 // SELECT MAX(id)FROM sys_men

如何使用一次for循环得到数组中第二大的数

装载声明:http://blog.csdn.net/lxsmk9059/article/details/77920206?locationNum=1&fps=1 1 int array[] = {1,12,58,369,45,17,59,3654,370}; 2 int max = array[0]; 3 int secondmax = array[0]; 4 5 for(int i = 0; i < sizeof(array)/sizeof(int); i++) 6 { 7 if(arra