databases
10 minTop-N and Third-Highest Salary Queries in PostgreSQL, MySQL, and Oracle
Choose between distinct salary ranking, row ranking, and pagination syntax while handling ties and NULLs explicitly.
Loading page...
databases
10 minChoose between distinct salary ranking, row ranking, and pagination syntax while handling ties and NULLs explicitly.
Q&A
Q&AUse DENSE_RANK for distinct values and state whether you want salary values or employee rows.
Filter out NULL salaries, compute DENSE_RANK() OVER (ORDER BY salary DESC), then select rank 3. DENSE_RANK treats equal salaries as the same rank. PostgreSQL, MySQL 8+, and Oracle support this window-function approach; if you instead mean the third row, use ROW_NUMBER with a deterministic tie-breaker.