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. matlab三维画图

    matlab三维画图主要有三个命令:plot3命令.mesh命令和surf命令. plot3 plot3是三维画图的基本函数,绘制的是最为主要的3D曲线图,最主要的调用格式是: plot3(X,Y,Z ...

  2. iphone分辨率终极指南(含有iphone6/6+)

    如文本不清楚.请 "对->图片另存为" 下载大图后, ------------------------- 原文及翻译文本 Points点 At the beginning, ...

  3. ARMv8 Linux内核源代码分析:__flush_dcache_all()

    1.1 /* *  __flush_dcache_all() *  Flush the wholeD-cache. * Corrupted registers: x0-x7, x9-x11 */ EN ...

  4. fcntl()

      fcntl() F_GETFL---------------------------------------------        将文件状态标志作为函数值返回. 文件状态标志:        ...

  5. perl 自动登陆网站发短信

    use LWP::UserAgent; use HTTP::Date qw(time2iso str2time time2iso time2isoz); use Net::Ping; use Sock ...

  6. boost操作xml 5分钟官方教程

    Five Minute Tutorial This tutorial uses XML. Note that the library is not specifically bound to XML, ...

  7. php的var关键字

    public和var的作用差不多 因为 var定义的变量如果没有加protected 或 private则默认为public php4 中一般是用 varphp5 中就一般是用 public了 现在基 ...

  8. 二维码闪电登录流程详解,附demo(2/2)

    上篇文章,我们重点介绍了一下二维码登录的流程,以及每个“角色”要做的事情,下面我们重点分析TV角色所做的工作. TV主要完成二维码图片显示,以及websocket请求.下面重点说一下这两点. 1. B ...

  9. asp.net2.0安全性(2)--用户个性化设置(1)--转载来自车老师

    在Membership表中可以存储一些用户的基本信息,但有的时候,我们需要记录的用户信息远远不止Membership表中提供的这些,如QQ.MSN.家庭住址.联系电话等等.那如何把这些用户信息记录到数 ...

  10. oracle执行带输入输入参数的存储过程

    declare a1 ); a2 ); begin PKG_INPATIENT.prc_autojf('Y', a1, a2); end;