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. codeforces 264D Colorful Stones

    题目 题目来自于rng_58Orz. 算法 讨论某个状态\((x,y)\)是否可达,\(x\)是狐狸到达的石头,\(y\)是猫的. 题解说,如果满足以下条件,那么它就是可到达状态: \(t[0..y] ...

  2. 多图片/文件上传 - SwfUpload/PlUpload

    <文件上传利器SWFUpload使用指南> <前端上传组件Plupload使用指南>

  3. MSSQL - 备份和还原数据库

    SQL语句备份和还原数据库:http://blog.csdn.net/liuhelong/article/details/3335687 1.MSSQL - SqlServer:此数据库处于单用户模式 ...

  4. linux 解决Ubuntu编译内核uImage出现问题“mkimage” command not found - U-Boot images will not be built问题

    解决Ubuntu编译内核uImage出现问题“mkimage” command not found - U-Boot images will not be built问题 http://www.lin ...

  5. mysql死锁问题分析(转)

    线上某服务时不时报出如下异常(大约一天二十多次):“Deadlock found when trying to get lock;”. Oh, My God! 是死锁问题.尽管报错不多,对性能目前看来 ...

  6. VS2010+QT4.8.5 +FastReport教程

    如需转载请标明出处:http://blog.csdn.net/itas109 採用QT 的QAxObject方式执行FastReport特别麻烦,并且无法在编译的时候知道代码是否正确,并且大部分的函数 ...

  7. WinFrom - DataGridView控件右键选中记录并弹出菜单

    dataGridView右键菜单并选中该行 程序代码: private void dataGridView1_CellMouseDown(object sender, DataGridViewCell ...

  8. 单选按钮、复选按钮——axure线框图部件库介绍

    有时候发现这做事情坚持下来是一件很不容易的,写教程也一样,不过听到很多朋友对我说 这个全部是图片的教程 对他们入门帮助很多,我就想想 在坚持坚持把基础部分先完善了! 1. 简单的问卷调查: 您的性别? ...

  9. 性能测试之LoardRunner 手动关联一

    概述: 1.什么是关联,关联的分类? 2.实例讲解 1.关联 为什么要关联?关联是应用LoadRunner进行性能测试的一项重要技能,那为什么我们要进行关联呢?当利用VuGen录制脚本时,它会拦截Cl ...

  10. 【Bootstrap3.0建站笔记二】button可下拉弹出层

    1.button可下拉弹出层: watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvY2hpbmFwbGFu/font/5a6L5L2T/fontsize/400 ...