不用聚合函数求最高工资
来源:互联网 发布:淘宝助理和千牛 编辑:程序博客网 时间:2024/05/01 02:23
对于emp 表,不用聚合函数求出最高工资
如果使用聚合函数的话,求出最高工资比较方便
select max(sal) from emp;
如果不使用聚合函数的话,该从哪个方向出发呢?
可以排序,然后从排序后的结果中取工资最高的;可以取出除最高工资之外的所有工资,然后再排除,剩下最高工资。
method1 按工资收入降序排列
select * from emp order by sal desc
工资最高的5000 就排在第一个,接下来再取第一个即可
select a.sal from (select * from emp order by sal desc) a where rownum = 1;
method2 取最高工资之外的所有工资
select e2.sal from emp e1,emp e2 where e1.sal>e2.sal;
这里采用自连接,判断条件 e1.sal > e2.sal,结果取的是e2.sal,这注定最高工资不可能出现在结果集中
然后 再在emp 表中,排除掉上面结果集的sal,剩余的就是最高的sal了
select e.sal from emp e where e.sal not in(select e2.sal from emp e1,emp e2 where e1.sal>e2.sal);
看到第二种方法,突然想到第三种方法,那就是用上distinct 和 minus
method3 沿用 method2 的思路
select distinct e2.sal from emp e1,emp e2 where e1.sal>e2.sal;再用emp 中所有sal 和 上面集合中的sal 求minus
select distinct sal from empminusselect distinct e2.sal from emp e1,emp e2 where e1.sal>e2.sal;
好了,这里介绍三种方法,你还有其它的方法吗,分享出来吧,一起学习。
0 0
- 不用聚合函数求最高工资
- 自定义聚合函数求中位数
- 求工资
- 求工资
- C#聚合函数中求最小值,平均值
- 做什么技术工资最高?
- 如何用分析函数找出EMP表中每个部门工资最高的员工
- 不用sqrt()函数,如何求平方根
- 不用sqrt()函数,如何求平方根
- sql 面试题 函数 求工资第n多的工资
- 工资最高时成本最低、利润最高解析
- 工资最高的时候成本最低
- 取各部门工资最高人员名称
- 求每年最高气温
- 求工作人员工资 hadoop
- 第七周 求工资
- 求周工资
- 聚合函数
- 常见OJ评判结果对照表,作为ACMer你懂得!【转】
- 通过网页访问SQL数据库,提示java.lang.ClassNotFoundException: com.microsoft.jdbc.sqlserver.SQLServerDriver
- hibernate 中 get和load区别
- jdbc 连接池动态代理模式
- 对自己近两年的产品小结
- 不用聚合函数求最高工资
- OC中数组
- Cocos2d-x,SQLite3数据库用法,编译,配置
- arm,gpio开发第二天
- [C++11 并发编程] 05 - Mutex 基本操作
- 基于nio架构的可高扩展的服务器(Architecture of a highly scalable NIO-based server)
- 如何清楚描述spring
- [leetcode-86]Partition List(c)
- Objective-C_Block