This script can be used to return the top N salaries in the emp table. Back to SQL scripts


SELECT empno, ename, sal, deptno
FROM emp a
WHERE &topN > (SELECT count(1)
FROM emp) b
WHERE b.sal > a.sal



To run the code, cut and paste it onto your favourite editor and save the file. When you run it through SQL*PLUS, it asks you to specify a value for the 'topN' value. So for example if you want to display the top 4 salaries, specify a value 4 for topN (see sample output below).

When used with the emp table, as in the example, you get the following output.

SQL> SELECT empno, ename, sal, deptno
FROM emp a
WHERE &topN > (SELECT count(1)
FROM emp) b
WHERE b.sal > a.sal
Enter value for topN: 4

EMPNOENAMESALDEPTNO
-------------------------------------
7566 JONES 2975 20
7788 SCOTT 3000 20
7839 KING 5000 10
7902 FORD 3000 20

SQL>


Back to SQL scripts
Hosted by www.Geocities.ws

1