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,然后找到第二个即可。

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

 

时间: 2024-07-28 17:35:28

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

LeetCode All in One 题目讲解汇总(持续更新中...)

终于将LeetCode的免费题刷完了,真是漫长的第一遍啊,估计很多题都忘的差不多了,这次开个题目汇总贴,并附上每道题目的解题连接,方便之后查阅吧~ 如果各位看官们,大神们发现了任何错误,或是代码无法通过OJ,或是有更好的解法,或是有任何疑问,意见和建议的话,请一定要在对应的帖子下面评论区留言告知博主啊,多谢多谢,祝大家刷得愉快,刷得精彩,刷出美好未来- 博主制作了一款iOS的应用"Leetcode Meet Me",里面有Leetcode上所有的题目,并且贴上了博主的解法,随时随地都能

CONNECTING DATABASE USING JAVASERVERPAGES (转载)

erp|server I have given you the source code, how to connect with database from jsp. this is easy and simple.cut and paste the below code and modify according to your requirements.This is esspecially for those who had coding experience in ASP.I have g

PL/SQL --&amp;gt; DML 触发器

--======================= -- PL/SQL --> DML 触发器 --=======================         何谓触发器?简言之,是一段命名的PL/SQL代码块,只不过该代码块在特定的条件下被触发并且执行.对于这样的代码我们称之为触发器 .触发器根据触发类型的不同又分为不同级别的触发器,下面将给出触发器的分类,定义,以及使用的示例.   一.触发器的相关概念     1.触发器的分类         通常根据触发条件以及触发级别的不同分为DM

PL/SQL12.1 ——DML 触发器

 http://blog.csdn.net/robinson_0612/article/details/6098258  何谓触发器?简言之,是一段命名的PL/SQL代码块,只不过该代码块在特定的条件下被触发并且执行.对于这样的代码我们称之为触发器 .触发器根据触发类型的不同又分为不同级别的触发器,下面将给出触发器的分类,定义,以及使用的示例.   一.触发器的相关概念     1.触发器的分类         通常根据触发条件以及触发级别的不同分为DML触发器,INSTEAD OF 触发器,系

[Unitils]初识Unitils

http://www.unitils.org/summary.html#  Summary  Unitils is an open source library aimed at making unit and integration testing easy and maintainable.  It is divided into several modules, each of them providing extra support for a certain aspect of you

zabbix feature &amp; architecture &amp; Data flow

主流的监控软件, nagios, zabbix, ganglia. 因为有项目用到zabbix, 研究一下. 3 Zabbix features Overview Zabbix is a highly integrated network monitoring solution, offering a multiplicity of features in a single package. Data gathering availability and performance checks s

C# 设计模式系列教程-外观模式_C#教程

1. 概述 为子系统中的一组接口提供一个一致的界面,此模式定义了一个高层接口,这个接口使得这一子系统更加容易使用. 2. 模式中的角色 2.1 外观类(Facade):外观类知道哪些子系统类负责处理请求,将客户的请求代理给恰当的子系统对象. 2.2 子系统类集合(SubSystem Classes):子系统类集合实现了子系统的功能,处理外观类对象指派的任务. 3. 模式解读 3.1 外观模式的类图 3.2 外观模式的代码实现 /// <summary> /// 子系统中的一个类 /// <

NoSQL Databases - CouchDB

CouchDB还是蛮有意思的一个DB, 总结一下, 他重要的特点 1. 最大的特点就是他的file layout and commitment system, 并由此可以保证ACID特性, 在Nosql里面相当有特点, 参考5.1.6 2. 采用View机制, 这个很方便, 通过javascript就可以简单的定义view, 并可以通过map/reduce逻辑生成view, 但要注意的是, 这是伪map/reduce, 因为只能在单机上运行, 只是使用了这种模型而已. 但存在一个问题, View

Amazon云计算应用领域

Hundreds of thousands of customers have joined the Amazon Web Services community in order to build solutions for their businesses. The AWS cloud computing platform provides the flexibility to build your application, your way, regardless of your use c

sql max()函数用法

最简单的语法 SELECT MAX(expression ) FROM tables WHERE predicates; 实例  代码如下 复制代码 SELECT MAX(salary) as "Highest salary" FROM employees; 让它与 GROUP BY同时使用  代码如下 复制代码 SELECT department, MAX(salary) as "Highest salary" FROM employees GROUP BY de