The four most commonly used sorting functions in SQL can be used in job interviews

When we write SQL code, as long as there is sorting, the first thing that comes to mind must be ORDER BY, so many friends think that sorting is so simple.

Today I will introduce four sorting functions that you don’t use very often. They are the four good brothers ROW_NUMBER(), RANK(), DENSE_RANK(), and NTILE() that are often used in sorting.

Let's first create a test data table Scores

WITH t AS  
(SELECT 1 StuID,70 Score  
UNION ALL  
SELECT 2,85  
UNION ALL  
SELECT 3,85  
UNION ALL  
SELECT 4,80  
UNION ALL  
SELECT 5,74  
)  
SELECT * INTO Scores FROM t;  
SELECT * FROM Scores

The result is as follows:

Technology Exchange

Technology must learn to share and communicate, and it is not recommended to work behind closed doors. A person can go fast, a group of people can go farther.

This article is shared and organized by fans of the technology group. The source code, data, and technical exchanges of the article can be obtained by adding the exchange group. The group has more than 2,000 members. The best way to add notes is: source + interest direction, which is convenient for finding like-minded friends.

Method ①, Add WeChat ID: pythoner666, Remarks: from CSDN + Remarks
Method ②, WeChat search official account: Python learning and data mining, background reply: add group

1、ROW_NUMBER()

Definition : The function of the ROW_NUMBER() function is to sort the data queried by SELECT, and add a serial number to each piece of data. It cannot be used to rank students' grades. It is generally used for page-by-page queries, such as querying the top 10 queries 10-100 student.

1.1 Sorting students' grades

example

SELECT   
ROW_NUMBER() OVER (ORDER BY SCORE DESC) AS [RANK],*  
FROM Scores;

(Hint: you can slide the code left and right)

The result is as follows:

Here RANK is the order after the ranking of each student, and DESC is reversed according to Score

1.2 Obtain the result information of the second place

SELECT * FROM (  
SELECT ROW_NUMBER() OVER (ORDER BY SCORE DESC) AS [RANK],*  
FROM Scores  
) t WHERE t.RANK=2;

result:

The idea used here is to add a layer of SELECT
WHERE t.RANK>=1 AND t.RANK<=3 to the original sql with the idea of ​​pagination query. Is it just to get the grade information of the top three students?

2、RANK()

Definition: The RANK() function, as the name suggests, can rank a certain field. What is the difference between this and ROW_NUMBER()? ROW_NUMBER() is sorting. When there are students with the same grade, ROW_NUMBER() will sort them in turn. Their serial numbers are different, but Rank() is different. If the same appears, their rankings are the same. Let's see an example:

example

SELECT ROW_NUMBER() OVER (ORDER BY SCORE DESC) AS [RANK],*  
FROM Scores;  
  
SELECT RANK() OVER (ORDER BY SCORE DESC) AS [RANK],*  
FROM Scores;

result:

Above is the result of the ROW_NUMBER() function, and below is the result of the RANK() function.

When there are two students with the same grade, there will be a change. RANK() is 1-1-3-4-5, while ROW_NUMBER() is still 1-2-3-4-5, this is the difference between RANK() and ROW_NUMBER()

3、DENSE_RANK()

Definition : The DENSE_RANK() function is also a ranking function. It is similar to RANK() and ranks fields. So what is the difference between it and RANK()? Especially in the case of the same score, DENSE_RANK() ranking is continuous, RANK() is a jumping ranking, and the ranking function used in general is RANK() Let's look at an example:

example

SELECT   
RANK() OVER (ORDER BY SCORE DESC) AS [RANK],*  
FROM Scores;  
  
SELECT   
DENSE_RANK() OVER (ORDER BY SCORE DESC) AS [RANK],*  
FROM Scores;

result:

Above is the result of RANK(), below is the result of DENSE_RANK()

4、NTILE()

Definition: The NTILE() function is to distribute the rows in the ordered partition to the specified number of groups, each group has a number, and the number starts from 1, just like what we call 'partition', it is divided into several districts, one district How many will there be.

SELECT NTILE(1) OVER (ORDER BY SCORE DESC) AS \[RANK\],\* FROM Scores;  
  
SELECT NTILE(2) OVER (ORDER BY SCORE DESC) AS \[RANK\],\* FROM Scores;  
  
SELECT NTILE(3) OVER (ORDER BY SCORE DESC) AS \[RANK\],\* FROM Scores;

result:

It is to divide the queried records into equal partitions according to the parameters in the NTILE function.

The introduction of these brothers is over, if you think it is good, remember to help forward and share it with more people

Guess you like

Origin blog.csdn.net/qq_34160248/article/details/130095168