Say all the employees are listed in the same table employees
along with their departments. How would you find the nth largest salary by department?
I know this gets you pretty close.
select SAL from EMPLOYEE E1 where
(N - 1) = (select count(distinct(SAL))
from EMPLOYEE E2
where E2.SAL > E1.SAL )
But adding groupby still wouldn't get you there unless you did a join on the condition?
select SAL from EMPLOYEE E1
where
(N - 1) = (select count(distinct(SAL))
from EMPLOYEE E2
inner join EMPLOYEE E3 ON department_id
where E2.SAL > E3.SAL )
groupby department