Move the subquery to the SELECT with no โtop 2โ constraint (obviously, get more rows back):
select a.*, ( select count( distinct(sal)) from emp where sal > a.sal and a.deptno=deptno ) as tally from emp a
Then you can limit the result set with the WHERE , which represents an additional level, for example.
select b.* from ( select a.*, ( select count( distinct(sal)) from emp where sal > a.sal and a.deptno=deptno ) as tally from emp a ) b where b.tally < 2 order by b.deptno, b.tally;
The above is more detailed, but it may be easier to follow the logic.
source share