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

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

SQL ANSI: Find Nth Highest Salary with CTE

SQL ANSI

Goal -- WPM

Ready
Exercise Algorithm Area
1WITH RankedSalaries AS (
2-- Assign a rank to each distinct salary in descending order
3SELECT
4salary,
5DENSE_RANK() OVER (ORDER BY salary DESC) as salary_rank
6FROM (
7-- Select only distinct salaries to avoid ranking duplicates multiple times
8SELECT DISTINCT salary
9FROM employees
10) AS DistinctSalaries
11)
12-- Select the salary that corresponds to the Nth rank
13SELECT salary AS NthHighestSalary
14FROM RankedSalaries
15WHERE salary_rank = :N -- :N is a placeholder for the desired rank (e.g., 2 for second highest)
16
17-- Helper function: The DENSE_RANK() window function is the core helper here.
18-- It assigns a rank to each unique value in a partition of a result set.
19-- Unlike RANK(), DENSE_RANK() assigns consecutive ranks without gaps.
20-- For example, if salaries are 100, 90, 90, 80, the ranks would be 1, 2, 2, 3.
21
22-- Edge Case: Empty 'employees' table.
23-- If 'employees' is empty, the inner SELECT DISTINCT salary will return an empty set.
24-- The CTE 'RankedSalaries' will also be empty.
25-- The final SELECT statement will query an empty CTE, resulting in an empty result set, which is correct.
26
27-- Edge Case: N is greater than the number of distinct salaries.
28-- If N is, for example, 5, but there are only 3 distinct salaries, the WHERE salary_rank = 5 clause
29-- will not find any matching rows in the 'RankedSalaries' CTE.
30-- The query will correctly return an empty result set.
31
32-- Edge Case: N is 0 or negative.
33-- The DENSE_RANK() function generates ranks starting from 1.
34-- If :N is 0 or negative, the WHERE salary_rank = :N condition will never be met,
35-- resulting in an empty result set, which is the desired behavior.
36
37-- Edge Case: Duplicate salaries.
38-- DENSE_RANK() handles duplicates by assigning the same rank to identical salaries
39-- and then assigning the next consecutive integer rank to the subsequent distinct salary.
40-- This ensures that if the 2nd highest salary is 90, and multiple employees earn 90,
41-- the query will still return 90 as the 2nd highest salary.
Algorithm description viewbox

SQL ANSI: Find Nth Highest Salary with CTE

Algorithm description:

This SQL query finds the Nth highest salary using Common Table Expressions (CTEs) and the DENSE_RANK() window function. It's designed to be flexible, allowing you to specify any rank 'N'. This is useful for salary analysis, identifying performance tiers, or setting salary bands based on historical data.

Algorithm explanation:

The query first defines a CTE named 'RankedSalaries'. Inside the CTE, it selects distinct salaries from the 'employees' table. Then, it applies the DENSE_RANK() window function to these distinct salaries, ordering them in descending order. DENSE_RANK() assigns a rank to each unique salary, ensuring no gaps in ranking even if there are duplicate salaries. The main query then selects the salary from 'RankedSalaries' where the assigned rank matches the input parameter ':N'. This approach correctly handles duplicate salaries and ensures that if 'N' is larger than the number of distinct salaries, or if the table is empty, an empty result set is returned. The time complexity is typically O(N log N) or O(N) depending on the database's implementation of window functions and sorting, and space complexity is O(N) for storing distinct salaries and their ranks within the CTE.

Pseudocode:

1. Define a CTE 'RankedSalaries'.
2. Inside the CTE:
   a. Select distinct salaries from the employees table.
   b. Assign a dense rank to each distinct salary in descending order.
3. Select the salary from 'RankedSalaries' where the rank equals the input parameter N.
4. Handle edge cases: empty table, N out of bounds, duplicate salaries.