不用彙總函式求最高工資

來源:互聯網
上載者:User

標籤:不用彙總函式求最高工資   最高工資   關聯查詢   rownum   


對於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;


好了,這裡介紹三種方法,你還有其它的方法嗎,分享出來吧,一起學習。



著作權聲明:本文為博主原創文章,未經博主允許不得轉載。

不用彙總函式求最高工資

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.