Skip to content

Commit

Permalink
Update and rename 0176. Second Highest Salary.sql to 0176.(Easy) Seco…
Browse files Browse the repository at this point in the history
…nd Highest Salary.sql
  • Loading branch information
haibarawu authored Jun 2, 2020
1 parent 430dcbb commit d263375
Showing 1 changed file with 16 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
/*
0176. Second Highest Salary
/****************************************************************************************************
176. Second Highest Salary
176. 第二高的薪水
Difficulty: Easy
Write a SQL query to get the second highest salary from the Employee table.
编写一个 SQL 查询,获取 Employee 表中第二高的薪水(Salary) 。
+----+--------+
| Id | Salary |
+----+--------+
Expand All @@ -12,15 +14,24 @@ Write a SQL query to get the second highest salary from the Employee table.
| 3 | 300 |
+----+--------+
SQL 架构:
Create table If Not Exists Employee (Id int, Salary int)
Truncate table Employee
insert into Employee (Id, Salary) values ('1', '100')
insert into Employee (Id, Salary) values ('2', '200')
insert into Employee (Id, Salary) values ('3', '300')
For example, given the above Employee table, the query should return 200 as the second highest salary.
If there is no second highest salary, then the query should return null.
例如上述 Employee 表,SQL查询应该返回 200 作为第二高的薪水。如果不存在第二高的薪水,那么查询应返回 null。
+---------------------+
| SecondHighestSalary |
+---------------------+
| 200 |
+---------------------+
*/
****************************************************************************************************/


--Method1:
SELECT MAX(Salary) AS SecondHighestSalary
Expand All @@ -32,7 +43,7 @@ WHERE Salary < (SELECT MAX(Salary) FROM Employee)
WITH salaryRank AS
(
SELECT *,
RANK() OVER(ORDER BY Salary Desc) AS rank
DENSE_RANK() OVER(ORDER BY Salary DESC) AS rank
FROM Employee
)

Expand All @@ -42,3 +53,4 @@ SELECT ISNULL(
WHERE rank = 2)
, NULL) AS SecondHighestSalary


0 comments on commit d263375

Please sign in to comment.