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.

思路:解法1.找出最高的salary, 所有小于最高salary中的最大值就是第二高的salary。

# Write your MySQL query statement below
select max(Salary) as SecondHighestSalary from Employee
where Salary < (select max(Salary) from Employee)

解法2. 参考 http://tsuinte.ru/2015/04/05/leetcode-database-176-second-highest-salary

构造一个tmp,从里面选,如果tmp为空则返回null

 # Write your MySQL query statement below
select
if(count(Salary) >= 1, Salary, null) as SecondHighestSalary
from (select distinct salary from Employee
order by Salary desc limit 1,1) tmp

Leetcode 176. Second Highest Salary的更多相关文章

  1. LeetCode 176 Second Highest Salary mysql,select 嵌套 难度:1

    https://leetcode.com/problems/second-highest-salary/ Write a SQL query to get the second highest sal ...

  2. LeetCode 176. Second Highest Salary (第二高的薪水)

    题目标签: 题目给了我们一个工资表,让我们返回第二高的工资. 利用Max,把第一高的工资找到,然后利用 NOT IN,去找到第二高的工资. Java Solution: Runtime:  153ms ...

  3. LeetCode 177. Nth Highest Salary

    https://leetcode.com/problems/nth-highest-salary/description/ Write a SQL query to get the nth highe ...

  4. LeetCode DB: Department Highest Salary

    The Employee table holds all employees. Every employee has an Id, a salary, and there is also a colu ...

  5. [LeetCode] 176. Second Highest Salary_Easy tag: SQL

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

  6. LeetCode SQL: Second Highest Salary

    , NULL, salary) as `salary` from ( ,) tmp Write a SQL query to get the second highest salary from th ...

  7. 【SQL】176. Second Highest Salary

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

  8. 176. Second Highest Salary

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

  9. [LeetCode#184]Department Highest Salary

    The Employee table holds all employees. Every employee has an Id, a salary, and there is also a colu ...

随机推荐

  1. HDU - 1083 Courses /POJ - 1469

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1083 http://poj.org/problem?id=1469 题意:给你P个课程,并且给出每个课 ...

  2. QQ的邮我功能

    首先是从QQ邮箱中获取到“邮我”的功能获取代码,然后点击后就可以直接的设定好收件人为自己了. 给我写信

  3. js动态添加file控件

    <html></head><script language="javascript" type="text/ecmascript" ...

  4. Myeclipse8.5中svn插件安装方法总结

    [转]http://lwcheng1985.iteye.com/blog/696143   有改动 方法一:在线安装 1.打开HELP->MyEclipse Configuration Cent ...

  5. (转)C++中返回对象的情形及RVO

    http://www.cnblogs.com/xkfz007/archive/2012/07/21/2602110.html 之前有文章介绍过临时对象和返回值优化RVO方面的问题.见此处. 在C++中 ...

  6. 上传数据插件 Easy Populate 遇到问题 ?langer=remove

    批量上传,出现如下错误提示: ADD NEW PRODUCT FAILED! – Model: no200 – SQL error. Check Easy Populate error log in ...

  7. (中等) CF 585D Lizard Era: Beginning,中途相遇。

    In the game Lizard Era: Beginning the protagonist will travel with three companions: Lynn, Meliana a ...

  8. 手动启动Android模拟器

    1.5版本中加了个所谓的AVD(Android Virtual Device),AVD就相当于是一个模拟器的,不过你可以利用AVD创建基于不同版本的模拟器,然后使用emulator-avd avdNa ...

  9. CodeForces 662D International Olympiad

    写出前几个找规律,然后直接输出. #include<cstdio> #include<cstring> #include<cmath> #include<al ...

  10. Scratch2.0例—接苹果

    Scratch2.0例—接苹果 [教学目标] 1. 学习例子,能用和构造条件 ,并把此条件插入到 中:能理解和应用,当条件成立时,不执行积木内的脚本. 2. 对比和 两个积木:前者用于无条件的重复执行 ...