cjql / algorithm

https://cjql.github.io/algorithm/
1 stars 1 forks source link

sql: second-highest-salary #15

Open cjql opened 4 years ago

cjql commented 4 years ago

https://leetcode.com/problems/second-highest-salary/

SELECT max(Salary) AS SecondHighestSalary
FROM Employee
WHERE Salary < (SELECT max(Salary) FROM Employee)

Runtime: 276 ms, faster than 34.31% of MySQL online submissions for Second Highest Salary. Memory Usage: 0B, less than 100.00% of MySQL online submissions for Second Highest Salary.

select (
  select distinct Salary from Employee order by Salary Desc limit 1 offset 1
)as SecondHighestSalary

Runtime: 346 ms, faster than 18.18% of MySQL online submissions for Second Highest Salary. Memory Usage: 0B, less than 100.00% of MySQL online submissions for Second Highest Salary.

 SELECT MAX(Salary) AS SecondHighestSalary
 FROM (SELECT E1.Salary
 FROM Employee as E1 JOIN Employee as E2
 ON E1.Salary < E2.Salary
 GROUP BY E1.Id HAVING COUNT(E2.Id) = 1
 )  AS SecondHighestSalary
 ORDER BY Salary DESC LIMIT 1;

Runtime: 372 ms, faster than 14.46% of MySQL online submissions for Second Highest Salary. Memory Usage: 0B, less than 100.00% of MySQL online submissions for Second Highest Salary.