Leetcode-Database-176-Second Highest Salary-Easy(转)
leetcode地址:https://oj.leetcode.com/problems/second-highest-salary/
这个问题很有趣,是要求我们写个sql来查询Employee表里第二高的工资,如果没有第二高的,那么返回null。
+----+--------+
| Id | Salary |
+----+--------+
| 1 | 100 |
| 2 | 200 |
| 3 | 300 |
+----+--------+
看到这个问题,可能很多人会想,这很简单啊,写个order by desc,然后找到第二个即可。
试试提交呗?Wrong answer,为什么?看条件约束啊,没有第二要返回null,我看到null的第一直觉是通过join搞到null值,于是有了下面的ac sql:
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(转)的更多相关文章
- leetcode - database - 177. Nth Highest Salary (Oracle)
题目链接:https://leetcode.com/problems/nth-highest-salary/description/ 题意:查询出表中工资第N高的值 思路: 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 ...
- Leetcode 176. Second Highest Salary
Write a SQL query to get the second highest salary from the Employee table. +----+--------+ | Id | S ...
- 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. +----+ ...
- 【SQL】176. Second Highest Salary
Write a SQL query to get the second highest salary from the Employee table. +----+--------+ | Id | S ...
- 176. Second Highest Salary
Write a SQL query to get the second highest salary from the Employee table. +----+--------+ | Id | S ...
- LeetCode 176. Second Highest Salary (第二高的薪水)
题目标签: 题目给了我们一个工资表,让我们返回第二高的工资. 利用Max,把第一高的工资找到,然后利用 NOT IN,去找到第二高的工资. Java Solution: Runtime: 153ms ...
- LeetCode_Mysql_Second Highest Salary
176. Second Highest Salary 1. 问题描写叙述: 写一个sql语句从 Employee 表里获取第二高位的工资. 2. 解决思路: 这道题非常easy,就当热身了.首先用ma ...
- LeetCode Database题解
175. Combine Two Tables 使用外连接即可. # Write your MySQL query statement below select FirstName, LastName ...
- [leetcode] database解题记录
175 Combine Two Tables 题目:左连接Person表和Address表. select FirstName,LastName,City,State from Person p le ...
随机推荐
- ARM相关知识
ARM7采用冯·诺依曼(Von-Neumann)结构,数据存储器和程序存储器重合在一起. 同时,此结构也被大多数计算机所采用. ARM7为三级流水线结构(取指,译码,执行),平均功耗为0.6mW ...
- android 调用系统打电话和发短,懒得记
Intent intent = new Intent(); intent.setAction(Intent.ACTION_CALL); intent.setData( ...
- 关于运行robotium提示连接不上jar问题
robotium运行测试helloworld报错: java.lang.NoClassDefFoundError: com.jayway.android.robotium.solo.Solo at c ...
- Swift - 获取字符串的MD5值
MD5即Message-Digest Algorithm 5(信息-摘要算法5),用于确保信息传输完整一致.是计算机广泛使用的杂凑算法之一(又译摘要算法.哈希算法),主流编程语言普遍已有MD5实现. ...
- crm高速开发之QueryExpression
/* 创建者:菜刀居士的博客 * 创建日期:2014年07月06号 */ namespace Net.CRM.OrganizationService { using System; ...
- Windows 7 taskbar and startmenu pin
原文 Windows 7 taskbar and startmenu pin 在Windows 7上,用户可以将自己喜欢的软件“钉”在开始菜单或任务栏,使用起来更加方便.但有时候我们也需要用程序来将这 ...
- 基于visual Studio2013解决面试题之1007鸡蛋和篮子
题目
- iOS "The sandbox is not in sync with the Podfile.lock"解决方式
更新Cocoapod之后出现故障: diff: /../Podfile.lock: No such file or directory diff: Manifest.lock: No such fil ...
- 通过Jexus 部署 dotnetcore
通过Jexus 部署 dotnetcore版本MusicStore 示例程序 ASPNET Music Store application 是一个展示最新的.NET 平台(包括.NET Core/Mo ...
- Qt录音程序
源地址:http://www.oschina.net/code/snippet_1243295_48623 [代码] [C/C++]代码 ? 1 2 3 4 5 6 7 8 9 10 11 12 13 ...