ℹ️ Select 'Choose Exercise', or randomize 'Next Random Exercise' in selected language.

Choose Exercise:
Timer 00:00
WPM --
Score --
Acc --
Correct chars --

SQL ANSI: Find the Nth Highest Salary

SQL ANSI

Goal -- WPM

Ready
Exercise Algorithm Area
1WITH RankedSalaries AS (
2-- Assign a dense rank to each distinct salary in descending order.
3-- DENSE_RANK is used to handle ties correctly, ensuring that if multiple employees
4-- have the same salary, they receive the same rank, and the next rank is consecutive.
5SELECT
6salary,
7DENSE_RANK() OVER (ORDER BY salary DESC) as salary_rank
8FROM
9Employees -- Assuming an 'Employees' table with a 'salary' column
10WHERE
11salary IS NOT NULL -- Exclude employees with no salary information
12)
13-- Select the salary that corresponds to the Nth rank.
14-- We use a subquery or CTE to first determine the ranks and then filter.
15SELECT
16salary
17FROM
18RankedSalaries
19WHERE
20salary_rank = :N -- Placeholder for the desired Nth rank (e.g., 3 for the 3rd highest salary)
21ORDER BY
22salary_rank ASC -- Ensure we get the salary for the Nth rank
23LIMIT 1; -- In case of ties for the Nth salary, this picks one (though DENSE_RANK handles this well).
Algorithm description viewbox

SQL ANSI: Find the Nth Highest Salary

Algorithm description:

This SQL query efficiently finds the Nth highest salary from a table of employees. It uses the `DENSE_RANK` window function to assign a unique rank to each distinct salary, treating ties appropriately. The query then selects the salary corresponding to the specified Nth rank. This is a common requirement for reporting and data analysis, particularly when dealing with compensation data.

Algorithm explanation:

The query utilizes a Common Table Expression (CTE) called `RankedSalaries`. Inside the CTE, `DENSE_RANK() OVER (ORDER BY salary DESC)` assigns a rank to each distinct salary value. `DENSE_RANK` is crucial here because it assigns consecutive ranks even if there are gaps in the salaries (e.g., if salaries are 10000, 9000, 7000, `DENSE_RANK` would assign ranks 1, 2, 3 respectively, whereas `RANK` would assign 1, 2, 4). The `ORDER BY salary DESC` ensures that higher salaries get lower ranks (e.g., rank 1 for the highest salary). The `WHERE salary IS NOT NULL` clause filters out any records without salary information. The main query then selects the `salary` from `RankedSalaries` where `salary_rank` equals the desired `N`. The `LIMIT 1` is a safeguard, though with `DENSE_RANK`, if `N` is valid, there should ideally be only one distinct salary for that rank. The time complexity is O(M log M) due to the sorting involved in the window function, where M is the number of employees with non-NULL salaries. Space complexity is O(M) for storing the ranked salaries. Edge cases include NULL salaries (handled), fewer than N distinct salaries (no result or an error depending on `N` and data), and `N` being invalid (e.g., 0 or negative, which would yield no results).

Pseudocode:

1. Create a CTE `RankedSalaries`:
   - Select `salary` from the `Employees` table.
   - Assign `DENSE_RANK()` ordered by `salary` DESC to `salary_rank`.
   - Filter out rows where `salary` is NULL.
2. Select `salary` from `RankedSalaries`.
3. Filter results where `salary_rank` equals N.
4. Order by `salary_rank` ASC.
5. Limit the result to 1.