不用聚合函数求最高工资
2015-08-09 20:00
309 查看
对于emp 表,不用聚合函数求出最高工资
如果使用聚合函数的话,求出最高工资比较方便
如果不使用聚合函数的话,该从哪个方向出发呢?
可以排序,然后从排序后的结果中取工资最高的;可以取出除最高工资之外的所有工资,然后再排除,剩下最高工资。
工资最高的5000 就排在第一个,接下来再取第一个即可
这里采用自连接,判断条件 e1.sal > e2.sal,结果取的是e2.sal,这注定最高工资不可能出现在结果集中
然后 再在emp 表中,排除掉上面结果集的sal,剩余的就是最高的sal了
看到第二种方法,突然想到第三种方法,那就是用上distinct 和 minus
再用emp 中所有sal 和 上面集合中的sal 求minus
好了,这里介绍三种方法,你还有其它的方法吗,分享出来吧,一起学习。
如果使用聚合函数的话,求出最高工资比较方便
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 emp minus select distinct e2.sal from emp e1,emp e2 where e1.sal>e2.sal;
好了,这里介绍三种方法,你还有其它的方法吗,分享出来吧,一起学习。
相关文章推荐
- [VB版机房]MSGFLEXGRID控件中的数据导出到excel
- JdbcTemplate --- 方法详解
- 二十大数据可视化工具(二)
- 随笔1
- Git的版本控制
- HDU 5366 简单动态规划DP
- JS监听组合按键
- 保存密码(KeyChain的使用)
- 栈的链表实现(JAVA)
- 初识MVC之建项
- Leetcode#114 Flatten Binary Tree to Linked List
- 通讯录的困惑
- JS获取URL后的参数并且解析汉字乱码
- 周总结0809
- 项目管理案例(二)
- 理解*ptr++
- iOS开发-- 设置UIButton的文字显示位置、字体的大小、字体的颜色
- Chemistry
- 大学生学习编程(PHP)
- ubuntu 10.04 ***搭建