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. ios 概况了解

    iOS的系统架构分为四个层次:( iOS是基于UNIX内核,android是基于Linux内核) 核心操作系统层(Core OS layer).核心服务层(Core Services layer).媒 ...

  2. CentOS Hadoop安装配置详细

    总体思路,准备主从服务器,配置主服务器可以无密码SSH登录从服务器,解压安装JDK,解压安装Hadoop,配置hdfs.mapreduce等主从关系. 1.环境,3台CentOS7,64位,Hadoo ...

  3. 关于python的可变和不可变对象

    在python中所有都是对象,在python中只有list和dict是可变对象,其他都是不可变对象. 具体参照:http://www.cnblogs.com/lovemo1314/archive/20 ...

  4. 고서--做完A之后做B, B受A影响

    1. 합격 소식을 듣고서 매우 기뻤어요.. 2. 친구하고 심하게 다투고서 마음이 안 좋았어요. 3. 급한 일을 먼저 끝내고서 이야기합시다.' 4. 창문을 열고서 상쾌한 공기를 마서 ...

  5. PHP 命名空间以及自动加载(自动调用的函数,来include文件)

    这篇文章的目的是记录 1. php中的自动加载函数 __autoload(), 和 spl_autoload_register()函数, 2 .php中命名空间的使用. 一.当不使用命名空间的时候 a ...

  6. myeclipse导出javadoc时特殊字符 尖括号

    源字符<?xml version="1.0" encoding="UTF-8" standalone="yes"?> javad ...

  7. Swift 项目中常用的第三方框架

    Swift 项目中可能用到的第三方框架 字数1004 阅读4091 评论17 喜欢93 这里记录下swift开发中可能用的框架 , 最近浏览了不少,积累在这里,以后用的时候方便查阅.顺便推荐给大家! ...

  8. Qt5:不规则按钮的实现---通过贴图实现

    在应用开发中,有时候为了美观会在UI界面中增加不规则的按钮 现在我们就来看看Qt中是怎么实现不规则按钮的 /////////////////////////////////////////////// ...

  9. PAT1012

    To evaluate the performance of our first year CS majored students, 为了计算第一年计算机科学学生的表现 we consider the ...

  10. 对于使用了SSH造成的中文乱码问题,4大解决方法

    修改struts2.xml struts2.xml 中添加 <constant name="struts.i18n.encoding" value="UTF-8&q ...