Comparative Analysis of Three Window Function Methods for Querying the Second Highest Salary in Oracle Database

Dec 08, 2025 · Programming · 9 views · 7.8

Keywords: Oracle Database | Window Functions | Ranking Queries

Abstract: This paper provides an in-depth exploration of three primary methods for querying the second highest salary record in Oracle databases: the ROW_NUMBER(), RANK(), and DENSE_RANK() window functions. Through comparative analysis of how these three functions handle duplicate salary values differently, it explains the core distinctions: ROW_NUMBER() generates unique sequences, RANK() creates ranking gaps, and DENSE_RANK() maintains continuous rankings. The article includes concrete SQL examples, discusses how to select the most appropriate query strategy based on actual business requirements, and offers complete code implementations along with performance considerations.

Application of Window Functions in Oracle Ranking Queries

In database queries, retrieving records with specific rankings is a common business requirement. Oracle database provides powerful window function capabilities that can efficiently handle such ranking queries. This paper focuses on analyzing three window functions: ROW_NUMBER(), RANK(), and DENSE_RANK(), which exhibit different behavioral characteristics when querying the second highest salary record.

ROW_NUMBER() Function: Generating Unique Sequences

The ROW_NUMBER() function assigns a unique consecutive integer to each row in the result set, allocating different sequence numbers even when sort field values are identical. This characteristic makes it particularly suitable for scenarios requiring guaranteed return of a single record.

SELECT * FROM (
  SELECT e.*, ROW_NUMBER() OVER (ORDER BY sal DESC) rn FROM emp e
)
WHERE rn = 2;

The above query first assigns row numbers to all employee records in descending order of salary, then filters for the record with row number 2. If multiple employees share the same second highest salary, this method will randomly select one of them to return.

RANK() and DENSE_RANK() Functions: Handling Duplicate Values

The RANK() and DENSE_RANK() functions exhibit similar behavior when encountering identical sort values, but their ranking allocation strategies have crucial differences.

-- Using RANK() function
SELECT * FROM (
  SELECT e.*, RANK() OVER (ORDER BY sal DESC) rnk FROM emp e
)
WHERE rnk = 2;

-- Using DENSE_RANK() function
SELECT * FROM (
  SELECT e.*, DENSE_RANK() OVER (ORDER BY sal DESC) drnk FROM emp e
)
WHERE drnk = 2;

Comparative Analysis of the Three Functions

Understanding the differences between these three functions is crucial for correctly selecting query methods:

Selection of Practical Application Scenarios

The choice of which function to use depends on specific business requirements:

  1. When it is necessary to ensure the return of a single record, even if multiple qualifying records exist, the ROW_NUMBER() function should be used.
  2. When following Olympic medal ranking logic (skipping subsequent rankings after tied positions), the RANK() function should be used.
  3. When maintaining ranking continuity without skipping any positions is required, the DENSE_RANK() function should be used.

Performance Optimization Recommendations

In actual production environments, besides functional correctness, query performance is an important consideration:

Conclusion

Oracle database's window functions provide flexible and powerful solutions for ranking queries. The ROW_NUMBER(), RANK(), and DENSE_RANK() functions each have their applicable scenarios, and understanding their core differences is key to correct query implementation. In practical applications, the most suitable function should be selected based on specific business requirements, data characteristics, and performance considerations to ensure accuracy and efficiency of query results.

Copyright Notice: All rights in this article are reserved by the operators of DevGex. Reasonable sharing and citation are welcome; any reproduction, excerpting, or re-publication without prior permission is prohibited.