leetcode地址:https://oj.leetcode.com/problems/second-highest-salary/

这个问题很有趣,是要求我们写个sql来查询Employee表里第二高的工资,如果没有第二高的,那么返回null。

+----+--------+
| Id | Salary |
+----+--------+
| 1  | 100    |
| 2  | 200    |
| 3  | 300    |
+----+--------+

看到这个问题,可能很多人会想,这很简单啊,写个order by desc,然后找到第二个即可。

select Salary from Employee order by Salary desc limit 1,1

试试提交呗?Wrong answer,为什么?看条件约束啊,没有第二要返回null,我看到null的第一直觉是通过join搞到null值,于是有了下面的ac sql:

select
    max(Salary) as SecondHighestSalary
from(
select
o1.*
,case when o2.s is null then 1 else 0 end as nt
from
(select * from Employee)o1
left outer join
(select max(Salary) as s from Employee)o2
on(o1.Salary=o2.s)
)t
where nt=1

思路简单说就是通过全表左外联最大salary,从关联不到的salary里再找最大不就是第二大吗?

最后的结果是894ms,当然我坚信有很多更快更高效的结果。

myself:
oracle中使用rownum不能实现,因为如果只有一条记录则会把这条记录做为最后的结果返回。

使用rownum的sql:

select * from (
select * from (
select * from employee e
order by e.salary desc
) t1
where rownum<3
)t2
where rownum<2
order by t2.salary asc

参考Change Dir,使用oracle时的另一种写法:

select max(salary) SecondHighestSalary from (

select o1.*,case when o2.s is null then 1 else 0 end status
from
(select * from employee) o1,
(select max(salary) s from employee)o2
where o1.salary=o2.s(+) )
where status=1

http://www.blogjava.net/changedi/archive/2015/01/27/422478.html

Leetcode-Database-176-Second Highest Salary-Easy(转)的更多相关文章

  1. leetcode - database - 177. Nth Highest Salary (Oracle)

    题目链接:https://leetcode.com/problems/nth-highest-salary/description/ 题意:查询出表中工资第N高的值 思路: 1.先按照工资从高到低排序 ...

  2. 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 ...

  3. Leetcode 176. Second Highest Salary

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

  4. leetcode Database3(Nth Highest Salary<—>Consecutive Numbers<—>Department Highest Salary)

    一.Nth Highest Salary Write a SQL query to get the nth highest salary from the Employee table. +----+ ...

  5. 【SQL】176. Second Highest Salary

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

  6. 176. Second Highest Salary

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

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

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

  8. LeetCode_Mysql_Second Highest Salary

    176. Second Highest Salary 1. 问题描写叙述: 写一个sql语句从 Employee 表里获取第二高位的工资. 2. 解决思路: 这道题非常easy,就当热身了.首先用ma ...

  9. LeetCode Database题解

    175. Combine Two Tables 使用外连接即可. # Write your MySQL query statement below select FirstName, LastName ...

  10. [leetcode] database解题记录

    175 Combine Two Tables 题目:左连接Person表和Address表. select FirstName,LastName,City,State from Person p le ...

随机推荐

  1. Swift - 使用UI Dynamics给UIKit组件添加重力和碰撞行为

    UI Dynamics是UIKit的一个新组成部分,它向iOS中的视图提供了与物理学有关的功能和动画.可以让你向视图中引入力和物理属性,可以让你的视图弹跳,舞动,受重力影响等等. 下面通过样例,演示使 ...

  2. HTML文档类型DTD与浏览器怪异模式

    虽然在兼容IE6时候经常会注意到两个模式的区别,但是系统的理解起来,还没有认真总结过.看了一些网上的资料.结合自己的理解汇总了一下,放在这里备忘并分享给大家. 浏览器从服务端获取网页后会根据文档的DO ...

  3. RHEL4 i386下安装rdesktop【原创】

    http://rpmfind.net/ 1.根据系统下载rdesktop (1)查看Linux版本:# lsb_release -aLSB Version: :core-3.0-ia32:core-3 ...

  4. js 动态切换视频

    如图所示,想要一个这样的效果,就是点击下面视频标题时,上面的视频跟着切换,但是要求页面不重新加载. 参考文章在这里 这里贴上部分代码供大家参考. <li id="about_li6&q ...

  5. 基于visual Studio2013解决C语言竞赛题之1061最大值和次最大值

       题目 解决代码及点评 /* 功能: 编写子函数, 求一维整型数组M[10]的最大值及次最大值(次最大值可能不存在). 主函数中输入10个整数, 然后调用上述子函数, 若次最大值存在, ...

  6. 能够返回运行结果的system函数加强版本号

    /*********************************************************************  * Author  : Samson  * Date   ...

  7. oracle flashback 2

    Flashback  database      After oracle 10g, oracle can rollback to an prior time by flashback databas ...

  8. 杭电 1711 Number Sequence

    Number Sequence Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

  9. git使用说明

    1,git clone git://github.com/schacon/simplegit.git git工作目录,暂存目录,本地代码仓库都有代码了. 2,git pull git://github ...

  10. 一百多道.NET面试题!

    1.a=10,b=15,在不用第三方变量的前提下,把a,b的值互换 方法一: a=a+b;  b=a-b;  a=a-b;  方法二: a^=b^(b^=a^b);  2.已知数组int[] max= ...