Comparison and Selection of Rank and other functions in Oracle Database

Source: Internet
Author: User

Oracle DatabaseWhat are the differences between the Rank, Dense_Rank, and Row_Number functions? In practical applications, how should we choose? This article describes these in combination with examples.

First, create an employee information table and execute the following code in the query Analyzer:

 
 
  1. Create Table EmployeeInfo (CODE Number(3) Not Null,EmployeeName varchar2(15),DepartmentID Number(3),Salary NUMBER(7,2),  
  2.  
  3. Constraint PK_EmployeeInfo Primary Key (CODE)); 

After the table is created, open the table in enterprise management and enter some information. To help you understand the entered content, I executed the query statement Select * From EMPLOYEEINFO; enter the employee information table as follows:

Execute the SQL statement:

 
 
  1. Select EMPLOYEENAME,SALARY,  
  2.  
  3. RANK() OVER (Order By SALARY Desc)  "RANK",  
  4.  
  5. DENSE_RANK() OVER (Order By SALARY Desc ) "DENSE_RANK",  
  6.  
  7. ROW_NUMBER() OVER(Order By SALARY Desc) "ROW_NUMBER"  
  8.  
  9.  From EMPLOYEEINFO 

The result is as follows:

Note:The Rank, Dense_rank, and Row_number functions generate a natural number from 1 to N for each record. The value of N may be less than or equal to the total number of records. The only difference between the three functions is the ranking strategy when the same data is encountered.

① ROW_NUMBER:

The Row_number function returns a unique value. When the same data is encountered, the ranking increases sequentially according to the record sequence in the record set.

② DENSE_RANK:

The Dense_rank function returns a unique value, except that when the same data is encountered, the ranking of all the same data is the same.

③ RANK:

The Rank function returns a unique value. Unless the same data is encountered, the ranking of all the same data is the same, at the same time, the ranking between the last record and the next record is empty.

At the same time, it can also be sorted by group, that is, adding Partition by groupField in the Over clause:

 
 
  1. Select DEPARTMENTID,EMPLOYEENAME,SALARY,  
  2.  
  3. RANK() OVER ( Partition By DEPARTMENTID Order By SALARY Desc)  "RANK",  
  4.  
  5. DENSE_RANK() OVER ( Partition By DEPARTMENTID Order By SALARY Desc ) "DENSE_RANK",  
  6.  
  7. ROW_NUMBER() OVER( Partition By DEPARTMENTID Order By SALARY Desc) "ROW_NUMBER"  
  8.  
  9. From EMPLOYEEINFO 

The result is as follows:

If a blank salary record is inserted, execute the preceding statement and the result is as follows:

It will be found that the null value is in the first place, which is obviously not the expected result. The solution is to add NULLS Last after the Over clause Order:

 
 
  1. Select EMPLOYEENAME,SALARY,  
  2.  
  3. RANK() OVER (Order By SALARY Desc  Nulls Last)  "RANK",  
  4.  
  5. DENSE_RANK() OVER (Order By SALARY Desc Nulls Last) "DENSE_RANK",  
  6.  
  7. ROW_NUMBER() OVER(Order By SALARY Desc Nulls Last ) "ROW_NUMBER"  
  8.  
  9. From EMPLOYEEINFO 

The result is as follows:

This article introduces you here. If you want to know

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.