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. 把Wordpress集成到zen-cart里方法 各种修改 经典机制

    作者: 闻庭牛 | 分类: zen cart插件精解 | 浏览: 4 | 评论: 暂时没有评论 如果你的Zen-cart需要一个Blog来发布一些你的最新动态,可以试试Wordpress,并且用WOZ ...

  2. Junit单元测试的简单使用(主要是在spring框架下的项目)

    首先是解释什么是单元测试,单元测试是指对于一个大型项目里,对于单一模块或者单一接口的测试. 然后解释为什么要写单元测试,首先对于一个大型的项目,如果你每次都要重启一遍服务器调页面或者接口的bug,那就 ...

  3. laravel提示Mcrypt PHP extension required

    系统Ubuntu 安装Apache,php后发现laravel报 Mcrypt PHP extension required错误 解决办法: apt-get install php5-mcrypt c ...

  4. zabbix agent自动安装脚本

    #!/bin/bash #desc: used for autoinstall zabbix client #说明:本脚本旨在批量安装zabbix_agent,在一个服务器上放好软件和配置文件,执行本 ...

  5. deibian不能加vpn

    http://www.cyberciti.biz/faq/deiban-ubuntu-linux-networkmanager-pptp-cisco-vpn-tab-disabled/ 经常搜goog ...

  6. FusionCharts生成Flash图表常见问题FAQ

    本文主要汇总了FusionCharts生成Flash图表时的一些常见问题(FAQ)以及解决方法/调试方法,欢迎交流! 问题描述:利用FusionCharts创建Flash图表时,能否直接从数组或rec ...

  7. hibernate里的generator中class =value介绍

    在*.hbm.xml必须声明的<generator>子元素是一个Java类的名字,用来为该持久化类的实例生成唯一的标识.<generator class="sequence ...

  8. IE6下最小19px像素

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...

  9. c# dev控件 gridcontrol 数据跟随鼠标滚轮滚动也可以编辑

    在绑定书到gridControl后经常发现: 如果你设置了 this.gridView3.OptionsBehavior.Editable = false; 那数据可以跟随滚轮滚动,但如果你要复制某个 ...

  10. css Loading 教程

    http://www.cnblogs.com/lhb25/p/loading-spinners-animated-with-css3.html