1. 程式人生 > >176. Second Highest Salary(Leetcode)

176. Second Highest Salary(Leetcode)

文章出處:http://www.cnblogs.com/grandyang/p/5348961.html

Write a SQL query to get the second highest salary from the Employee table.

+----+--------+
| Id | Salary |
+----+--------+
| 1  | 100    |
| 2  | 200    |
| 3  | 300    |
+----+--------+

For example, given the above Employee table, the second highest salary is 200

. If there is no second highest salary, then the query should return null.

這道題讓我們找表中某列第二大的數,這道題有很多種解法,先來看一種使用Limit和Offset兩個關鍵字的解法,MySQL中Limit後面的數字限制了我們返回資料的個數,Offset是偏移量,那麼如果我們想找第二高薪水,我們首先可以先對薪水進行降序排列,然後我們將Offset設為1,那麼就是從第二個開始,也就是第二高薪水,然後我們將Limit設為1,就是隻取出第二高薪水,如果將Limit設為2,那麼就將第二高和第三高薪水都取出來:

解法一:

SELECT Salary as SecondHighestSalary 
FROM Employee GROUP BY Salary
UNION ALL (SELECT NULL AS Salary)
ORDER BY Salary DESC LIMIT 1 OFFSET 1;

我們也可以使用Max函式來做,這個返回最大值,邏輯是我們取出的不包含最大值的數字中的最大值,即為第二大值:

解法二:

SELECT MAX(Salary) FROM Employee 
WHERE Salary NOT IN
(SELECT MAX(Salary) FROM Employee);

下面這種方法和上面基本一樣,就是用小於號<代替了Not in關鍵字,效果相同:

解法三:

SELECT MAX(Salary) FROM Employee
Where Salary <
(SELECT MAX(Salary) FROM Employee);

最後來看一種可以擴充套件到找到第N高的薪水的方法,只要將下面語句中的1改為N-1即可,第二高的薪水帶入N-1就是1,下面語句的邏輯是,假如我們要找第二高的薪水,那麼我們允許其中一個最大值存在,然後在其餘的數字中找出最大的,即為整個的第二大的值;

 Distinct是去重操作,一般放在開頭(緊跟select後)

解法四理解:要找到第二高的薪水,在表中就存在一個1個比它大的值,E1.Salary < E2.Salary 縮小了查詢範圍,將E1表第一高的薪水捨去,因為在表中沒有比他小的值。其實連MAX都不用加,因為最後得出的表中只有一個值(除非有重複值),但是leetcode要求不存在第二打返回null,只有聚合函式能返回null值,如果不加MAX,Salary不存在會返回空白(leetcode判定)

解法四:

SELECT MAX(Salary) FROM Employee E1
WHERE 1 =
(SELECT COUNT(DISTINCT(E2.Salary)) FROM Employee E2

WHERE E2.Salary > E1.Salary);

相關推薦

176. Second Highest SalaryLeetcode)

文章出處:http://www.cnblogs.com/grandyang/p/5348961.html Write a SQL query to get the second highest salary from the Employee table. +----+

Leetcode176. Second Highest Salary Easy)

1.題目Write a SQL query to get the second highest salary from the Employee table.+----+--------+ | Id | Salary | +----+--------+ | 1 | 100

sql leetcode 176. Second Highest Salary

Write a SQL query to get the second highest salary from the Employeetable. +----+--------+ | Id | Salary | +----+--------+ | 1 | 100 | | 2

LeetCode-Algorithms #002 Add Two Numbers, Database #176 Second Highest Salary

LeetCode-Algorithms #002 Add Two Numbers 給定兩個非空的以連結串列結構表示的非負整數, 這個結構長這樣: 1 public class ListNode { 2 int val; 3 ListNode next; 4 ListNode(int

leetcode 176. Second Highest Salary

Write a SQL query to get the second highest salary from the Employee table. +----+--------+ | Id | Salary | +----+--------+ | 1 | 100 | | 2 | 200

[理解leetcode解法]176. Second Highest Salary

176. Second Highest Salary #題目: Write a SQL query to get the second highest salary from theEmploy

176. Second Highest Salary(Easy)

tco targe query rip con -s second count rom Source of the question Write a SQL query to get the second highest salary from the Employee t

176. Second Highest Salary

max color class statement select mysql ble clas cond Write a SQL query to get the second highest salary from the Employee table. +----+-

176. Second Highest Salary SQL查詢語句中的 limit offset

題目: Write a SQL query to get the second highest salary from the Employee table Id | Salary ---|--- 1 | 100 2 | 200 3 | 300 For example, given the ab

176.Second Highest Salary

Write a SQL query to get the second highest salary from the Employee table. +----+--------+ | Id | Salary | +----+--------+ | 1 | 100

Second Highest Salary選擇第二高的工資)

要求: For example, given the above Employee table, the query should return 200 as the second highest

Leetcode176. Second Highest SalaryMYSQL limit,offset 區別)

原題 Write a SQL query to get the second highest salary from the Employee table. +----+--------+ | Id | Salary | +----+--------+

LeetCode-第二高的薪水(second-highest-salary)

第二高的薪水 難度 簡單 更多LeetCode答案歡迎大家關注Github: https://github.com/lxyer/LeetCodeAnswer 編寫一個 SQL 查詢,獲取 Employee 表中第二高的薪水(Salary) 。 +----+---

leetcode 2--Second Highest Salary

https://leetcode.com/problems/second-highest-salary/description/ Write a SQL query to get the second highest salary from the Employee table. +—

LeetCode Second Highest Salary

Problem Write a SQL query to get the second highest salary from the Employee table. +----+--------+ | Id | Salary | +----

[LeetCode] Second Highest Salary 第二高薪水

Write a SQL query to get the second highest salary from the Employee table. +----+--------+ | Id | Salary | +----+--------+ | 1 | 100 | | 2 | 2

LeetCodeSecond Highest Salary && Nth Highest Salary

Total Accepted: 1030 Total Submissions: 4309 My Submissions Question Solution Write a SQL query to get the second highest salary from th

LeetcodeSecond Highest Salary

題目: Write a SQL query to get the second highest salary from the Employee table. +—-+——–+ | Id |

找第二大的數SQL-Second Highest Salary

des rom tin .com begin get pre sql 最大 1: 找小於最大的最大的 select max(Salary) from Employee where Salary<(select MAX(Salary) from Employe

Second Highest Salary

Write a SQL query to get the second highest salary from the Employee table. +----+--------+ | Id | Salary | +----+--------+ | 1 | 100