Oracle FAQ | Your Portal to the Oracle Knowledge Grid |
![]() |
![]() |
Home -> Community -> Usenet -> c.d.o.tools -> Re: SQL Question
hi,
> Given an employees table like this,
> how could I query the top three salaried people per department in SQL?
imagine that
1 blue 1000
1 red 2000
1 green 4000
1 yellow 5000
1 black 2000
there's no proof that there will be just three top salaried people (how do
you decide between black or red which is the third?)
you can get the three top amount using something like
r1
select dpt,max(salary) "salary" from tables
r2
select dpt,max(salary) "salary" from table,(r1)r1
where r1.dpt=salary.dpt
and salary < r1."salary"
r3
select dpt,max(salary) "salary" from table,(r2)r2
where r2.dpt=salary.dpt
and salary < r2."salary"
select r1.dpt,r1.salary,r2.salary,r3.salary
from r1,r2,r3
where
r1.dpt=r2.dpt (+)
and r2.dpt=r3.dpt (+)
the "(+)" is in case most salary are the same (think of a small dpt) then there's no such thing as second ot third top salary
-- Antoine FERNIQUEReceived on Thu Nov 02 2000 - 06:56:34 CST
![]() |
![]() |