SQL

Retrieve the Nth Highest Value from a Column

Efficiently fetch the Nth highest distinct value from a specific column in a database table using SQL's ORDER BY, LIMIT, and OFFSET clauses for ranking.

SELECT DISTINCT salary
FROM employees
ORDER BY salary DESC
LIMIT 1 OFFSET 2;
How it works: This query retrieves the Nth highest distinct `salary` from the `employees` table. It first selects `DISTINCT salary` values to avoid duplicate ranks. Then, it orders them in `DESC`ending order. `LIMIT 1` fetches only one row, and `OFFSET 2` skips the first two highest salaries (1st and 2nd), effectively returning the 3rd highest distinct salary. Adjust the `OFFSET` value to `N-1` for the Nth highest value desired.

Need help integrating this into your project?

Our team of expert developers can help you build your custom application from scratch.

Hire DigitalCodeLabs