Home » SQL & PL/SQL » SQL & PL/SQL » regarding query(7.1version)
regarding query(7.1version) [message #23656] Sun, 22 December 2002 21:07 Go to next message
akularaju
Messages: 6
Registered: July 2002
Junior Member
I want to select the first 3 higest paid salaries.
I am using oracle 7.1 version.Help me.
thanx in advance
regards,
Raj
Re: regarding query(7.1version) [message #23664 is a reply to message #23656] Mon, 23 December 2002 11:45 Go to previous messageGo to next message
Amit Chauhan
Messages: 74
Registered: July 1999
Member
Hi,
This is certainly one question asked many times :)
Heres a simple solution, but am not sure will it work in Oracle 7.1 or not (Quite old version, upgrade :) :
   select * from 
     (select *
      from emp 
      order by sal desc) a
   where rownum <= 3
/


Hope that helps
Thanks
Amit
Re: regarding query(7.1version) [message #23675 is a reply to message #23656] Tue, 24 December 2002 19:50 Go to previous message
Barbara Boehmer
Messages: 9096
Registered: November 2002
Location: California, USA
Senior Member
The method that Amit provided is the fastest method for retrieving the first 3 highest paid employees in Oracle 8i and 9i. However, prior to Oracle 8i, that is in Oracle 8.0 and Oracle 7, it was not allowed to have an order by clause within a sub-query, therefore you cannot use that method.

Below I have provided some older, slower methods that worked in Oracle 8.0. I don't know if they will work in Oracle 7.1 or not. Please test them and lest us know. You didn't provide your table structure, so I used the old Oracle emp demo table, for the examples. I also included the newer, faster methods, that you can use after you upgrade.

I have listed 8.0 methods for obtaining the first 3 highest paid employees for the company and the first 3 highest paid employees by department. I have also included 8.0 methods for retrieving the employees who earn the first 3 highest salaries, regardless of how many there are. This provides different results in case of ties. For example, if you want the 3 highest paid employees, you get one who earns $5,000 and two who earn $3,000. However, if you want the employees who earn the three highest salaries, you get one who earns $5,000, two who earn $3,000 and one who earns $2,975.

SQL> -- prior to Oracle 8i:
SQL> -- top 3 highest paid employees for whole company:
SQL> CLEAR    BREAKS
SQL> SELECT   outer.deptno, outer.empno, outer.ename, outer.sal
  2  FROM     emp outer
  3  WHERE    3 >=
  4  	      (SELECT COUNT (*) + 1
  5  	       FROM   emp inner
  6  	       WHERE  inner.sal > outer.sal)
  7  ORDER BY outer.sal DESC
  8  /

    DEPTNO      EMPNO ENAME             SAL                                     
---------- ---------- ---------- ----------                                     
        10       7839 KING             5000                                     
        20       7788 SCOTT            3000                                     
        20       7902 FORD             3000                                     

SQL> 
SQL> 
SQL> -- prior to Oracle 8i:
SQL> -- top 3 highest paid employees for each department:
SQL> BREAK    ON deptno
SQL> SELECT   outer.deptno, outer.empno, outer.ename, outer.sal
  2  FROM     emp outer
  3  WHERE    3 >=
  4  	      (SELECT COUNT (*) + 1
  5  	       FROM   emp inner
  6  	       WHERE  inner.sal > outer.sal
  7  	       AND    inner.deptno = outer.deptno)
  8  ORDER BY outer.deptno, outer.sal DESC
  9  /

    DEPTNO      EMPNO ENAME             SAL                                     
---------- ---------- ---------- ----------                                     
        10       7839 KING             5000                                     
                 7782 CLARK            2450                                     
                 7934 MILLER           1300                                     
        20       7788 SCOTT            3000                                     
                 7902 FORD             3000                                     
                 7566 JONES            2975                                     
        30       7698 BLAKE            2850                                     
                 7499 ALLEN            1600                                     
                 7844 TURNER           1500                                     

SQL> 
SQL> 
SQL> -- prior to Oracle 8i:
SQL> -- employees earning top 3 salaries for whole company:
SQL> CLEAR    BREAKS
SQL> SELECT   outer.deptno, outer.empno, outer.ename, outer.sal
  2  FROM     emp outer
  3  WHERE    3 >=
  4  	      (SELECT COUNT (DISTINCT sal) + 1
  5  	       FROM   emp inner
  6  	       WHERE  inner.sal > outer.sal)
  7  ORDER BY outer.sal DESC
  8  /

    DEPTNO      EMPNO ENAME             SAL                                     
---------- ---------- ---------- ----------                                     
        10       7839 KING             5000                                     
        20       7788 SCOTT            3000                                     
        20       7902 FORD             3000                                     
        20       7566 JONES            2975                                     

SQL> 
SQL> 
SQL> -- prior to Oracle 8i:
SQL> -- employees earning top 3 salaries for each department:
SQL> BREAK    ON deptno
SQL> SELECT   outer.deptno, outer.empno, outer.ename, outer.sal
  2  FROM     emp outer
  3  WHERE    3 >=
  4  	      (SELECT COUNT (DISTINCT sal) + 1
  5  	       FROM   emp inner
  6  	       WHERE  inner.sal > outer.sal
  7  	       AND    inner.deptno = outer.deptno)
  8  ORDER BY outer.deptno, outer.sal DESC
  9  /

    DEPTNO      EMPNO ENAME             SAL                                     
---------- ---------- ---------- ----------                                     
        10       7839 KING             5000                                     
                 7782 CLARK            2450                                     
                 7934 MILLER           1300                                     
        20       7788 SCOTT            3000                                     
                 7902 FORD             3000                                     
                 7566 JONES            2975                                     
                 7876 ADAMS            1100                                     
        30       7698 BLAKE            2850                                     
                 7499 ALLEN            1600                                     
                 7844 TURNER           1500                                     

SQL> 
SQL> 
SQL> -- since Oracle 8i:
SQL> -- top 3 highest paid employees for whole company:
SQL> CLEAR    BREAKS
SQL> SELECT deptno, empno, ename, sal
  2  FROM   (SELECT   deptno, empno, ename, sal
  3  	     FROM     emp
  4  	     ORDER BY sal DESC)
  5  WHERE  ROWNUM <= 3
  6  /

    DEPTNO      EMPNO ENAME             SAL                                     
---------- ---------- ---------- ----------                                     
        10       7839 KING             5000                                     
        20       7788 SCOTT            3000                                     
        20       7902 FORD             3000                                     

SQL> 
SQL> 
SQL> -- since Oracle 8i (requires Enterprise Edition):
SQL> -- top 3 highest paid employees for each department:
SQL> BREAK    ON deptno
SQL> SELECT  deptno, empno, ename, sal
  2  FROM    (SELECT deptno, empno, ename, sal,
  3  		     RANK () OVER (PARTITION BY deptno ORDER BY sal DESC) AS rk
  4  	      FROM   emp)
  5  WHERE   rk <= 3
  6  /

    DEPTNO      EMPNO ENAME             SAL                                     
---------- ---------- ---------- ----------                                     
        10       7839 KING             5000                                     
                 7782 CLARK            2450                                     
                 7934 MILLER           1300                                     
        20       7788 SCOTT            3000                                     
                 7902 FORD             3000                                     
                 7566 JONES            2975                                     
        30       7698 BLAKE            2850                                     
                 7499 ALLEN            1600                                     
                 7844 TURNER           1500                                     

SQL> 
SQL> 
SQL> -- since Oracle 8i:
SQL> -- employees earning top 3 salaries for whole company:
SQL> CLEAR    BREAKS
SQL> SELECT   deptno, empno, ename, sal
  2  FROM     emp
  3  WHERE    sal IN
  4  	      (SELECT sal
  5  	       FROM   (SELECT	DISTINCT sal
  6  		       FROM	emp
  7  		       ORDER BY sal DESC)
  8  	       WHERE  ROWNUM <= 3)
  9  ORDER BY sal DESC
 10  /

    DEPTNO      EMPNO ENAME             SAL                                     
---------- ---------- ---------- ----------                                     
        10       7839 KING             5000                                     
        20       7902 FORD             3000                                     
        20       7788 SCOTT            3000                                     
        20       7566 JONES            2975                                     

SQL> 
SQL> 
SQL> -- since Oracle 8i (requires Enterprise Edition):
SQL> -- employees earning top 3 salaries for each department:
SQL> BREAK    ON deptno
SQL> SELECT  deptno, empno, ename, sal
  2  FROM    (SELECT deptno, empno, ename, sal,
  3  		     DENSE_RANK () OVER (PARTITION BY deptno ORDER BY sal DESC) AS rk
  4  	      FROM   emp)
  5  WHERE   rk <= 3
  6  /

    DEPTNO      EMPNO ENAME             SAL                                     
---------- ---------- ---------- ----------                                     
        10       7839 KING             5000                                     
                 7782 CLARK            2450                                     
                 7934 MILLER           1300                                     
        20       7788 SCOTT            3000                                     
                 7902 FORD             3000                                     
                 7566 JONES            2975                                     
                 7876 ADAMS            1100                                     
        30       7698 BLAKE            2850                                     
                 7499 ALLEN            1600                                     
                 7844 TURNER           1500                                     

SQL> 
Previous Topic: How to write a Matrix report use SQL syntax ?
Next Topic: Oracle trigger calling java static method.
Goto Forum:
  


Current Time: Tue May 21 03:52:35 CDT 2024