Use this when the business question is about the nth highest value rather than the nth physical row.
Example
with ranked_salaries as (
select
employee_id,
salary,
dense_rank() over (order by salary desc) as salary_rank
from hr.employees
)
select
employee_id,
salary
from ranked_salaries
where salary_rank = 3;
Why this pattern helps
- handles ties without dropping valid rows
- is easier to explain than nested
max()logic - adapts cleanly to top-n and per-group ranking problems
Notes
- replace
3with any target rank you need - add
partition by department_idto find the nth highest value within a group - use
row_number()instead when you truly need the nth row only