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. 转:Linux基本命令大全

    Linux基本命令大全   新手刚刚接触Linux的时候可能处处感到不便,不过没有关系,接触新的事物都有这样的一个过程,在你用过Linux一段时间后,你就会逐渐了解Linux其实和Windows一样容 ...

  2. Intent之间无法传递大数据的替代方法

    /** * TODO: Activity之间传递list,对象等工具类 * * @author * @date 2014-9-12 下午5:35:38 * @version 0.1.0 */ publ ...

  3. C++:string类的使用

    类 <string> std::string String类的定义 , 其也是个模板类 typedef basic_string<char> string; String cl ...

  4. Using Sphinx to index CNS database

    1, look at the sphinx.person.address.conf to see how to configure the conf file2, index the database ...

  5. Nginx配置proxy_pass【转载】

    在nginx中配置proxy_pass时,当在后面的url加上了/,相当于是绝对根路径,则nginx不会把location中匹配的路径部分代理走;如果没有/,则会把匹配的路径部分也给代理走. 下面四种 ...

  6. 无法加载shockwave flash

    热心网友 360浏览器的话,浏览器——工具——选项(非Internet选项)——高级设置——FLASH, 默认使用PPAPI Flash(需要重启浏览器) 默认使用NPAPI Flash(需要重启浏览 ...

  7. C# 双引号的输出

    Console.WriteLine("\"a little list.\"");

  8. 使用FusionCharts出柱状图和饼状图

    在最近的项目中,需要使用出图,能够查看柱状图,饼状图等效果,刚开始我们用JS写的效果,发现效果不理想,找了一个JS插件发现效果还是不理想,客户也不满意,客户希望要很炫的效果,最后我们使用了Fusion ...

  9. 简单的划分数问题<划分问题>

    将整数n分成k份,且每份不能为空,任意两份不能相同(不考虑顺序). 例如:n=7,k=3,下面三种分法被认为是相同的. 1,1,5; 1,5,1; 5,1,1; 问有多少种不同的分法. 思路: 动态规 ...

  10. s15day14 ssh秘钥远程连接

    1 使用密钥登录    1.1 创建密钥对    1.2 上传公钥文件    1.3 导入公钥信息    1.4 使用密钥对登录2 远程执行命令    2.1 简单命令    2.2 使用脚本执行多命 ...